-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #686 from NEUBIAS/add-py-strings-paths
Added basic implementation for string and path manipulation in python
- Loading branch information
Showing
3 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
# TODO | ||
# %% | ||
# Creating log messages | ||
|
||
image_index = 11 | ||
num_images = 100 | ||
|
||
# %% | ||
# strings can be added together with the '+' operator | ||
print("Analyzing image " + str(image_index) + "/" + str(num_images) + "...") | ||
|
||
# %% | ||
# An alternative way to combine strings and variables from your code | ||
# are so called f-strings: in f-strings you can add variables (and some | ||
# code) in between curly braces. Types are automatically coerced to strings. | ||
# The below code achieves the same output. | ||
print(f"Analysing image {image_index}/{num_images}...") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
// manipulating file paths | ||
tmpFolder = getDirectory("temp"); | ||
print(tmpFolder); | ||
fileName = "nuclei.tif"; | ||
path = tmpFolder + File.separator + fileName; | ||
path = tmpFolder + fileName; | ||
print(path); | ||
|
||
// Let's create a file name for a label image derived from the nuclei.tif | ||
// such the file name will be nuclei_labels.tif. | ||
filenameWithoutExtension = File.getNameWithoutExtension(path); | ||
print(filenameWithoutExtension) | ||
labelsPath = tmpFolder + filenameWithoutExtension + "_labels.tif"; | ||
print(labelsPath); | ||
|
||
// Now we want to create a file name for a .csv file, that shares the | ||
// file name up to the extension. | ||
csvPath = tmpFolder + filenameWithoutExtension + ".csv"; | ||
print(csvPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,38 @@ | ||
# TODO | ||
# %% | ||
# Manipulating file paths | ||
|
||
import pathlib | ||
import tempfile | ||
|
||
# %% | ||
# Let's get a folder and a file name to practice with. | ||
# Temporary folder is different on different operating systems. | ||
# We use tempfile.gettempdir() from the standard library as it has all that logic. | ||
tmp_folder = pathlib.Path(tempfile.gettempdir()) | ||
file_name = "nuclei.tif" | ||
|
||
# %% | ||
# Combine folder and file name. | ||
# pathlib.Path objects support "syntactic sugar", like using '/' as a | ||
# separator when composing paths. | ||
image_path = tmp_folder / file_name | ||
print(image_path) | ||
|
||
# %% | ||
# The parent for a Path object gives you the containing folder | ||
parent_folder = image_path.parent | ||
print(parent_folder == tmp_folder) | ||
|
||
# %% | ||
# Let's create a file name for a label image derived from the nuclei.tif | ||
# such the file name will be nuclei_labels.tif. | ||
filename_no_extension = image_path.stem | ||
print(filename_no_extension) | ||
labels_path = tmp_folder / f"{filename_no_extension}_labels.tif" | ||
|
||
# %% | ||
# Now we want to create a file name for a .csv file, that shares the | ||
# file name up to the extension. | ||
# If we only change the extension, we can use the with_suffix method. | ||
csv_path = image_path.with_suffix(".csv") | ||
print(csv_path) |