2019-03-29 11:07:31 +01:00
# String(label="Please enter your name",description="Name field") name
# OUTPUT String greeting
# A Jython script with parameters.
# It is the duty of the scripting framework to harvest
# the 'name' parameter from the user, and then display
# the 'greeting' output parameter, based on its type.
2019-04-11 20:23:20 +02:00
from ij import IJ # pylint: disable=import-error
from ij import ImagePlus # pylint: disable=import-error
2019-03-29 11:07:31 +01:00
# greeting = "Hello, " + name + "!"
# image prefix :
# AF
# blé : coupes de blé
# CA : coupe d'amande
# FE : feuille d'épinard
# GGH : globule gras humain
# CRF chloroplastes de feuille d'épinard
# OL : oléosome
# DARK : dark
# white :
#
# - cin1 : cinétique 1
# phiG_40x_1 : cinétique avant et après injection enzyme gastrique
# phiG_40x_Zstack20um_1 : stack
# 0mn : on commence à enregistrer et on attend 10mn (pour le bleaching) -> phiG_40x_1
# 10mn : debut injection phase gastrique (poussée)
# 13mn : la phase gastrique (le petit tuyau contient 20ul) arrive dans la cellule d'un coup (1 nanol)
# 15mn : on arrête l'injection
# 50mn : on fait un stack -> phiG_40x_Zstack20um_1
# 51mn : début d'injection phase intestinale (poussée) -> phiG_I_40x_1
# x mn : on arrête l'injection
# 90mn : on fait un stack -> phiG_I_40x_Zstack20um_1
# - cin2 : autre échantillon similaire à cin1
# - cond[5678] : condition non réalistes
import json
2019-04-11 20:23:20 +02:00
2019-03-29 11:07:31 +01:00
class Sequence ( object ) :
def __init__ ( self , catalog , sequence_id , micro_manager_metadata_file_path ) :
self . catalog = catalog
self . sequence_id = sequence_id
self . micro_manager_metadata_file_path = micro_manager_metadata_file_path
print ( micro_manager_metadata_file_path )
with open ( micro_manager_metadata_file_path , " r " ) as mmm_file :
2019-04-11 20:23:20 +02:00
self . mmm = json . load ( mmm_file , encoding = ' latin-1 ' ) # note : the micromanager metadata files are encoded in latin-1, not utf8 (see accents in comments)
2019-03-29 11:07:31 +01:00
@property
def num_frames ( self ) :
summary = self . mmm [ ' Summary ' ]
return int ( summary [ ' Frames ' ] )
@property
def width ( self ) :
summary = self . mmm [ ' Summary ' ]
return int ( summary [ ' Width ' ] )
@property
def height ( self ) :
summary = self . mmm [ ' Summary ' ]
return int ( summary [ ' Height ' ] )
@property
def num_channels ( self ) :
summary = self . mmm [ ' Summary ' ]
return int ( summary [ ' Channels ' ] )
@property
def num_slices ( self ) :
summary = self . mmm [ ' Summary ' ]
return int ( summary [ ' Slices ' ] )
@property
def num_bits_per_pixels ( self ) :
summary = self . mmm [ ' Summary ' ]
return int ( summary [ ' BitDepth ' ] )
def get_root_path ( self ) :
return ' / ' . join ( self . micro_manager_metadata_file_path . split ( ' / ' ) [ : - 1 ] )
2019-03-29 11:57:52 +01:00
def get_image_file_path ( self , channel_index , frame_index , z_index = 0 ) :
2019-04-11 20:23:20 +02:00
'''
: param int channel_index :
: param int frame_index :
: param int z_index :
'''
2019-03-29 11:07:31 +01:00
assert frame_index < self . num_frames
2019-03-29 11:57:52 +01:00
assert channel_index < self . num_channels
2019-04-11 20:23:20 +02:00
frame_info = self . mmm [ ' FrameKey- %d - %d - %d ' % ( frame_index , channel_index , z_index ) ]
2019-03-29 11:07:31 +01:00
rel_file_path = frame_info [ ' FileName ' ]
return self . get_root_path ( ) + ' / ' + rel_file_path
2019-03-29 11:57:52 +01:00
def get_channel_index ( self , channel_id ) :
2019-04-11 20:23:20 +02:00
'''
: param str channel_id :
'''
2019-03-29 11:57:52 +01:00
summary = self . mmm [ ' Summary ' ]
channel_index = summary [ ' ChNames ' ] . index ( channel_id )
2019-04-11 20:23:20 +02:00
return channel_index
def get_black ( self ) :
''' returns the black sequence related to the the sequence self
: return Sequence :
'''
seqid_to_black = {
' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0 ' : ' res_soleil2018/DARK/DARK_40X_60min_1 im pae min_1/Pos0 ' ,
' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos2 ' : ' res_soleil2018/DARK/DARK_40X_60min_1 im pae min_1/Pos0 ' ,
}
white_sequence = seqid_to_black [ self . sequence_id ]
return self . catalog . sequences [ white_sequence ]
def get_white ( self ) :
''' returns the white sequence related to the the sequence self
: return Sequence :
'''
seqid_to_white = {
' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0 ' : ' res_soleil2018/white/white_24112018_2/Pos0 ' ,
' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos2 ' : ' res_soleil2018/white/white_24112018_2/Pos0 ' ,
}
white_sequence = seqid_to_white [ self . sequence_id ]
return self . catalog . sequences [ white_sequence ]
2019-03-29 11:57:52 +01:00
2019-03-29 11:07:31 +01:00
def open_in_imagej ( self ) :
2019-04-11 20:23:20 +02:00
# ip = IJ.createHyperStack(title=self.sequence_id, width=self.width, height= self.height, channels=1, slices=1, frames=self.get_num_frames(), bitdepth=16)
2019-03-29 11:07:31 +01:00
hyperstack = IJ . createHyperStack ( self . sequence_id , self . width , self . height , self . num_channels , self . num_slices , self . num_frames , self . num_bits_per_pixels )
2019-03-29 11:57:52 +01:00
for channel_index in range ( self . num_channels ) :
2019-03-29 11:07:31 +01:00
for frame_index in range ( self . num_frames ) :
slice_index = 0
2019-03-29 11:57:52 +01:00
src_image_file_path = self . get_image_file_path ( channel_index = channel_index , frame_index = frame_index )
# print(src_image_file_path)
2019-03-29 11:07:31 +01:00
src_image = IJ . openImage ( src_image_file_path )
2019-03-29 11:57:52 +01:00
# print(src_image.getProperties())
2019-04-11 20:23:20 +02:00
hyperstack . setPositionWithoutUpdate ( channel_index + 1 , slice_index + 1 , frame_index + 1 )
2019-03-29 11:07:31 +01:00
hyperstack . setProcessor ( src_image . getProcessor ( ) )
hyperstack . show ( )
2019-03-29 11:57:52 +01:00
for channel_index in range ( self . num_channels ) :
2019-04-11 20:23:20 +02:00
hyperstack . setPositionWithoutUpdate ( channel_index + 1 , 1 , 1 )
2019-03-29 11:57:52 +01:00
IJ . run ( " Enhance Contrast " , " saturated=0.35 " )
2019-04-11 20:23:20 +02:00
return hyperstack
2019-03-29 11:07:31 +01:00
class ImageCatalog ( object ) :
def __init__ ( self , raw_images_root ) :
self . raw_images_root = raw_images_root
2019-04-11 20:23:20 +02:00
self . sequences = { }
2019-03-29 11:57:52 +01:00
2019-04-01 12:26:37 +02:00
# nb : we use the path as sequence id because the "Comment" field in the summary section of the metadata file is not guaranteed to be unique (eg they are the same in res_soleil2018/white/white_24112018_1/Pos0 and in res_soleil2018/white/white_24112018_2/Pos0)
sequence_ids = [ ]
sequence_ids . append ( ' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0 ' )
sequence_ids . append ( ' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos2 ' )
2019-04-11 20:23:20 +02:00
2019-04-01 12:26:37 +02:00
sequence_ids . append ( ' res_soleil2018/DARK/DARK_40X_60min_1 im pae min_1/Pos0 ' )
2019-04-11 20:23:20 +02:00
sequence_ids . append ( ' res_soleil2018/DARK/DARK_40X_zstack_vis_327-353_1/Pos0 ' )
2019-04-01 12:26:37 +02:00
# sequence_ids.append('res_soleil2018/white/white_24112018_1/Pos0') # this sequence seems broken (only 5 images while there's supposed to be 201 frames)
sequence_ids . append ( ' res_soleil2018/white/white_24112018_2/Pos0 ' )
for sequence_id in sequence_ids :
2019-03-29 11:57:52 +01:00
micro_manager_metadata_file_path = raw_images_root + ' / ' + sequence_id + ' /metadata.txt '
2019-04-11 20:23:20 +02:00
# micro_manager_metadata_file_path = '/tmp/toto.json'
self . sequences [ sequence_id ] = Sequence ( self , sequence_id , micro_manager_metadata_file_path )
2019-03-29 11:07:31 +01:00
def __str__ ( self ) :
for sequence_id , sequence in self . sequences . iteritems ( ) :
return str ( sequence_id ) + ' : ' + str ( sequence )
# self.add_micromanager_metadata(raw_images_root + '/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0/metadata.txt')
2019-04-11 20:23:20 +02:00
# def add_micromanager_metadata(self, micro_manager_metadata_file_path):
2019-03-29 11:07:31 +01:00
# self.sequences[ micro_manager_metadata_file_path ] = Sequence(self, micro_manager_metadata_file_path)
2019-04-11 20:23:20 +02:00
def get_image_median_value ( src_image ) :
return 0
def test_get_image_median_value ( ) :
image_file_path = ' /Users/graffy/ownCloud/ipr/lipase/raw-images/res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0/img_000000000_DM300_327-353_fluo_000.tif '
2019-04-12 12:53:17 +02:00
image = IJ . openImage ( image_file_path )
2019-04-11 20:23:20 +02:00
median_value = get_image_median_value ( image )
print ( ' median value : %d ' % median_value )
# double median( cv::Mat channel )
# {
# double m = (channel.rows*channel.cols) / 2;
# int bin = 0;
# double med = -1.0;
# int histSize = 256;
# float range[] = { 0, 256 };
# const float* histRange = { range };
# bool uniform = true;
# bool accumulate = false;
# cv::Mat hist;
# cv::calcHist( &channel, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );
# for ( int i = 0; i < histSize && med < 0.0; ++i )
# {
# bin += cvRound( hist.at< float >( i ) );
# if ( bin > m && med < 0.0 )
# med = i;
# }
# return med;
# }
def find_depth_index ( src_image , white_sequence ) :
''' finds in the image sequence white_sequence the image that correlates the best to src_image
2019-04-12 12:53:17 +02:00
: param IJ . ImagePlus src_image :
2019-04-11 20:23:20 +02:00
: param Sequence white_sequence :
'''
for z_index in range ( white_sequence . num_slices ) :
white_image_file_path = white_sequence . get_image_file_path ( channel_index = 0 , frame_index = 0 , z_index = z_index )
2019-04-12 12:53:17 +02:00
white_image = IJ . openImage ( white_image_file_path )
2019-04-11 20:23:20 +02:00
# imp2mat = ImagePlusMatConverter()
# white_mat = imp2mat.toMat(white_image.getProcessor())
2019-04-12 12:53:17 +02:00
# white_mat = imread(white_image_file_path)
print ( white_image )
2019-04-11 20:23:20 +02:00
break
def process_sequence ( sequence ) :
'''
: param Sequence sequence :
'''
white_seq = sequence . get_white ( )
src_image_file_path = sequence . get_image_file_path ( channel_index = 0 , frame_index = 0 , z_index = 0 )
2019-04-12 12:53:17 +02:00
src_image = IJ . openImage ( src_image_file_path )
2019-04-11 20:23:20 +02:00
# src_image = IJ.openImage(src_image_file_path)
find_depth_index ( src_image , white_seq )
2019-03-29 11:07:31 +01:00
def run_script ( ) :
2019-04-01 12:26:37 +02:00
raw_images_root = ' /Users/graffy/ownCloud/ipr/lipase/raw-images '
2019-03-29 11:07:31 +01:00
catalog = ImageCatalog ( raw_images_root )
print ( catalog )
2019-04-11 20:23:20 +02:00
# catalog.sequences['GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0'].open_in_imagej()
# catalog.sequences['res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos2'].open_in_imagej()
# catalog.sequences['DARK_40X_60min_1 im pae min_1/Pos0'].open_in_imagej()
# catalog.sequences['white_24112018_1/Pos0'].open_in_imagej()
process_sequence ( catalog . sequences [ ' res_soleil2018/GGH/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos2 ' ] )
2019-03-29 11:57:52 +01:00
if False :
sequence = catalog . sequences [ ' GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0 ' ]
src_image_file_path = sequence . get_image_file_path ( channel_index = sequence . get_channel_index ( ' DM300_327-353_fluo ' ) , frame_index = 3 )
2019-04-11 20:23:20 +02:00
src_image = IJ . openImage ( src_image_file_path ) # pylint: disable=unused-variable
# dark_image = IJ.openImage(raw_images_root + '/GGH_2018_cin2_phiG_I_327_vis_-40_1/Pos0/img_000000000_DM300_327-353_fluo_000.tif')
# src_image.show()
# assert src_image
2019-03-29 11:07:31 +01:00
# If a Jython script is run, the variable __name__ contains the string '__main__'.
# If a script is loaded as module, __name__ has a different value.
2019-04-11 20:23:20 +02:00
if __name__ in [ ' __builtin__ ' , ' __main__ ' ] :
# run_script()
2019-04-12 12:53:17 +02:00
test_get_image_median_value ( )