Skip to content

Commit

Permalink
EasIlastik 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanlegourrierec committed Apr 12, 2024
1 parent 2e4e9be commit b85860b
Show file tree
Hide file tree
Showing 6 changed files with 838 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store

dist/
EasIlastik.egg-info/
EasIlastik/__pycache__/
setup.py
test.py
Empty file added EasIlastik/__init__.py
Empty file.
59 changes: 59 additions & 0 deletions EasIlastik/find_ilastik.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import platform
import time
import os

def get_os():
"""
This function returns the name of the operating system on which the Python interpreter is running.
Returns:
str: The name of the operating system. Possible values are 'Linux', 'Darwin' (for MacOS), or 'Windows'.
"""

return platform.system()


def find_file(filename, start_path):
"""
This function searches for a specified file starting from a specified path.
Parameters:
filename (str): The name of the file to search for.
start_path (str): The path to start the search from.
Returns:
str: The path to the file if found, else None.
"""

start_time = time.time()

for root, dirs, files in os.walk(start_path):
if time.time() - start_time > 60:
return None
if filename in files:
return os.path.join(root, filename)

return None


def find_ilastik():
"""
This function searches for the Ilastik executable file on the current operating system.
Returns:
str: The path to the Ilastik executable if found, else None.
"""

os_name = get_os()

if os_name == 'Darwin' or os_name == 'Linux':
filename = 'run_ilastik.sh'
start_path = '/Applications' if os_name == 'Darwin' else '/'
elif os_name == 'Windows':
filename = 'ilastik.exe'
start_path = 'C:\\'
else:
print(f'Unsupported OS: {os_name}')
return None

return find_file(filename, start_path)
80 changes: 80 additions & 0 deletions EasIlastik/run_ilastik.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import os
import subprocess
from EasIlastik.find_ilastik import find_ilastik
from EasIlastik.utils import get_image_paths

def run_ilastik(input_path, project_path, result_base_path, ilastik_script_path = find_ilastik() ,export_source="Simple Segmentation", output_format="png"):
"""
This function runs the Ilastik software in headless mode with the specified parameters.
Parameters:
input_path (str): The path to the image file or folder to be processed.
ilastik_script_path (str): The path to the Ilastik script.
project_path (str): The path to the Ilastik project file.
result_base_path (str): The base path where the result will be saved.
export_source (str, optional): The type of data to export. Default is "Simple Segmentation". Must be one of ["Probabilities", "Simple Segmentation", "Uncertainty", "Features", "Labels"].
output_format (str, optional): The format of the output file. Default is "png". Must be one oof ["bmp", "gif", "hdr", "jpeg", "jpg", "pbm", "pgm", "png", "pnm", "ppm", "ras", "tif", "tiff", "xv", "bmp sequence", "gif sequence", "hdr sequence", "jpeg sequence", "jpg sequence", "pbm sequence", "pgm sequence", "png sequence", "pnm sequence", "ppm sequence", "ras sequence", "tif sequence", "tiff sequence", "xv sequence", "multipage tiff", "multipage tiff sequence", "hdf5", "compressed hdf5", "numpy, dvid"].
Returns:
None
Raises:
ValueError: If export_source or output_format is not in the allowed list.
subprocess.CalledProcessError: If there is an error during the execution of the Ilastik command.
"""

if ilastik_script_path is None:
print("ilastik_script_path is None. Please check the path format at https://learn.microsoft.com/fr-fr/dotnet/standard/io/file-path-formats")
return

ALLOWED_SOURCES = ["Probabilities", "Simple Segmentation", "Uncertainty", "Features", "Labels"]
ALLOWED_FORMATS = ["bmp", "gif", "hdr", "jpeg", "jpg", "pbm", "pgm", "png", "pnm", "ppm", "ras",
"tif", "tiff", "xv", "bmp sequence", "gif sequence", "hdr sequence", "jpeg sequence",
"jpg sequence", "pbm sequence", "pgm sequence", "png sequence", "pnm sequence",
"ppm sequence", "ras sequence", "tif sequence", "tiff sequence", "xv sequence",
"multipage tiff", "multipage tiff sequence", "hdf5", "compressed hdf5", "numpy, dvid"]

if export_source not in ALLOWED_SOURCES:
raise ValueError(f"Invalid export_source. Allowed values are {ALLOWED_SOURCES}")

if output_format not in ALLOWED_FORMATS:
raise ValueError(f"Invalid output_format. Allowed values are {ALLOWED_FORMATS}")

# Check if result_base_path exists, if not, create it
if not os.path.exists(result_base_path):
os.makedirs(result_base_path)

# Check if input_path is a directory or a file
if os.path.isdir(input_path):
image_arg = get_image_paths(input_path)
# Arguments to execute Ilastik in headless mode
ilastik_args = [
ilastik_script_path,
"--headless",
"--project=" + project_path,
"--export_source=" + export_source,
"--output_format=" + output_format,
"--output_filename_format=" + result_base_path + "{nickname}_" + export_source.replace(" ", "_"),
*image_arg
]

else:
image_arg = input_path
# Arguments to execute Ilastik in headless mode
ilastik_args = [
ilastik_script_path,
"--headless",
"--project=" + project_path,
"--export_source=" + export_source,
"--output_format=" + output_format,
"--output_filename_format=" + result_base_path + "{nickname}_" + export_source.replace(" ", "_"),
image_arg
]

# Execute the Ilastik command in headless mode with the specified arguments
try:
subprocess.run(ilastik_args, check=True)
print(f"Conversion of {input_path} completed successfully")
except subprocess.CalledProcessError as e:
print("Error during conversion : ", e)
raise RuntimeError("Error during Ilastik execution. See console output for details.")
18 changes: 18 additions & 0 deletions EasIlastik/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import glob

def get_image_paths(image_folder):
"""
This function retrieves the paths of all image files in a specified folder.
Parameters:
image_folder (str): The path to the folder containing the images.
Returns:
list: A list of paths to the image files.
"""

# Use glob to get all file paths in the image_folder
image_paths = glob.glob(os.path.join(image_folder, '*'))

return image_paths
Loading

0 comments on commit b85860b

Please sign in to comment.