<s> from cellprofiler . gui . help import LOADING_IMAGE_SEQ_HELP_REF <EOL> __doc__ = '''<STR_LIT>''' % globals ( ) <EOL> import numpy as np <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . cpimage as cpi <EOL> import cellprofiler . settings as cps <EOL> P_AVERAGE = '<STR_LIT>' <EOL> P_MAXIMUM = '<STR_LIT>' <EOL> P_MINIMUM = '<STR_LIT>' <EOL> P_SUM = '<STR_LIT>' <EOL> P_VARIANCE = '<STR_LIT>' <EOL> P_POWER = '<STR_LIT>' <EOL> P_BRIGHTFIELD = '<STR_LIT>' <EOL> P_MASK = '<STR_LIT>' <EOL> P_ALL = [ P_AVERAGE , P_MAXIMUM , P_MINIMUM , P_SUM , P_VARIANCE , P_POWER , <EOL> P_BRIGHTFIELD , P_MASK ] <EOL> K_PROVIDER = "<STR_LIT>" <EOL> class MakeProjection ( cpm . CPModule ) : <EOL> module_name = '<STR_LIT>' <EOL> category = '<STR_LIT>' <EOL> variable_revision_number = <NUM_LIT:2> <EOL> def create_settings ( self ) : <EOL> self . image_name = cps . ImageNameSubscriber ( <EOL> '<STR_LIT>' , cps . NONE , doc = '''<STR_LIT>''' ) <EOL> self . projection_type = cps . Choice ( <EOL> '<STR_LIT>' , <EOL> P_ALL , doc = '''<STR_LIT>''' % globals ( ) ) <EOL> self . projection_image_name = cps . ImageNameProvider ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , doc = '''<STR_LIT>''' , <EOL> provided_attributes = { cps . AGGREGATE_IMAGE_ATTRIBUTE : True , <EOL> cps . AVAILABLE_ON_LAST_ATTRIBUTE : True } ) <EOL> self . frequency = cps . Float ( <EOL> "<STR_LIT>" , <NUM_LIT> , minval = <NUM_LIT:1.0> , doc = """<STR_LIT>""" % globals ( ) ) <EOL> def settings ( self ) : <EOL> return [ self . image_name , self . projection_type , <EOL> self . projection_image_name , self . frequency ] <EOL> def visible_settings ( self ) : <EOL> result = [ self . image_name , self . projection_type , <EOL> self . projection_image_name ] <EOL> if self . projection_type == P_POWER : <EOL> result += [ self . frequency ] <EOL> return result <EOL> def prepare_group ( self , workspace , grouping , image_numbers ) : <EOL> '''<STR_LIT>''' <EOL> if len ( image_numbers ) > <NUM_LIT:0> : <EOL> provider = ImageProvider ( self . projection_image_name . value , <EOL> self . projection_type . value , <EOL> self . frequency . value ) <EOL> provider . save_state ( self . get_dictionary ( ) ) <EOL> return True <EOL> def run ( self , workspace ) : <EOL> provider = ImageProvider . restore_from_state ( self . get_dictionary ( ) ) <EOL> workspace . image_set . providers . append ( provider ) <EOL> image = workspace . image_set . get_image ( self . image_name . value ) <EOL> pixels = image . pixel_data <EOL> if not provider . has_image : <EOL> provider . set_image ( image ) <EOL> else : <EOL> provider . accumulate_image ( image ) <EOL> provider . save_state ( self . get_dictionary ( ) ) <EOL> if self . show_window : <EOL> workspace . display_data . pixels = pixels <EOL> workspace . display_data . provider_pixels = provider . provide_image ( workspace . image_set ) . pixel_data <EOL> def is_aggregation_module ( self ) : <EOL> '''<STR_LIT>''' <EOL> return True <EOL> def post_group ( self , workspace , grouping ) : <EOL> '''<STR_LIT>''' <EOL> image_set = workspace . image_set <EOL> if self . projection_image_name . value not in image_set . get_names ( ) : <EOL> provider = ImageProvider . restore_from_state ( self . get_dictionary ( ) ) <EOL> image_set . providers . append ( provider ) <EOL> def display ( self , workspace , figure ) : <EOL> pixels = workspace . display_data . pixels <EOL> provider_pixels = workspace . display_data . provider_pixels <EOL> figure . set_subplots ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> if provider_pixels . ndim == <NUM_LIT:3> : <EOL> figure . subplot_imshow ( <NUM_LIT:0> , <NUM_LIT:0> , pixels , <EOL> self . image_name . value ) <EOL> figure . subplot_imshow ( <NUM_LIT:1> , <NUM_LIT:0> , provider_pixels , <EOL> self . projection_image_name . value , <EOL> sharexy = figure . subplot ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> else : <EOL> figure . subplot_imshow_bw ( <NUM_LIT:0> , <NUM_LIT:0> , pixels , <EOL> self . image_name . value ) <EOL> figure . subplot_imshow_bw ( <NUM_LIT:1> , <NUM_LIT:0> , provider_pixels , <EOL> self . projection_image_name . value , <EOL> sharexy = figure . subplot ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> def upgrade_settings ( self , setting_values , <EOL> variable_revision_number , <EOL> module_name , from_matlab ) : <EOL> if from_matlab and module_name == '<STR_LIT>' : <EOL> setting_values = setting_values [ : <NUM_LIT:2> ] + P_AVERAGE <EOL> from_matlab = False <EOL> module_name = self . module_name <EOL> variable_revision_number = <NUM_LIT:1> <EOL> if ( from_matlab and module_name == '<STR_LIT>' and <EOL> variable_revision_number == <NUM_LIT:3> ) : <EOL> setting_values = setting_values [ : <NUM_LIT:3> ] <EOL> from_matlab = False <EOL> variable_revision_number = <NUM_LIT:1> <EOL> if ( not from_matlab ) and variable_revision_number == <NUM_LIT:1> : <EOL> setting_values = setting_values + [ "<STR_LIT>" ] <EOL> return setting_values , variable_revision_number , from_matlab <EOL> class ImageProvider ( cpi . AbstractImageProvider ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , how_to_accumulate , frequency = <NUM_LIT:6> ) : <EOL> """<STR_LIT>""" <EOL> super ( ImageProvider , self ) . __init__ ( ) <EOL> self . __name = name <EOL> self . frequency = frequency <EOL> self . __image = None <EOL> self . __how_to_accumulate = how_to_accumulate <EOL> self . __image_count = None <EOL> self . __cached_image = None <EOL> self . __vsquared = None <EOL> self . __vsum = None <EOL> self . __power_image = None <EOL> self . __power_mask = None <EOL> self . __stack_number = <NUM_LIT:0> <EOL> self . __bright_max = None <EOL> self . __bright_min = None <EOL> self . __norm0 = None <EOL> D_NAME = "<STR_LIT:name>" <EOL> D_FREQUENCY = "<STR_LIT>" <EOL> D_IMAGE = "<STR_LIT:image>" <EOL> D_HOW_TO_ACCUMULATE = "<STR_LIT>" <EOL> D_IMAGE_COUNT = "<STR_LIT>" <EOL> D_VSQUARED = "<STR_LIT>" <EOL> D_VSUM = "<STR_LIT>" <EOL> D_POWER_IMAGE = "<STR_LIT>" <EOL> D_POWER_MASK = "<STR_LIT>" <EOL> D_STACK_NUMBER = "<STR_LIT>" <EOL> D_BRIGHT_MAX = "<STR_LIT>" <EOL> D_BRIGHT_MIN = "<STR_LIT>" <EOL> D_NORM0 = "<STR_LIT>" <EOL> def save_state ( self , d ) : <EOL> '''<STR_LIT>''' <EOL> d [ self . D_NAME ] = self . __name <EOL> d [ self . D_FREQUENCY ] = self . frequency <EOL> d [ self . D_IMAGE ] = self . __image <EOL> d [ self . D_HOW_TO_ACCUMULATE ] = self . __how_to_accumulate <EOL> d [ self . D_IMAGE_COUNT ] = self . __image_count <EOL> d [ self . D_VSQUARED ] = self . __vsquared <EOL> d [ self . D_VSUM ] = self . __vsum <EOL> d [ self . D_POWER_IMAGE ] = self . __power_image <EOL> d [ self . D_POWER_MASK ] = self . __power_mask <EOL> d [ self . D_STACK_NUMBER ] = self . __stack_number <EOL> d [ self . D_BRIGHT_MIN ] = self . __bright_min <EOL> d [ self . D_BRIGHT_MAX ] = self . __bright_max <EOL> d [ self . D_NORM0 ] = self . __norm0 <EOL> @ staticmethod <EOL> def restore_from_state ( d ) : <EOL> '''<STR_LIT>''' <EOL> name = d [ ImageProvider . D_NAME ] <EOL> frequency = d [ ImageProvider . D_FREQUENCY ] <EOL> how_to_accumulate = d [ ImageProvider . D_HOW_TO_ACCUMULATE ] <EOL> image_provider = ImageProvider ( name , how_to_accumulate , frequency ) <EOL> image_provider . __image = d [ ImageProvider . D_IMAGE ] <EOL> image_provider . __image_count = d [ ImageProvider . D_IMAGE_COUNT ] <EOL> image_provider . __vsquared = d [ ImageProvider . D_VSQUARED ] <EOL> image_provider . __vsum = d [ ImageProvider . D_VSUM ] <EOL> image_provider . __power_image = d [ ImageProvider . D_POWER_IMAGE ] <EOL> image_provider . __power_mask = d [ ImageProvider . D_POWER_MASK ] <EOL> image_provider . __stack_number = d [ ImageProvider . D_STACK_NUMBER ] <EOL> image_provider . __bright_min = d [ ImageProvider . D_BRIGHT_MIN ] <EOL> image_provider . __bright_max = d [ ImageProvider . D_BRIGHT_MAX ] <EOL> image_provider . __norm0 = d [ ImageProvider . D_NORM0 ] <EOL> return image_provider <EOL> def reset ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . __image_count = None <EOL> self . __image = None <EOL> self . __cached_image = None <EOL> self . __vsquared = None <EOL> self . __vsum = None <EOL> self . __power_image = None <EOL> self . __power_mask = None <EOL> self . __stack_number = <NUM_LIT:0> <EOL> self . __bright_max = None <EOL> self . __bright_min = None <EOL> @ property <EOL> def has_image ( self ) : <EOL> return self . __image_count is not None <EOL> @ property <EOL> def count ( self ) : <EOL> return self . __image_count <EOL> def set_image ( self , image ) : <EOL> self . __cached_image = None <EOL> if image . has_mask : <EOL> self . __image_count = image . mask . astype ( int ) <EOL> else : <EOL> self . __image_count = np . ones ( image . pixel_data . shape [ : <NUM_LIT:2> ] , int ) <EOL> if self . __how_to_accumulate == P_VARIANCE : <EOL> self . __vsum = image . pixel_data . copy ( ) <EOL> self . __vsum [ ~ image . mask ] = <NUM_LIT:0> <EOL> self . __image_count = image . mask . astype ( int ) <EOL> self . __vsquared = self . __vsum . astype ( np . float64 ) ** <NUM_LIT> <EOL> return <EOL> if self . __how_to_accumulate == P_POWER : <EOL> self . __vsum = image . pixel_data . copy ( ) <EOL> self . __vsum [ ~ image . mask ] = <NUM_LIT:0> <EOL> self . __image_count = image . mask . astype ( int ) <EOL> self . __power_mask = self . __image_count . astype ( np . complex128 ) . copy ( ) <EOL> self . __power_image = image . pixel_data . astype ( np . complex128 ) . copy ( ) <EOL> self . __stack_number = <NUM_LIT:1> <EOL> return <EOL> if self . __how_to_accumulate == P_BRIGHTFIELD : <EOL> self . __bright_max = image . pixel_data . copy ( ) <EOL> self . __bright_min = image . pixel_data . copy ( ) <EOL> self . __norm0 = np . mean ( image . pixel_data ) <EOL> return <EOL> if self . __how_to_accumulate == P_MASK : <EOL> self . __image = image . mask <EOL> return <EOL> self . __image = image . pixel_data . copy ( ) <EOL> if image . has_mask : <EOL> nan_value = <NUM_LIT:1> if self . __how_to_accumulate == P_MINIMUM else <NUM_LIT:0> <EOL> self . __image [ ~ image . mask ] = nan_value <EOL> def accumulate_image ( self , image ) : <EOL> self . __cached_image = None <EOL> if image . has_mask : <EOL> self . __image_count += image . mask . astype ( int ) <EOL> else : <EOL> self . __image_count += <NUM_LIT:1> <EOL> if self . __how_to_accumulate in [ P_AVERAGE , P_SUM ] : <EOL> if image . has_mask : <EOL> self . __image [ image . mask ] += image . pixel_data [ image . mask ] <EOL> else : <EOL> self . __image += image . pixel_data <EOL> elif self . __how_to_accumulate == P_MAXIMUM : <EOL> if image . has_mask : <EOL> self . __image [ image . mask ] = np . maximum ( self . __image [ image . mask ] , <EOL> image . pixel_data [ image . mask ] ) <EOL> else : <EOL> self . __image = np . maximum ( image . pixel_data , self . __image ) <EOL> elif self . __how_to_accumulate == P_MINIMUM : <EOL> if image . has_mask : <EOL> self . __image [ image . mask ] = np . minimum ( self . __image [ image . mask ] , <EOL> image . pixel_data [ image . mask ] ) <EOL> else : <EOL> self . __image = np . minimum ( image . pixel_data , self . __image ) <EOL> elif self . __how_to_accumulate == P_VARIANCE : <EOL> mask = image . mask <EOL> self . __vsum [ mask ] += image . pixel_data [ mask ] <EOL> self . __vsquared [ mask ] += image . pixel_data [ mask ] . astype ( np . float64 ) ** <NUM_LIT:2> <EOL> elif self . __how_to_accumulate == P_POWER : <EOL> multiplier = np . exp ( <NUM_LIT> * np . pi * float ( self . __stack_number ) / <EOL> self . frequency ) <EOL> self . __stack_number += <NUM_LIT:1> <EOL> mask = image . mask <EOL> self . __vsum [ mask ] += image . pixel_data [ mask ] <EOL> self . __power_image [ mask ] += multiplier * image . pixel_data [ mask ] <EOL> self . __power_mask [ mask ] += multiplier <EOL> elif self . __how_to_accumulate == P_BRIGHTFIELD : <EOL> mask = image . mask <EOL> norm = np . mean ( image . pixel_data ) <EOL> pixel_data = image . pixel_data * self . __norm0 / norm <EOL> max_mask = ( ( self . __bright_max < pixel_data ) & mask ) <EOL> min_mask = ( ( self . __bright_min > pixel_data ) & mask ) <EOL> self . __bright_min [ min_mask ] = pixel_data [ min_mask ] <EOL> self . __bright_max [ max_mask ] = pixel_data [ max_mask ] <EOL> self . __bright_min [ max_mask ] = self . __bright_max [ max_mask ] <EOL> elif self . __how_to_accumulate == P_MASK : <EOL> self . __image = self . __image & image . mask <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % <EOL> self . __how_to_accumulate ) <EOL> def provide_image ( self , image_set ) : <EOL> image_count = self . __image_count <EOL> mask_2d = image_count > <NUM_LIT:0> <EOL> if self . __how_to_accumulate == P_VARIANCE : <EOL> ndim_image = self . __vsquared <EOL> elif self . __how_to_accumulate == P_POWER : <EOL> ndim_image = self . __power_image <EOL> elif self . __how_to_accumulate == P_BRIGHTFIELD : <EOL> ndim_image = self . __bright_max <EOL> else : <EOL> ndim_image = self . __image <EOL> if ndim_image . ndim == <NUM_LIT:3> : <EOL> image_count = np . dstack ( [ image_count ] * ndim_image . shape [ <NUM_LIT:2> ] ) <EOL> mask = image_count > <NUM_LIT:0> <EOL> if self . __cached_image is not None : <EOL> return self . __cached_image <EOL> if self . __how_to_accumulate == P_AVERAGE : <EOL> cached_image = self . __image / image_count <EOL> elif self . __how_to_accumulate == P_VARIANCE : <EOL> cached_image = np . zeros ( self . __vsquared . shape , np . float32 ) <EOL> cached_image [ mask ] = self . __vsquared [ mask ] / image_count [ mask ] <EOL> cached_image [ mask ] -= self . __vsum [ mask ] ** <NUM_LIT:2> / ( image_count [ mask ] ** <NUM_LIT:2> ) <EOL> elif self . __how_to_accumulate == P_POWER : <EOL> cached_image = np . zeros ( image_count . shape , np . complex128 ) <EOL> cached_image [ mask ] = self . __power_image [ mask ] <EOL> cached_image [ mask ] -= ( self . __vsum [ mask ] * self . __power_mask [ mask ] / <EOL> image_count [ mask ] ) <EOL> cached_image = ( cached_image * np . conj ( cached_image ) ) . real . astype ( np . float32 ) <EOL> elif self . __how_to_accumulate == P_BRIGHTFIELD : <EOL> cached_image = np . zeros ( image_count . shape , np . float32 ) <EOL> cached_image [ mask ] = self . __bright_max [ mask ] - self . __bright_min [ mask ] <EOL> elif self . __how_to_accumulate == P_MINIMUM and np . any ( ~ mask ) : <EOL> cached_image = self . __image . copy ( ) <EOL> cached_image [ ~ mask ] = <NUM_LIT:0> <EOL> else : <EOL> cached_image = self . __image <EOL> cached_image [ ~ mask ] = <NUM_LIT:0> <EOL> if np . all ( mask ) or self . __how_to_accumulate == P_MASK : <EOL> self . __cached_image = cpi . Image ( cached_image ) <EOL> else : <EOL> self . __cached_image = cpi . Image ( cached_image , mask = mask_2d ) <EOL> return self . __cached_image <EOL> def get_name ( self ) : <EOL> return self . __name <EOL> def release_memory ( self ) : <EOL> '''<STR_LIT>''' <EOL> pass </s>
