<s> """<STR_LIT>""" <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from cStringIO import StringIO <EOL> import json <EOL> import javabridge <EOL> import numpy as np <EOL> import threading <EOL> import uuid <EOL> import zmq <EOL> if not hasattr ( zmq , "<STR_LIT>" ) : <EOL> def ZmqFrame ( data = "<STR_LIT>" ) : <EOL> return data <EOL> zmq . Frame = ZmqFrame <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . measurements as cpmeas <EOL> import cellprofiler . cpimage as cpi <EOL> import cellprofiler . objects as cpo <EOL> import cellprofiler . pipeline as cpp <EOL> import cellprofiler . settings as cps <EOL> import cellprofiler . workspace as cpw <EOL> CONNECT_REQ_1 = "<STR_LIT>" <EOL> CONNECT_REPLY_1 = "<STR_LIT>" <EOL> PIPELINE_INFO_REQ_1 = "<STR_LIT>" <EOL> PIPELINE_INFO_REPLY_1 = "<STR_LIT>" <EOL> RUN_REQ_1 = "<STR_LIT>" <EOL> RUN_GROUP_REQ_1 = "<STR_LIT>" <EOL> RUN_REPLY_1 = "<STR_LIT>" <EOL> CELLPROFILER_EXCEPTION_1 = "<STR_LIT>" <EOL> PIPELINE_EXCEPTION_1 = "<STR_LIT>" <EOL> CLEAN_PIPELINE_REQ_1 = "<STR_LIT>" <EOL> CLEAN_PIPELINE_REPLY_1 = "<STR_LIT>" <EOL> class KnimeBridgeServer ( threading . Thread ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , context , address , notify_address , notify_stop , ** kwargs ) : <EOL> super ( KnimeBridgeServer , self ) . __init__ ( ** kwargs ) <EOL> self . setDaemon ( True ) <EOL> self . setName ( "<STR_LIT>" ) <EOL> self . address = address <EOL> self . context = context <EOL> self . notify_addr = notify_address <EOL> self . stop_msg = notify_stop <EOL> self . dispatch = { <EOL> CONNECT_REQ_1 : self . connect , <EOL> CLEAN_PIPELINE_REQ_1 : self . clean_pipeline , <EOL> PIPELINE_INFO_REQ_1 : self . pipeline_info , <EOL> RUN_REQ_1 : self . run_request , <EOL> RUN_GROUP_REQ_1 : self . run_group_request <EOL> } <EOL> self . start_addr = "<STR_LIT>" + uuid . uuid4 ( ) . hex <EOL> self . start_socket = context . socket ( zmq . PAIR ) <EOL> self . start_socket . bind ( self . start_addr ) <EOL> def __enter__ ( self ) : <EOL> if self . address is not None : <EOL> self . start ( ) <EOL> def __exit__ ( self , exc_type , value , tb ) : <EOL> if self . address is not None : <EOL> self . join ( ) <EOL> def start ( self ) : <EOL> super ( KnimeBridgeServer , self ) . start ( ) <EOL> self . start_socket . recv ( ) <EOL> self . start_socket . close ( ) <EOL> def run ( self ) : <EOL> javabridge . attach ( ) <EOL> try : <EOL> self . socket = self . context . socket ( zmq . REP ) <EOL> self . socket . bind ( self . address ) <EOL> logger . info ( "<STR_LIT>" % self . address ) <EOL> poller = zmq . Poller ( ) <EOL> poller . register ( self . socket , flags = zmq . POLLIN ) <EOL> if self . notify_addr is not None : <EOL> self . notify_socket = self . context . socket ( zmq . SUB ) <EOL> self . notify_socket . setsockopt ( zmq . SUBSCRIBE , "<STR_LIT>" ) <EOL> self . notify_socket . connect ( self . notify_addr ) <EOL> poller . register ( self . notify_socket , flags = zmq . POLLIN ) <EOL> else : <EOL> self . notify_socket = None <EOL> start_socket = self . context . socket ( zmq . PAIR ) <EOL> start_socket . connect ( self . start_addr ) <EOL> start_socket . send ( "<STR_LIT:OK>" ) <EOL> start_socket . close ( ) <EOL> try : <EOL> while True : <EOL> for socket , event in poller . poll ( <NUM_LIT:1> ) : <EOL> if socket == self . notify_socket : <EOL> msg = self . notify_socket . recv_string ( ) <EOL> if msg == self . stop_msg : <EOL> break <EOL> elif socket == self . socket : <EOL> msg = self . socket . recv_multipart ( copy = False ) <EOL> session_id_frame = msg . pop ( <NUM_LIT:0> ) <EOL> session_id = str ( session_id_frame . bytes ) <EOL> wrapper = msg . pop ( <NUM_LIT:0> ) <EOL> message_type = msg . pop ( <NUM_LIT:0> ) . bytes <EOL> if message_type not in self . dispatch : <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , <EOL> "<STR_LIT>" % message_type ) <EOL> else : <EOL> try : <EOL> self . dispatch [ message_type ] ( <EOL> session_id , message_type , msg ) <EOL> except Exception , e : <EOL> logger . warn ( e . message , exc_info = <NUM_LIT:1> ) <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , e . message ) <EOL> else : <EOL> continue <EOL> break <EOL> finally : <EOL> if self . notify_socket : <EOL> self . notify_socket . close ( ) <EOL> self . socket . close ( ) <EOL> finally : <EOL> javabridge . detach ( ) <EOL> def connect ( self , session_id , message_type , message ) : <EOL> '''<STR_LIT>''' <EOL> self . socket . send_multipart ( <EOL> [ zmq . Frame ( session_id ) , <EOL> zmq . Frame ( ) , <EOL> zmq . Frame ( CONNECT_REPLY_1 ) ] ) <EOL> def pipeline_info ( self , session_id , message_type , message ) : <EOL> '''<STR_LIT>''' <EOL> logger . info ( "<STR_LIT>" ) <EOL> pipeline_txt = message . pop ( <NUM_LIT:0> ) . bytes <EOL> pipeline = cpp . Pipeline ( ) <EOL> try : <EOL> pipeline . loadtxt ( StringIO ( pipeline_txt ) ) <EOL> except Exception , e : <EOL> logger . warning ( <EOL> "<STR_LIT>" , <EOL> exc_info = <NUM_LIT:1> ) <EOL> self . raise_pipeline_exception ( session_id , str ( e ) ) <EOL> return <EOL> input_modules , other_modules = self . split_pipeline ( pipeline ) <EOL> channels = self . find_channels ( input_modules ) <EOL> type_names , measurements = self . find_measurements ( <EOL> other_modules , pipeline ) <EOL> body = json . dumps ( [ channels , type_names , measurements ] ) <EOL> msg_out = [ <EOL> zmq . Frame ( session_id ) , <EOL> zmq . Frame ( ) , <EOL> zmq . Frame ( PIPELINE_INFO_REPLY_1 ) , <EOL> zmq . Frame ( body ) ] <EOL> self . socket . send_multipart ( msg_out ) <EOL> def clean_pipeline ( self , session_id , message_type , message ) : <EOL> '''<STR_LIT>''' <EOL> logger . info ( "<STR_LIT>" ) <EOL> pipeline_txt = message . pop ( <NUM_LIT:0> ) . bytes <EOL> module_names = json . loads ( message . pop ( <NUM_LIT:0> ) . bytes ) <EOL> pipeline = cpp . Pipeline ( ) <EOL> try : <EOL> pipeline . loadtxt ( StringIO ( pipeline_txt ) ) <EOL> except Exception , e : <EOL> logger . warning ( <EOL> "<STR_LIT>" , <EOL> exc_info = <NUM_LIT:1> ) <EOL> self . raise_pipeline_exception ( session_id , str ( e ) ) <EOL> return <EOL> to_remove = [ ] <EOL> for module in pipeline . modules ( exclude_disabled = False ) : <EOL> if module . module_name in module_names : <EOL> to_remove . insert ( <NUM_LIT:0> , module ) <EOL> for module in to_remove : <EOL> pipeline . remove_module ( module . module_num ) <EOL> pipeline_fd = StringIO ( ) <EOL> pipeline . savetxt ( pipeline_fd , save_image_plane_details = False ) <EOL> msg_out = [ <EOL> zmq . Frame ( session_id ) , <EOL> zmq . Frame ( ) , <EOL> zmq . Frame ( CLEAN_PIPELINE_REPLY_1 ) , <EOL> zmq . Frame ( pipeline_fd . getvalue ( ) ) ] <EOL> self . socket . send_multipart ( msg_out ) <EOL> def run_request ( self , session_id , message_type , message ) : <EOL> '''<STR_LIT>''' <EOL> pipeline , m , object_set = self . prepare_run ( message , session_id ) <EOL> if pipeline is None : <EOL> return <EOL> m [ cpmeas . IMAGE , cpmeas . GROUP_NUMBER ] = <NUM_LIT:1> <EOL> m [ cpmeas . IMAGE , cpmeas . GROUP_INDEX ] = <NUM_LIT:1> <EOL> input_modules , other_modules = self . split_pipeline ( pipeline ) <EOL> for module in other_modules : <EOL> workspace = cpw . Workspace ( pipeline , module , m , None , m , None ) <EOL> module . prepare_run ( workspace ) <EOL> for module in other_modules : <EOL> workspace = cpw . Workspace ( <EOL> pipeline , module , m , object_set , m , None ) <EOL> try : <EOL> logger . info ( <EOL> "<STR_LIT>" % <EOL> ( module . module_num , module . module_name ) ) <EOL> pipeline . run_module ( module , workspace ) <EOL> if workspace . disposition in ( cpw . DISPOSITION_SKIP , cpw . DISPOSITION_CANCEL ) : <EOL> break <EOL> except Exception , e : <EOL> msg = "<STR_LIT>" % ( <EOL> module . module_name , e . message ) <EOL> logger . warning ( msg , exc_info = <NUM_LIT:1> ) <EOL> self . raise_cellprofiler_exception ( session_id , msg ) <EOL> return <EOL> type_names , feature_dict = self . find_measurements ( <EOL> other_modules , pipeline ) <EOL> double_features = [ ] <EOL> double_data = [ ] <EOL> float_features = [ ] <EOL> float_data = [ ] <EOL> int_features = [ ] <EOL> int_data = [ ] <EOL> string_features = [ ] <EOL> string_data = [ ] <EOL> metadata = [ double_features , float_features , <EOL> int_features , string_features ] <EOL> no_data = ( ) <EOL> for object_name , features in feature_dict . items ( ) : <EOL> df = [ ] <EOL> double_features . append ( ( object_name , df ) ) <EOL> ff = [ ] <EOL> float_features . append ( ( object_name , ff ) ) <EOL> intf = [ ] <EOL> int_features . append ( ( object_name , intf ) ) <EOL> sf = [ ] <EOL> string_features . append ( ( object_name , sf ) ) <EOL> for feature , data_type in features : <EOL> if not m . has_feature ( object_name , feature ) : <EOL> data = no_data <EOL> else : <EOL> data = np . atleast_1d ( m [ object_name , feature ] ) <EOL> if type_names [ data_type ] == '<STR_LIT>' : <EOL> df . append ( ( feature , len ( data ) ) ) <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> double_data . append ( data . astype ( "<STR_LIT>" ) ) <EOL> elif type_names [ data_type ] == '<STR_LIT>' : <EOL> ff . append ( ( feature , len ( data ) ) ) <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> float_data . append ( data . astype ( '<STR_LIT>' ) ) <EOL> elif type_names [ data_type ] == '<STR_LIT>' : <EOL> intf . append ( ( feature , len ( data ) ) ) <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> int_data . append ( data . astype ( '<STR_LIT>' ) ) <EOL> elif type_names [ data_type ] == '<STR_LIT>' : <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> sf . append ( ( feature , <NUM_LIT:0> ) ) <EOL> else : <EOL> s = data [ <NUM_LIT:0> ] <EOL> if isinstance ( s , unicode ) : <EOL> s = s . encode ( "<STR_LIT:utf-8>" ) <EOL> else : <EOL> s = str ( s ) <EOL> string_data . append ( np . frombuffer ( s , np . uint8 ) ) <EOL> data = np . hstack ( [ <EOL> np . frombuffer ( np . hstack ( ditem ) . data , np . uint8 ) <EOL> for ditem in double_data , float_data , int_data , string_data <EOL> if len ( ditem ) > <NUM_LIT:0> ] ) <EOL> self . socket . send_multipart ( <EOL> [ zmq . Frame ( session_id ) , <EOL> zmq . Frame ( ) , <EOL> zmq . Frame ( RUN_REPLY_1 ) , <EOL> zmq . Frame ( json . dumps ( metadata ) ) , <EOL> zmq . Frame ( bytes ( data . data ) ) ] ) <EOL> def run_group_request ( self , session_id , message_type , message ) : <EOL> '''<STR_LIT>''' <EOL> pipeline = cpp . Pipeline ( ) <EOL> m = cpmeas . Measurements ( ) <EOL> image_group = m . hdf5_dict . hdf5_file . create_group ( "<STR_LIT>" ) <EOL> if len ( message ) < <NUM_LIT:2> : <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , "<STR_LIT>" ) <EOL> return <EOL> pipeline_txt = message . pop ( <NUM_LIT:0> ) . bytes <EOL> image_metadata = message . pop ( <NUM_LIT:0> ) . bytes <EOL> n_image_sets = None <EOL> try : <EOL> image_metadata = json . loads ( image_metadata ) <EOL> channel_names = [ ] <EOL> for channel_name , channel_metadata in image_metadata : <EOL> channel_names . append ( channel_name ) <EOL> if len ( message ) < <NUM_LIT:1> : <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , <EOL> "<STR_LIT>" % channel_name ) <EOL> return None , None , None <EOL> pixel_data = self . decode_image ( <EOL> channel_metadata , message . pop ( <NUM_LIT:0> ) . bytes , <EOL> grouping_allowed = True ) <EOL> if pixel_data . ndim < <NUM_LIT:3> : <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , <EOL> "<STR_LIT>" ) <EOL> return <EOL> if n_image_sets is None : <EOL> n_image_sets = pixel_data . shape [ <NUM_LIT:0> ] <EOL> elif n_image_sets != pixel_data . shape [ <NUM_LIT:0> ] : <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , <EOL> "<STR_LIT>" ) <EOL> return <EOL> image_group . create_dataset ( channel_name , <EOL> data = pixel_data ) <EOL> except Exception , e : <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , e . message ) <EOL> return None , None , None <EOL> try : <EOL> pipeline . loadtxt ( StringIO ( pipeline_txt ) ) <EOL> except Exception , e : <EOL> logger . warning ( <EOL> "<STR_LIT>" , <EOL> exc_info = <NUM_LIT:1> ) <EOL> self . raise_pipeline_exception ( session_id , str ( e ) ) <EOL> return <EOL> image_numbers = np . arange ( <NUM_LIT:1> , n_image_sets + <NUM_LIT:1> ) <EOL> for image_number in image_numbers : <EOL> m [ cpmeas . IMAGE , cpmeas . GROUP_NUMBER , image_number ] = <NUM_LIT:1> <EOL> m [ cpmeas . IMAGE , cpmeas . GROUP_INDEX , image_number ] = image_number <EOL> input_modules , other_modules = self . split_pipeline ( pipeline ) <EOL> workspace = cpw . Workspace ( <EOL> pipeline , None , m , None , m , None ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> for module in other_modules : <EOL> module . prepare_group ( <EOL> workspace , <EOL> dict ( [ ( "<STR_LIT>" , i ) for i in image_numbers ] ) , <EOL> image_numbers ) <EOL> for image_index in range ( n_image_sets ) : <EOL> object_set = cpo . ObjectSet ( ) <EOL> m . next_image_set ( image_index + <NUM_LIT:1> ) <EOL> for channel_name in channel_names : <EOL> dataset = image_group [ channel_name ] <EOL> pixel_data = dataset [ image_index ] <EOL> m . add ( channel_name , cpi . Image ( pixel_data ) ) <EOL> for module in other_modules : <EOL> workspace = cpw . Workspace ( <EOL> pipeline , module , m , object_set , m , None ) <EOL> try : <EOL> logger . info ( <EOL> "<STR_LIT>" % <EOL> ( module . module_num , module . module_name ) ) <EOL> pipeline . run_module ( module , workspace ) <EOL> if workspace . disposition in ( cpw . DISPOSITION_SKIP , cpw . DISPOSITION_CANCEL ) : <EOL> break <EOL> except Exception , e : <EOL> msg = "<STR_LIT>" % ( <EOL> module . module_name , e . message ) <EOL> logger . warning ( msg , exc_info = <NUM_LIT:1> ) <EOL> self . raise_cellprofiler_exception ( session_id , msg ) <EOL> return <EOL> else : <EOL> continue <EOL> if workspace . disposition == cpw . DISPOSITION_CANCEL : <EOL> break <EOL> for module in other_modules : <EOL> module . post_group ( <EOL> workspace , <EOL> dict ( [ ( "<STR_LIT>" , i ) for i in image_numbers ] ) ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> type_names , feature_dict = self . find_measurements ( <EOL> other_modules , pipeline ) <EOL> double_features = [ ] <EOL> double_data = [ ] <EOL> float_features = [ ] <EOL> float_data = [ ] <EOL> int_features = [ ] <EOL> int_data = [ ] <EOL> string_features = [ ] <EOL> string_data = [ ] <EOL> metadata = [ double_features , float_features , <EOL> int_features , string_features ] <EOL> for object_name , features in feature_dict . items ( ) : <EOL> df = [ ] <EOL> double_features . append ( ( object_name , df ) ) <EOL> ff = [ ] <EOL> float_features . append ( ( object_name , ff ) ) <EOL> intf = [ ] <EOL> int_features . append ( ( object_name , intf ) ) <EOL> sf = [ ] <EOL> string_features . append ( ( object_name , sf ) ) <EOL> if object_name == cpmeas . IMAGE : <EOL> object_counts = [ ] * n_image_sets <EOL> else : <EOL> object_numbers = m [ object_name , cpmeas . OBJECT_NUMBER , <EOL> image_numbers ] <EOL> object_counts = [ len ( x ) for x in object_numbers ] <EOL> for feature , data_type in features : <EOL> if data_type == '<STR_LIT>' : <EOL> continue <EOL> if not m . has_feature ( object_name , feature ) : <EOL> data = np . zeros ( np . sum ( object_counts ) ) <EOL> else : <EOL> data = m [ object_name , feature , image_numbers ] <EOL> temp = [ ] <EOL> for i , ( di , count ) in enumerate ( zip ( data , object_counts ) ) : <EOL> if count == <NUM_LIT:0> : <EOL> continue <EOL> di = np . atleast_1d ( di ) <EOL> if len ( di ) > count : <EOL> di = di [ : count ] <EOL> elif len ( di ) == count : <EOL> temp . append ( di ) <EOL> else : <EOL> temp += [ di + np . zeros ( len ( di ) - count ) ] <EOL> if len ( temp ) > <NUM_LIT:0> : <EOL> data = np . hstack ( temp ) <EOL> if type_names [ data_type ] == '<STR_LIT>' : <EOL> df . append ( ( feature , len ( data ) ) ) <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> double_data . append ( data . astype ( "<STR_LIT>" ) ) <EOL> elif type_names [ data_type ] == '<STR_LIT>' : <EOL> ff . append ( ( feature , len ( data ) ) ) <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> float_data . append ( data . astype ( '<STR_LIT>' ) ) <EOL> elif type_names [ data_type ] == '<STR_LIT>' : <EOL> intf . append ( ( feature , len ( data ) ) ) <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> int_data . append ( data . astype ( '<STR_LIT>' ) ) <EOL> data = np . hstack ( [ <EOL> np . frombuffer ( np . ascontiguousarray ( np . hstack ( ditem ) ) . data , np . uint8 ) <EOL> for ditem in double_data , float_data , int_data <EOL> if len ( ditem ) > <NUM_LIT:0> ] ) <EOL> data = np . ascontiguousarray ( data ) <EOL> self . socket . send_multipart ( <EOL> [ zmq . Frame ( session_id ) , <EOL> zmq . Frame ( ) , <EOL> zmq . Frame ( RUN_REPLY_1 ) , <EOL> zmq . Frame ( json . dumps ( metadata ) ) , <EOL> zmq . Frame ( data ) ] ) <EOL> def prepare_run ( self , message , session_id , grouping_allowed = False ) : <EOL> '''<STR_LIT>''' <EOL> pipeline = cpp . Pipeline ( ) <EOL> m = cpmeas . Measurements ( ) <EOL> object_set = cpo . ObjectSet ( ) <EOL> if len ( message ) < <NUM_LIT:2> : <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , "<STR_LIT>" ) <EOL> return <EOL> pipeline_txt = message . pop ( <NUM_LIT:0> ) . bytes <EOL> image_metadata = message . pop ( <NUM_LIT:0> ) . bytes <EOL> try : <EOL> image_metadata = json . loads ( image_metadata ) <EOL> for channel_name , channel_metadata in image_metadata : <EOL> if len ( message ) < <NUM_LIT:1> : <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , <EOL> "<STR_LIT>" % channel_name ) <EOL> return None , None , None <EOL> pixel_data = self . decode_image ( <EOL> channel_metadata , message . pop ( <NUM_LIT:0> ) . bytes , <EOL> grouping_allowed = grouping_allowed ) <EOL> m . add ( channel_name , cpi . Image ( pixel_data ) ) <EOL> except Exception , e : <EOL> logger . warn ( "<STR_LIT>" , exc_info = <NUM_LIT:1> ) <EOL> self . raise_cellprofiler_exception ( <EOL> session_id , e . message ) <EOL> return None , None , None <EOL> try : <EOL> pipeline . loadtxt ( StringIO ( pipeline_txt ) ) <EOL> except Exception , e : <EOL> logger . warning ( <EOL> "<STR_LIT>" , <EOL> exc_info = <NUM_LIT:1> ) <EOL> self . raise_pipeline_exception ( session_id , str ( e ) ) <EOL> return None , None , None <EOL> return pipeline , m , object_set <EOL> def raise_pipeline_exception ( self , session_id , message ) : <EOL> if isinstance ( message , unicode ) : <EOL> message = message . encode ( "<STR_LIT:utf-8>" ) <EOL> else : <EOL> message = str ( message ) <EOL> self . socket . send_multipart ( <EOL> [ zmq . Frame ( session_id ) , <EOL> zmq . Frame ( ) , <EOL> zmq . Frame ( PIPELINE_EXCEPTION_1 ) , <EOL> zmq . Frame ( message ) ] ) <EOL> def raise_cellprofiler_exception ( self , session_id , message ) : <EOL> if isinstance ( message , unicode ) : <EOL> message = message . encode ( "<STR_LIT:utf-8>" ) <EOL> else : <EOL> message = str ( message ) <EOL> self . socket . send_multipart ( <EOL> [ zmq . Frame ( session_id ) , <EOL> zmq . Frame ( ) , <EOL> zmq . Frame ( CELLPROFILER_EXCEPTION_1 ) , <EOL> zmq . Frame ( message ) ] ) <EOL> def split_pipeline ( self , pipeline ) : <EOL> '''<STR_LIT>''' <EOL> input_modules = [ ] <EOL> other_modules = [ ] <EOL> for module in pipeline . modules ( ) : <EOL> if module . is_load_module ( ) or module . is_input_module ( ) : <EOL> input_modules . append ( module ) <EOL> else : <EOL> other_modules . append ( module ) <EOL> return input_modules , other_modules <EOL> def find_channels ( self , input_modules ) : <EOL> '''<STR_LIT>''' <EOL> channels = [ ] <EOL> for module in input_modules : <EOL> for setting in module . visible_settings ( ) : <EOL> if isinstance ( setting , cps . ImageNameProvider ) : <EOL> channels . append ( setting . value ) <EOL> return channels <EOL> def find_measurements ( self , modules , pipeline ) : <EOL> '''<STR_LIT>''' <EOL> jtypes = [ "<STR_LIT>" ] <EOL> features = { } <EOL> for module in modules : <EOL> assert isinstance ( module , cpm . CPModule ) <EOL> for column in module . get_measurement_columns ( pipeline ) : <EOL> objects , name , dbtype = column [ : <NUM_LIT:3> ] <EOL> qualifiers = { } if len ( column ) < <NUM_LIT:4> else column [ <NUM_LIT:3> ] <EOL> if objects == cpmeas . EXPERIMENT and qualifiers . get ( cpmeas . MCA_AVAILABLE_POST_RUN , False ) == True : <EOL> continue <EOL> if dbtype == cpmeas . COLTYPE_FLOAT : <EOL> jtype = "<STR_LIT>" <EOL> elif dbtype == cpmeas . COLTYPE_INTEGER : <EOL> jtype = "<STR_LIT>" <EOL> elif dbtype . startswith ( cpmeas . COLTYPE_VARCHAR ) : <EOL> jtype = "<STR_LIT>" <EOL> else : <EOL> continue <EOL> if jtype in jtypes : <EOL> type_idx = jtypes . index ( jtype ) <EOL> else : <EOL> type_idx = len ( jtypes ) <EOL> jtypes . append ( jtype ) <EOL> if objects not in features : <EOL> ofeatures = features [ objects ] = { } <EOL> else : <EOL> ofeatures = features [ objects ] <EOL> if name not in ofeatures : <EOL> ofeatures [ name ] = type_idx <EOL> for key in features : <EOL> features [ key ] [ cpmeas . IMAGE_NUMBER ] = <NUM_LIT:0> <EOL> features_out = dict ( [ ( k , v . items ( ) ) for k , v in features . items ( ) ] ) <EOL> return jtypes , features_out <EOL> def decode_image ( self , channel_metadata , buf , grouping_allowed = False ) : <EOL> '''<STR_LIT>''' <EOL> pixel_data = np . frombuffer ( buf , "<STR_LIT>" ) <EOL> strides_out = [ None ] * len ( channel_metadata ) <EOL> dimensions_out = [ None ] * len ( channel_metadata ) <EOL> grouping = False <EOL> for axis_name , dim , stride in channel_metadata : <EOL> if axis_name . lower ( ) == "<STR_LIT:y>" : <EOL> if strides_out [ <NUM_LIT:0> ] is not None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> strides_out [ <NUM_LIT:0> ] = stride * <NUM_LIT:8> <EOL> dimensions_out [ <NUM_LIT:0> ] = dim <EOL> elif axis_name . lower ( ) == "<STR_LIT:x>" : <EOL> if strides_out [ <NUM_LIT:1> ] is not None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> strides_out [ <NUM_LIT:1> ] = stride * <NUM_LIT:8> <EOL> dimensions_out [ <NUM_LIT:1> ] = dim <EOL> elif axis_name . lower ( ) == "<STR_LIT>" : <EOL> if strides_out [ <NUM_LIT:2> ] is not None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> strides_out [ <NUM_LIT:2> ] = stride * <NUM_LIT:8> <EOL> dimensions_out [ <NUM_LIT:2> ] = dim <EOL> elif grouping == False and grouping_allowed : <EOL> grouping = True <EOL> strides_out [ - <NUM_LIT:1> ] = stride * <NUM_LIT:8> <EOL> dimensions_out [ - <NUM_LIT:1> ] = dim <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" + axis_name ) <EOL> if grouping : <EOL> strides_out [ <NUM_LIT:0> ] , strides_out [ <NUM_LIT:1> : ] = strides_out [ - <NUM_LIT:1> ] , strides_out [ : - <NUM_LIT:1> ] <EOL> dimensions_out [ <NUM_LIT:0> ] , dimensions_out [ <NUM_LIT:1> : ] = dimensions_out [ - <NUM_LIT:1> ] , dimensions_out [ : - <NUM_LIT:1> ] <EOL> pixel_data . shape = tuple ( dimensions_out ) <EOL> pixel_data . strides = tuple ( strides_out ) <EOL> return pixel_data <EOL> __all__ = [ KnimeBridgeServer ] <EOL> __all__ += [ CONNECT_REQ_1 , CONNECT_REPLY_1 , <EOL> PIPELINE_INFO_REQ_1 , PIPELINE_INFO_REPLY_1 , <EOL> RUN_REQ_1 , RUN_GROUP_REQ_1 , RUN_REPLY_1 , <EOL> PIPELINE_EXCEPTION_1 , CELLPROFILER_EXCEPTION_1 ] </s>
