Skip to content

Commit

Permalink
Merge pull request #686 from NEUBIAS/add-py-strings-paths
Browse files Browse the repository at this point in the history
Added basic implementation for string and path manipulation in python
  • Loading branch information
tischi authored May 28, 2024
2 parents 516ab72 + a405c3b commit 3ab4583
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
17 changes: 16 additions & 1 deletion _includes/string_concat/creating_log_messages.py
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}...")
15 changes: 14 additions & 1 deletion _includes/string_concat/manipulating_file_paths.ijm
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);
39 changes: 38 additions & 1 deletion _includes/string_concat/manipulating_file_paths.py
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)

0 comments on commit 3ab4583

Please sign in to comment.