<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> from . utils import multi_tau_lags <EOL> from . roi import extract_label_indices <EOL> from collections import namedtuple <EOL> import numpy as np <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> def _one_time_process ( buf , G , past_intensity_norm , future_intensity_norm , <EOL> label_array , num_bufs , num_pixels , img_per_level , <EOL> level , buf_no , norm , lev_len ) : <EOL> """<STR_LIT>""" <EOL> img_per_level [ level ] += <NUM_LIT:1> <EOL> i_min = num_bufs // <NUM_LIT:2> if level else <NUM_LIT:0> <EOL> for i in range ( i_min , min ( img_per_level [ level ] , num_bufs ) ) : <EOL> t_index = level * num_bufs / <NUM_LIT:2> + i <EOL> delay_no = ( buf_no - i ) % num_bufs <EOL> past_img = buf [ level , delay_no ] <EOL> future_img = buf [ level , buf_no ] <EOL> ind = int ( t_index - lev_len [ : level ] . sum ( ) ) <EOL> normalize = img_per_level [ level ] - i - norm [ level + <NUM_LIT:1> ] [ ind ] <EOL> if np . isnan ( past_img ) . any ( ) or np . isnan ( future_img ) . any ( ) : <EOL> norm [ level + <NUM_LIT:1> ] [ ind ] += <NUM_LIT:1> <EOL> else : <EOL> for w , arr in zip ( [ past_img * future_img , past_img , future_img ] , <EOL> [ G , past_intensity_norm , future_intensity_norm ] ) : <EOL> binned = np . bincount ( label_array , weights = w ) [ <NUM_LIT:1> : ] <EOL> arr [ t_index ] += ( ( binned / num_pixels - <EOL> arr [ t_index ] ) / normalize ) <EOL> return None <EOL> results = namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> _internal_state = namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> _two_time_internal_state = namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def _init_state_one_time ( num_levels , num_bufs , labels ) : <EOL> """<STR_LIT>""" <EOL> ( label_array , pixel_list , num_rois , num_pixels , lag_steps , buf , <EOL> img_per_level , track_level , cur , norm , <EOL> lev_len ) = _validate_and_transform_inputs ( num_bufs , num_levels , labels ) <EOL> G = np . zeros ( ( ( num_levels + <NUM_LIT:1> ) * num_bufs / <NUM_LIT:2> , num_rois ) , <EOL> dtype = np . float64 ) <EOL> past_intensity = np . zeros_like ( G ) <EOL> future_intensity = np . zeros_like ( G ) <EOL> return _internal_state ( <EOL> buf , <EOL> G , <EOL> past_intensity , <EOL> future_intensity , <EOL> img_per_level , <EOL> label_array , <EOL> track_level , <EOL> cur , <EOL> pixel_list , <EOL> num_pixels , <EOL> lag_steps , <EOL> norm , <EOL> lev_len , <EOL> ) <EOL> def lazy_one_time ( image_iterable , num_levels , num_bufs , labels , <EOL> internal_state = None ) : <EOL> """<STR_LIT>""" <EOL> if internal_state is None : <EOL> internal_state = _init_state_one_time ( num_levels , num_bufs , labels ) <EOL> s = internal_state <EOL> for image in image_iterable : <EOL> level = <NUM_LIT:0> <EOL> s . cur [ <NUM_LIT:0> ] = ( <NUM_LIT:1> + s . cur [ <NUM_LIT:0> ] ) % num_bufs <EOL> s . buf [ <NUM_LIT:0> , s . cur [ <NUM_LIT:0> ] - <NUM_LIT:1> ] = np . ravel ( image ) [ s . pixel_list ] <EOL> buf_no = s . cur [ <NUM_LIT:0> ] - <NUM_LIT:1> <EOL> _one_time_process ( s . buf , s . G , s . past_intensity , s . future_intensity , <EOL> s . label_array , num_bufs , s . num_pixels , <EOL> s . img_per_level , level , buf_no , s . norm , s . lev_len ) <EOL> processing = num_levels > <NUM_LIT:1> <EOL> level = <NUM_LIT:1> <EOL> while processing : <EOL> if not s . track_level [ level ] : <EOL> s . track_level [ level ] = True <EOL> processing = False <EOL> else : <EOL> prev = ( <NUM_LIT:1> + ( s . cur [ level - <NUM_LIT:1> ] - <NUM_LIT:2> ) % num_bufs ) <EOL> s . cur [ level ] = ( <EOL> <NUM_LIT:1> + s . cur [ level ] % num_bufs ) <EOL> s . buf [ level , s . cur [ level ] - <NUM_LIT:1> ] = ( ( <EOL> s . buf [ level - <NUM_LIT:1> , prev - <NUM_LIT:1> ] + <EOL> s . buf [ level - <NUM_LIT:1> , s . cur [ level - <NUM_LIT:1> ] - <NUM_LIT:1> ] ) / <NUM_LIT:2> ) <EOL> s . track_level [ level ] = False <EOL> buf_no = s . cur [ level ] - <NUM_LIT:1> <EOL> _one_time_process ( s . buf , s . G , s . past_intensity , <EOL> s . future_intensity , s . label_array , num_bufs , <EOL> s . num_pixels , s . img_per_level , level , buf_no , <EOL> s . norm , s . lev_len ) <EOL> level += <NUM_LIT:1> <EOL> processing = level < num_levels <EOL> if len ( np . where ( s . past_intensity == <NUM_LIT:0> ) [ <NUM_LIT:0> ] ) != <NUM_LIT:0> : <EOL> g_max = np . where ( s . past_intensity == <NUM_LIT:0> ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> g_max = s . past_intensity . shape [ <NUM_LIT:0> ] <EOL> g2 = ( s . G [ : g_max ] / ( s . past_intensity [ : g_max ] * <EOL> s . future_intensity [ : g_max ] ) ) <EOL> yield results ( g2 , s . lag_steps [ : g_max ] , s ) <EOL> def multi_tau_auto_corr ( num_levels , num_bufs , labels , images ) : <EOL> """<STR_LIT>""" <EOL> gen = lazy_one_time ( images , num_levels , num_bufs , labels ) <EOL> for result in gen : <EOL> pass <EOL> return result . g2 , result . lag_steps <EOL> def auto_corr_scat_factor ( lags , beta , relaxation_rate , baseline = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return beta * np . exp ( - <NUM_LIT:2> * relaxation_rate * lags ) + baseline <EOL> def two_time_corr ( labels , images , num_frames , num_bufs , num_levels = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> gen = lazy_two_time ( labels , images , num_frames , num_bufs , num_levels ) <EOL> for result in gen : <EOL> pass <EOL> return two_time_state_to_results ( result ) <EOL> def lazy_two_time ( labels , images , num_frames , num_bufs , num_levels = <NUM_LIT:1> , <EOL> two_time_internal_state = None ) : <EOL> """<STR_LIT>""" <EOL> if two_time_internal_state is None : <EOL> two_time_internal_state = _init_state_two_time ( num_levels , num_bufs , <EOL> labels , num_frames ) <EOL> s = two_time_internal_state <EOL> for img in images : <EOL> s . cur [ <NUM_LIT:0> ] = ( <NUM_LIT:1> + s . cur [ <NUM_LIT:0> ] ) % num_bufs <EOL> s . count_level [ <NUM_LIT:0> ] = <NUM_LIT:1> + s . count_level [ <NUM_LIT:0> ] <EOL> s = s . _replace ( current_img_time = ( s . current_img_time + <NUM_LIT:1> ) ) <EOL> s . buf [ <NUM_LIT:0> , s . cur [ <NUM_LIT:0> ] - <NUM_LIT:1> ] = ( np . ravel ( img ) ) [ s . pixel_list ] <EOL> _two_time_process ( s . buf , s . g2 , s . label_array , num_bufs , <EOL> s . num_pixels , s . img_per_level , s . lag_steps , <EOL> s . current_img_time , <EOL> level = <NUM_LIT:0> , buf_no = s . cur [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> s . time_ind [ <NUM_LIT:0> ] . append ( s . current_img_time ) <EOL> processing = num_levels > <NUM_LIT:1> <EOL> level = <NUM_LIT:1> <EOL> while processing : <EOL> if not s . track_level [ level ] : <EOL> s . track_level [ level ] = <NUM_LIT:1> <EOL> processing = False <EOL> else : <EOL> prev = <NUM_LIT:1> + ( s . cur [ level - <NUM_LIT:1> ] - <NUM_LIT:2> ) % num_bufs <EOL> s . cur [ level ] = <NUM_LIT:1> + s . cur [ level ] % num_bufs <EOL> s . count_level [ level ] = <NUM_LIT:1> + s . count_level [ level ] <EOL> s . buf [ level , s . cur [ level ] - <NUM_LIT:1> ] = ( s . buf [ level - <NUM_LIT:1> , prev - <NUM_LIT:1> ] + <EOL> s . buf [ level - <NUM_LIT:1> , <EOL> s . cur [ level - <NUM_LIT:1> ] - <NUM_LIT:1> ] ) / <NUM_LIT:2> <EOL> t1_idx = ( s . count_level [ level ] - <NUM_LIT:1> ) * <NUM_LIT:2> <EOL> current_img_time = ( ( s . time_ind [ level - <NUM_LIT:1> ] ) [ t1_idx ] + <EOL> ( s . time_ind [ level - <NUM_LIT:1> ] ) [ t1_idx + <NUM_LIT:1> ] ) / <NUM_LIT> <EOL> s . time_ind [ level ] . append ( current_img_time ) <EOL> s . track_level [ level ] = <NUM_LIT:0> <EOL> _two_time_process ( s . buf , s . g2 , s . label_array , num_bufs , <EOL> s . num_pixels , s . img_per_level , s . lag_steps , <EOL> current_img_time , <EOL> level = level , buf_no = s . cur [ level ] - <NUM_LIT:1> ) <EOL> level += <NUM_LIT:1> <EOL> processing = level < num_levels <EOL> yield s <EOL> def two_time_state_to_results ( state ) : <EOL> """<STR_LIT>""" <EOL> for q in range ( np . max ( state . label_array ) ) : <EOL> x0 = ( state . g2 ) [ q , : , : ] <EOL> ( state . g2 ) [ q , : , : ] = ( np . tril ( x0 ) + np . tril ( x0 ) . T - <EOL> np . diag ( np . diag ( x0 ) ) ) <EOL> return results ( state . g2 , state . lag_steps , state ) <EOL> def _two_time_process ( buf , g2 , label_array , num_bufs , num_pixels , <EOL> img_per_level , lag_steps , current_img_time , <EOL> level , buf_no ) : <EOL> """<STR_LIT>""" <EOL> img_per_level [ level ] += <NUM_LIT:1> <EOL> if level == <NUM_LIT:0> : <EOL> i_min = <NUM_LIT:0> <EOL> else : <EOL> i_min = num_bufs // <NUM_LIT:2> <EOL> for i in range ( i_min , min ( img_per_level [ level ] , num_bufs ) ) : <EOL> t_index = level * num_bufs / <NUM_LIT:2> + i <EOL> delay_no = ( buf_no - i ) % num_bufs <EOL> past_img = buf [ level , delay_no ] <EOL> future_img = buf [ level , buf_no ] <EOL> tmp_binned = ( np . bincount ( label_array , <EOL> weights = past_img * future_img ) [ <NUM_LIT:1> : ] ) <EOL> pi_binned = ( np . bincount ( label_array , <EOL> weights = past_img ) [ <NUM_LIT:1> : ] ) <EOL> fi_binned = ( np . bincount ( label_array , <EOL> weights = future_img ) [ <NUM_LIT:1> : ] ) <EOL> tind1 = ( current_img_time - <NUM_LIT:1> ) <EOL> tind2 = ( current_img_time - lag_steps [ t_index ] - <NUM_LIT:1> ) <EOL> if not isinstance ( current_img_time , int ) : <EOL> nshift = <NUM_LIT:2> ** ( level - <NUM_LIT:1> ) <EOL> for i in range ( - nshift + <NUM_LIT:1> , nshift + <NUM_LIT:1> ) : <EOL> g2 [ : , int ( tind1 + i ) , <EOL> int ( tind2 + i ) ] = ( tmp_binned / ( pi_binned * <EOL> fi_binned ) ) * num_pixels <EOL> else : <EOL> g2 [ : , tind1 , tind2 ] = tmp_binned / ( pi_binned * fi_binned ) * num_pixels <EOL> def _init_state_two_time ( num_levels , num_bufs , labels , num_frames ) : <EOL> """<STR_LIT>""" <EOL> ( label_array , pixel_list , num_rois , num_pixels , lag_steps , <EOL> buf , img_per_level , track_level , cur , norm , <EOL> lev_len ) = _validate_and_transform_inputs ( num_bufs , num_levels , labels ) <EOL> count_level = np . zeros ( num_levels , dtype = np . int64 ) <EOL> current_img_time = <NUM_LIT:0> <EOL> time_ind = { key : [ ] for key in range ( num_levels ) } <EOL> g2 = np . zeros ( ( num_rois , num_frames , num_frames ) , dtype = np . float64 ) <EOL> return _two_time_internal_state ( <EOL> buf , <EOL> img_per_level , <EOL> label_array , <EOL> track_level , <EOL> cur , <EOL> pixel_list , <EOL> num_pixels , <EOL> lag_steps , <EOL> g2 , <EOL> count_level , <EOL> current_img_time , <EOL> time_ind , <EOL> norm , <EOL> lev_len , <EOL> ) <EOL> def _validate_and_transform_inputs ( num_bufs , num_levels , labels ) : <EOL> """<STR_LIT>""" <EOL> if num_bufs % <NUM_LIT:2> != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % num_bufs ) <EOL> label_array , pixel_list = extract_label_indices ( labels ) <EOL> label_mapping = { label : n + <NUM_LIT:1> <EOL> for n , label in enumerate ( np . unique ( label_array ) ) } <EOL> for label , n in label_mapping . items ( ) : <EOL> label_array [ label_array == label ] = n <EOL> num_rois = len ( label_mapping ) <EOL> num_pixels = np . bincount ( label_array ) [ <NUM_LIT:1> : ] <EOL> tot_channels , lag_steps , dict_lag = multi_tau_lags ( num_levels , num_bufs ) <EOL> norm = { key : [ <NUM_LIT:0> ] * len ( dict_lag [ key ] ) for key in ( dict_lag . keys ( ) ) } <EOL> lev_len = np . array ( [ len ( dict_lag [ i ] ) for i in ( dict_lag . keys ( ) ) ] ) <EOL> buf = np . zeros ( ( num_levels , num_bufs , len ( pixel_list ) ) , <EOL> dtype = np . float64 ) <EOL> img_per_level = np . zeros ( num_levels , dtype = np . int64 ) <EOL> track_level = np . zeros ( num_levels , dtype = bool ) <EOL> cur = np . ones ( num_levels , dtype = np . int64 ) <EOL> return ( label_array , pixel_list , num_rois , num_pixels , <EOL> lag_steps , buf , img_per_level , track_level , cur , <EOL> norm , lev_len ) <EOL> def one_time_from_two_time ( two_time_corr ) : <EOL> """<STR_LIT>""" <EOL> one_time_corr = np . zeros ( ( two_time_corr . shape [ <NUM_LIT:0> ] , two_time_corr . shape [ <NUM_LIT:2> ] ) ) <EOL> for g in two_time_corr : <EOL> for j in range ( two_time_corr . shape [ <NUM_LIT:2> ] ) : <EOL> one_time_corr [ : , j ] = np . trace ( g , offset = j ) / two_time_corr . shape [ <NUM_LIT:2> ] <EOL> return one_time_corr </s>
