Skip to content

Commit

Permalink
added illumination correction option
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Perez Meza committed Oct 31, 2024
1 parent a7a0720 commit b02f3e7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
6 changes: 6 additions & 0 deletions macsima2mc/CLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def get_args():
help='string specifying the name of the reference marker'
)

parser.add_argument('-ic',
'--illumination_correction',
action='store_true',
help='Applies illumination correction to all tiles, the illumination profiles are created with basicpy'
)


parser.add_argument('-he',
'--hi_exposure_only',
Expand Down
24 changes: 24 additions & 0 deletions macsima2mc/illumination_corr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from basicpy import BaSiC
import numpy as np

def indices_per_channel(total_imgs,no_of_channels):
#total_imgs in the stack
img_indices=[ list( range(ch,total_imgs,no_of_channels) ) for ch in range( 0, no_of_channels ) ]
return img_indices

def extract_channel_imgs (stack,indices):
return stack[indices,:,:]

def apply_corr(uncorr_stack,no_of_channels):
corr_stack=np.zeros( uncorr_stack.shape,dtype=uncorr_stack.dtype )
total_imgs=uncorr_stack.shape[0]
indices=indices_per_channel(total_imgs, no_of_channels)
basic = BaSiC(get_darkfield=False, smoothness_flatfield=1.0,fitting_mode = "approximate", sort_intensity = True)
for ind_list in indices:
uncorr_imgs=extract_channel_imgs(uncorr_stack, ind_list)
basic.fit(uncorr_imgs)
ffp=basic.flatfield
corr_stack[ind_list,:,:]=np.uint16( np.clip( uncorr_imgs.astype(float)/ffp ,0, 65535) )
return corr_stack


3 changes: 2 additions & 1 deletion macsima2mc/macsima2mc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def main(args):
input=args.input
output=args.output
ref=args.reference_marker
basicpy_corr=args.illumination_correction
cycle_info=tools.cycle_info( input , macsima_pattern(version=2),ref_marker= ref)
cycle_info=tools.append_metadata( cycle_info )
#cycle_info.to_csv( args.output / 'cycle_{c}_info.csv'.format(c=f'{6:03d}'), index=False )
output_dirs=tools.create_stack( cycle_info, output ,ref_marker=ref,hi_exp=args.hi_exposure_only )
output_dirs=tools.create_stack( cycle_info, output ,ref_marker=ref,hi_exp=args.hi_exposure_only,ill_corr=basicpy_corr)
[mc_tools.write_markers_file(path) for path in output_dirs]


Expand Down
13 changes: 11 additions & 2 deletions macsima2mc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
from pathlib import Path
import ome_writer
import illumination_corr


def merge_dicts(list_of_dicts):
Expand Down Expand Up @@ -183,7 +184,7 @@ def select_by_exposure(list_indices,exp_index=4,target='max'):

return selected_indices

def create_stack(cycle_info_df,output_dir,ref_marker='DAPI',hi_exp=False,extended_outputs=False):
def create_stack(cycle_info_df,output_dir,ref_marker='DAPI',hi_exp=False,ill_corr=False,extended_outputs=False):

if extended_outputs:
out=outputs_dic()
Expand Down Expand Up @@ -225,7 +226,15 @@ def create_stack(cycle_info_df,output_dir,ref_marker='DAPI',hi_exp=False,extende
stack[counter,:,:]=tifff.imread(Path(target_path))
counter+=1
stack_name =cast_stack_name(frame.cycle.iloc[0],index,conformed_markers)
stack_file_path= stack_output_dir/ stack_name

if ill_corr:
tag='corr_'
no_of_channels=len(conformed_markers)
stack=illumination_corr.apply_corr(stack,no_of_channels)
else:
tag=''

stack_file_path= stack_output_dir/ '{prefix}{base}'.format(prefix=tag,base=stack_name)

if extended_outputs:
out['index'].append(index)
Expand Down

0 comments on commit b02f3e7

Please sign in to comment.