Skip to content

Commit

Permalink
added ill_correction.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Perez Meza committed Oct 28, 2024
1 parent 997a714 commit 72f7200
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
28 changes: 28 additions & 0 deletions macsima_pipeline.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/bash
#SBATCH --job-name=basicpy_test
#SBATCH --time=12:00:00
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --mem=16gb
#SBATCH --array=1

#INPUTS
acquisitions=/hpc/scratch/hdd1/vp232003/tsvs/acquisitions.tsv
staging_container=/hpc/scratch/hdd1/vp232003/containers/multiplex_macsima_v1.0.0.sif
output_dir=/hpc/scratch/hdd1/vp232003/output/
#END INPUTS

sample=$(awk -v ArrayTaskID=$SLURM_ARRAY_TASK_ID '$1==ArrayTaskID {print $2}' $acquisitions)




for cycle in $sample/*;
do

cycle_folder=$(basename "$cycle")
sample_id=${SLURM_ARRAY_TASK_ID}
sample_id+="_$(basename $sample)"
singularity exec --bind $sample:/mnt,$output_dir:/media --no-home $staging_container python /staging/macsima2mc/macsima2mc.py -i /mnt/$cycle_folder -o /media/$sample_id
done

107 changes: 107 additions & 0 deletions scripts/ill_correction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import tifffile as tifff
import numpy as np
import os
from pathlib import Path
import argparse
import sys

#---CLI-BLOCK---#
def setup_cli():

parser=argparse.ArgumentParser()


parser.add_argument('-i',
'--input',
required=True,
help='path to the image to be corrected'
)

parser.add_argument('-o',
'--output',
required=True,
help='Directory where the corrected image will be written'
)

args=parser.parse_args()

return args

#---END_CLI-BLOCK---#

def args2dict():
args_dict={}
for index,element in enumerate(sys.argv):
if type(element)==str:
if element.startswith('-'):
args_dict[element]=sys.argv[index+1]
return args_dict



def cast_canvas(img_path):
with tifff.TiffFile(img_path) as tif:
im=tif.series[0].asarray()
stack_len=len(tif.pages)
channels=int(stack_len/len(tif.series))
x_size=im.shape[2]
y_size=im.shape[1]
canvas_img=np.zeros( (stack_len,y_size,x_size) ,dtype='uint16')

return canvas_img,(stack_len,channels)

def ill_profile(img_path):

file_name=img_path.stem
flat_field_path=img_path.parents[1] / 'illumination' / '{x}-ffp.tiff'.format(x=file_name)

return flat_field_path

def ill_correction(img_path,ffp_path):

ffp=tifff.imread( ffp_path )
canvas,(z_size,no_of_chan)=cast_canvas(img_path)

for ch in range(no_of_chan):
range_of_interest=range(ch,z_size,no_of_chan)
img=tifff.imread(img_path,key=range_of_interest)
ffp_tile=np.tile(ffp[:,:,ch],(img.shape[0],1,1))
img_corrected= img.astype(float) / ffp_tile

canvas[range_of_interest,:,:]=np.uint16(img_corrected)


return canvas

def apply_correction(img_path,out_dir):

img_name=img_path.stem + img_path.suffix
img_corr=ill_correction(img_path,ill_profile(img_path))
output_img_path=out_dir / img_name
ome_metadata=tifff.tiffcomment(img_path)
tifff.imwrite( output_img_path ,img_corr )
tifff.tiffcomment(output_img_path,ome_metadata)

return



def main(args_dict):
#read CLI arguments
img_path=Path(args_dict['-i'])
out_dir =Path(args_dict['-o'])
#apply correction to image and write result in out_dir
apply_correction(img_path,out_dir)



if __name__=='__main__':
args=setup_cli()
main(args2dict())







0 comments on commit 72f7200

Please sign in to comment.