Skip to content

Commit

Permalink
First draft of CLI implementation passed
Browse files Browse the repository at this point in the history
The code must be ran in the folder where the cli code is created. The
input nifti must be present in this folder. There are important things
to analyze here regarding saving the files.
  • Loading branch information
sriosq committed Jul 4, 2024
1 parent 4de1a89 commit 1aad131
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 42 deletions.
47 changes: 28 additions & 19 deletions cli/tissue_to_mr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import click
import os
import sys
import logging
import nibabel as nib
# With the next line we add to path the project folder to navigate into functions folder
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

Expand All @@ -9,16 +11,9 @@

# tissue_to_mr --input [labeled_nifti] --type [choose_MR_property] --output [name=default]

@click.command()
@click.option('-i',"--input",help="Input must be segmented label nifti")
@click.option("--type",help="Please choose MR property to convert to")
@click.option('-o',"--output",help="Output name (optional)")
def hello(input,type,output):
if output == None:
output = "default"
click.echo(f"Input: {input} Type: {type} Output:{output}")
else:
click.echo(f"Input: {input} Type: {type} Output:{output}")
@click.group
def my_commands():
pass

PROPERTIES = {
"t2s" : "T2 star",
Expand All @@ -28,18 +23,32 @@ def hello(input,type,output):
"t2" : "T2",
}
@click.command()
@click.option('-i',"--input",help="Input must be segmented label nifti")
@click.option("--type",type=click.Choice(PROPERTIES.keys()), help="Please choose MR property to convert to")
def converter(input,type):
@click.argument("input_file", required=True) #, help="Input must be segmented label nifti",type=click.Path(exists=True))
@click.option('-s',"--segtool", help="State what segmentator was used")
@click.argument('output_file', required=False, type=click.Path())
@click.option('-t',"--type",required=True, type=click.Choice(PROPERTIES.keys()), help="Please choose MR property to convert to")
def converter(input_file,segtool,output_file, type):
# We need to check if the input is a nifti file
if is_nifti(input):
new_vol = volume(input)
if is_nifti(input_file):
print("start")
logging.info(f"Creating a new volume with {type} values")
file = nib.load(input_file)
print("file loaded")
new_vol = volume(file)
print("a")
# Using the type:
new_vol.create_segmentation_labels() # Automatically adding the names to known labels
new_vol.create_type_vol(type)
else:
print("Input must be a nifti file")
print("b")
if output_file == None:

new_vol.create_type_vol(type) # Thiscreates and saves a Nifti file
else:
new_vol.create_type_vol(type,output_file)

print(f"Input segmented by:{segtool}")
else:
print("Input must be a Nifti file (.nii or .nii.gz extensions)")

my_commands.add_command(converter)
if __name__ == "__main__":
converter()
my_commands()
2 changes: 1 addition & 1 deletion functions/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def is_nifti(filepath):
Returns:
bool: True if the file is a NIfTI file, False otherwise.
"""
if filepath[-4:] == '.nii':
if filepath[-4:] == '.nii' or filepath[-7:] == '.nii.gz':
return True
else:
return False
61 changes: 41 additions & 20 deletions functions/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,25 @@ def show_labels(self):
label = self.segmentation_labels[i]
print(label) # Calling __str__ from label

def create_type_vol(self,type):
def create_type_vol(self,type,output_name="default"):
# This function is for the CLI app
# Depending on the type we automatically call the specific function
#
if type =='sus':
self.create_sus_dist()
self.save_sus_dist_nii()
self.save_sus_dist_nii(output_name)
if type =='t2s':
self.create_t2_star_vol()
self.save_t2star_dist()
self.save_t2star_dist(output_name)
if type =='pd':
self.create_pd_vol()
self.save_pd_dist()
self.save_pd_dist(output_name)
if type =='t1':
print("T1 value volume comming soon!")
if type =='t2':
print("T2 volume comming soon!")



def create_sus_dist(self):
# Code for create a susceptibility distribution volume
# Using the label class
Expand All @@ -233,12 +232,20 @@ def create_sus_dist(self):

return self.sus_dist

def save_sus_dist_nii(self):
def save_sus_dist_nii(self, fn):
# Method to save the susceptibility distribution created to nifti
temp_img = nib.Nifti1Image(self.sus_dist, affine=self.nifti.affine)
path = os.path.join('output','sus_dist.nii.gz')
# Save the new NIfTI image to a file
nib.save(temp_img,path)
if fn == "default":
fn = 'sus_dist.nii.gz'
temp_img = nib.Nifti1Image(self.sus_dist, affine=self.nifti.affine)
path = os.path.join('output', fn)
# Save the new NIfTI image to a file
nib.save(temp_img,path)
else:
temp_img = nib.Nifti1Image(self.sus_dist, affine=self.nifti.affine)
path = os.path.join('output', fn)
# Save the new NIfTI image to a file
nib.save(temp_img,path)

del temp_img
del path

Expand All @@ -261,12 +268,19 @@ def create_pd_vol(self):
self.pd_dist[i,j,k] = pd

return self.pd_dist
def save_pd_dist(self):
def save_pd_dist(self, fn = 'default'):
# Method to save the proton density distribution created to nifti
temp_img = nib.Nifti1Image(self.pd_dist, affine=self.nifti.affine)
# Save the new NIfTI image to a file
path = os.path.join('output', 'pd_dist.nii.gz')
nib.save(temp_img,path)
if fn == "default":
fn = 'pd_dist.nii.gz'
temp_img = nib.Nifti1Image(self.pd_dist, affine=self.nifti.affine)
path = os.path.join('output', fn)
# Save the new NIfTI image to a file
nib.save(temp_img,path)
else:
temp_img = nib.Nifti1Image(self.pd_dist, affine=self.nifti.affine)
path = os.path.join('output', fn)
# Save the new NIfTI image to a file
nib.save(temp_img,path)
del temp_img
del path
def create_t2_star_vol(self):
Expand All @@ -287,12 +301,19 @@ def create_t2_star_vol(self):
self.t2star_vol[i,j,k] = t2star

return self.t2star_vol
def save_t2star_dist(self):
def save_t2star_dist(self, fn = "default"):
# Method to save the proton density distribution created to nifti
temp_img = nib.Nifti1Image(self.t2star_vol, affine=self.nifti.affine)
# Save the new NIfTI image to a file
path = os.path.join('output', 't2_star.nii.gz')
nib.save(temp_img,path)
if fn == "default":
fn = 't2_star.nii.gz'
temp_img = nib.Nifti1Image(self.t2star_vol, affine=self.nifti.affine)
path = os.path.join('output', fn)
# Save the new NIfTI image to a file
nib.save(temp_img,path)
else:
temp_img = nib.Nifti1Image(self.t2star_vol, affine=self.nifti.affine)
path = os.path.join('output', fn)
# Save the new NIfTI image to a file
nib.save(temp_img,path)
del temp_img
del path
def save_sus_csv(self):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
click
pandas
nibabel
nilearn
Expand Down
32 changes: 30 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
from setuptools import find_packages, setup
from os import path

# Get directory where this current file is saved
here = path.abspath(path.dirname(__file__))

with open(path.join(here, "README.rst"), encoding="utf-8") as f:
long_description = f.read()

setup(
name="functions",
packages=find_packages(),
name="tissue-to-MRproperty",
description = "Code that creates a volume from a segmented Nifti file to a selected MR property",
long_description = long_description,
keywords='',
entry_points = {
'console_scripts': [
"tissue_to_MR = tissue-to-MRproperty.cli.tissue_to_mr:converter"
]
},
packages=find_packages(exclude=["docs"]),
install_requires=[
"click",
"pandas",
"nibabel",
"nilearn",
"numpy",
"matplotlib",
"ipykernel",
"ipython",
"treelib",
"pytest",
"jupyterlab"
]
)

0 comments on commit 1aad131

Please sign in to comment.