@@ -440,26 +440,37 @@ def plot_motion_energy_hist(camera_dict, trials_df):
440440 'body' : '#035382' }
441441
442442 start_window , end_window = plt_window (trials_df ['stimOn_times' ])
443+ missing_data = []
443444 for cam in camera_dict .keys ():
444- try :
445- motion_energy = zscore (camera_dict [cam ]['motion_energy' ], nan_policy = 'omit' )
446- start_idx = insert_idx (camera_dict [cam ]['times' ], start_window )
447- end_idx = np .array (start_idx + int (WINDOW_LEN * SAMPLING [cam ]), dtype = 'int64' )
448- me_all = [motion_energy [start_idx [i ]:end_idx [i ]] for i in range (len (start_idx ))]
449- times = np .arange (len (me_all [0 ])) / SAMPLING [cam ] + WINDOW_LAG
450- me_mean = np .mean (me_all , axis = 0 )
451- me_std = np .std (me_all , axis = 0 ) / np .sqrt (len (me_all ))
452- plt .plot (times , me_mean , label = f'{ cam } cam' , color = colors [cam ], linewidth = 2 )
453- plt .fill_between (times , me_mean + me_std , me_mean - me_std , color = colors [cam ], alpha = 0.2 )
454- except AttributeError :
455- logger .warning (f"Cannot load motion energy AND times data for { cam } camera" )
445+ if (camera_dict [cam ]['motion_energy' ] is not None and len (camera_dict [cam ]['motion_energy' ]) > 0
446+ and camera_dict [cam ]['times' ] is not None and len (camera_dict [cam ]['times' ]) > 0 ):
447+ try :
448+ motion_energy = zscore (camera_dict [cam ]['motion_energy' ], nan_policy = 'omit' )
449+ start_idx = insert_idx (camera_dict [cam ]['times' ], start_window )
450+ end_idx = np .array (start_idx + int (WINDOW_LEN * SAMPLING [cam ]), dtype = 'int64' )
451+ me_all = [motion_energy [start_idx [i ]:end_idx [i ]] for i in range (len (start_idx ))]
452+ times = np .arange (len (me_all [0 ])) / SAMPLING [cam ] + WINDOW_LAG
453+ me_mean = np .mean (me_all , axis = 0 )
454+ me_std = np .std (me_all , axis = 0 ) / np .sqrt (len (me_all ))
455+ plt .plot (times , me_mean , label = f'{ cam } cam' , color = colors [cam ], linewidth = 2 )
456+ plt .fill_between (times , me_mean + me_std , me_mean - me_std , color = colors [cam ], alpha = 0.2 )
457+ except AttributeError :
458+ logger .warning (f"Cannot load motion energy and/or times data for { cam } camera" )
459+ missing_data .append (cam )
460+ else :
461+ logger .warning (f"Data missing or empty for motion energy and/or times data for { cam } camera" )
462+ missing_data .append (cam )
456463
457464 plt .xticks ([- 0.5 , 0 , 0.5 , 1 , 1.5 ])
458465 plt .ylabel ('z-scored motion energy [a.u.]' )
459466 plt .xlabel ('time [sec]' )
460467 plt .axvline (x = 0 , label = 'stimOn' , linestyle = '--' , c = 'k' )
461468 plt .legend (loc = 'lower right' )
462469 plt .title ('Motion Energy' )
470+ if len (missing_data ) > 0 :
471+ ax = plt .gca ()
472+ ax .text (.95 , .35 , f"Data incomplete for\n { ' and ' .join (missing_data )} camera" , color = 'r' , fontsize = 10 ,
473+ horizontalalignment = 'right' , verticalalignment = 'center' , transform = ax .transAxes )
463474 return plt .gca ()
464475
465476
0 commit comments