Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic implementation for string and path manipulation in python #686

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) + "...")
k-dominik marked this conversation as resolved.
Show resolved Hide resolved

# %%
# 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.
k-dominik marked this conversation as resolved.
Show resolved Hide resolved
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)
Loading