@@ -84,13 +84,15 @@ def count_persons_in_json(file_path):
8484 return len (data .get ('people' , []))
8585
8686
87- def indices_of_first_last_non_nan_chunks (series , min_chunk_size = 10 ):
87+ def indices_of_first_last_non_nan_chunks (series , min_chunk_size = 10 , chunk_choice_method = 'largest' ):
8888 '''
8989 Find indices of the first and last chunks of at least min_chunk_size consecutive non-NaN values.
9090
9191 INPUT:
9292 - series: pandas Series to trim
9393 - min_chunk_size: minimum size of consecutive non-NaN values to consider (default: 5)
94+ - chunk_choice_method: 'largest' to return the largest chunk, 'all' to return all of them,
95+ 'first' to return only the first one, 'last' to return only the last one
9496
9597 OUTPUT:
9698 - tuple: (start_index, end_index) of the first and last valid chunks
@@ -116,9 +118,20 @@ def indices_of_first_last_non_nan_chunks(series, min_chunk_size=10):
116118 if not valid_runs :
117119 return (0 ,0 )
118120
119- # Get the start of the first valid run and the end of the last valid run
120- first_run_start = valid_runs [0 ][0 ] + series .index .start
121- last_run_end = valid_runs [- 1 ][1 ] + series .index .start
121+ if chunk_choice_method == 'largest' :
122+ # Choose the largest chunk
123+ valid_runs .sort (key = lambda x : x [1 ] - x [0 ], reverse = True )
124+ first_run_start , last_run_end = valid_runs [0 ]
125+ elif chunk_choice_method == 'all' :
126+ # Get the start of the first valid run and the end of the last valid run
127+ first_run_start = valid_runs [0 ][0 ]
128+ last_run_end = valid_runs [- 1 ][1 ]
129+ elif chunk_choice_method == 'first' :
130+ # Get the start of the first valid run and the end of that run
131+ first_run_start , last_run_end = valid_runs [0 ]
132+ elif chunk_choice_method == 'last' :
133+ # Get the start of the last valid run and the end of that run
134+ first_run_start , last_run_end = valid_runs [- 1 ]
122135
123136 # Return the trimmed series
124137 return first_run_start , last_run_end
@@ -876,7 +889,7 @@ def triangulate_all(config_dict):
876889 if nb_persons_to_detect == 0 :
877890 raise Exception ('No persons have been triangulated. Please check your calibration and your synchronization, or the triangulation parameters in Config.toml.' )
878891
879- logging .info ("Trimming " + ", " .join ([f"Participant { n } around frames { f_range_trimmed [n ]} " for n in range (nb_persons_to_detect )])+ "." )
892+ logging .info ("Trimming " + ", " .join ([f"Participant { n + 1 } around frames { f_range_trimmed [n ]} " for n in range (nb_persons_to_detect )])+ "." )
880893 Q_tot = [Q_tot [n ].loc [f_range_trimmed [n ][0 ]:f_range_trimmed [n ][1 ]- 1 ] for n in range (nb_persons_to_detect )]
881894 error_tot = [error_tot [n ].loc [f_range_trimmed [n ][0 ]:f_range_trimmed [n ][1 ]- 1 ] for n in range (nb_persons_to_detect )]
882895 nb_cams_excluded_tot = [nb_cams_excluded_tot [n ].loc [f_range_trimmed [n ][0 ]:f_range_trimmed [n ][1 ]- 1 ] for n in range (nb_persons_to_detect )]
0 commit comments