|
17 | 17 | _misc_metric_name_to_func, |
18 | 18 | _possible_pc_metric_names, |
19 | 19 | qm_compute_name_to_column_names, |
| 20 | + column_name_to_column_dtype, |
20 | 21 | ) |
21 | 22 | from .misc_metrics import _default_params as misc_metrics_params |
22 | 23 | from .pca_metrics import _default_params as pca_metrics_params |
@@ -140,13 +141,20 @@ def _merge_extension_data( |
140 | 141 | all_unit_ids = new_sorting_analyzer.unit_ids |
141 | 142 | not_new_ids = all_unit_ids[~np.isin(all_unit_ids, new_unit_ids)] |
142 | 143 |
|
| 144 | + # this creates a new metrics dictionary, but the dtype for everything will be |
| 145 | + # object. So we will need to fix this later after computing metrics |
143 | 146 | metrics = pd.DataFrame(index=all_unit_ids, columns=old_metrics.columns) |
144 | | - |
145 | 147 | metrics.loc[not_new_ids, :] = old_metrics.loc[not_new_ids, :] |
146 | 148 | metrics.loc[new_unit_ids, :] = self._compute_metrics( |
147 | 149 | new_sorting_analyzer, new_unit_ids, verbose, metric_names, **job_kwargs |
148 | 150 | ) |
149 | 151 |
|
| 152 | + # we need to fix the dtypes after we compute everything because we have nans |
| 153 | + # we can iterate through the columns and convert them back to the dtype |
| 154 | + # of the original quality dataframe. |
| 155 | + for column in old_metrics.columns: |
| 156 | + metrics[column] = metrics[column].astype(old_metrics[column].dtype) |
| 157 | + |
150 | 158 | new_data = dict(metrics=metrics) |
151 | 159 | return new_data |
152 | 160 |
|
@@ -229,10 +237,20 @@ def _compute_metrics(self, sorting_analyzer, unit_ids=None, verbose=False, metri |
229 | 237 | # add NaN for empty units |
230 | 238 | if len(empty_unit_ids) > 0: |
231 | 239 | metrics.loc[empty_unit_ids] = np.nan |
| 240 | + # num_spikes is an int and should be 0 |
| 241 | + if "num_spikes" in metrics.columns: |
| 242 | + metrics.loc[empty_unit_ids, ["num_spikes"]] = 0 |
232 | 243 |
|
233 | 244 | # we use the convert_dtypes to convert the columns to the most appropriate dtype and avoid object columns |
234 | 245 | # (in case of NaN values) |
235 | 246 | metrics = metrics.convert_dtypes() |
| 247 | + |
| 248 | + # we do this because the convert_dtypes infers the wrong types sometimes. |
| 249 | + # the actual types for columns can be found in column_name_to_column_dtype dictionary. |
| 250 | + for column in metrics.columns: |
| 251 | + if column in column_name_to_column_dtype: |
| 252 | + metrics[column] = metrics[column].astype(column_name_to_column_dtype[column]) |
| 253 | + |
236 | 254 | return metrics |
237 | 255 |
|
238 | 256 | def _run(self, verbose=False, **job_kwargs): |
|
0 commit comments