[DEPRECATED] DO NOT USE Load all metrics stored in a CSV file into the StatsManager instance. Will be removed in a future release after becoming a no-op. Arguments: csv_file: A file handle opened in read mode (e.g. open('...', 'r')) or a path as str. Re
(self, csv_file: StrPath | bytes | ty.TextIO)
| 219 | # TODO(v1.0): Create a replacement for a calculation cache that functions like load_from_csv |
| 220 | # did, but is better integrated with detectors for cached calculations instead of statistics. |
| 221 | def load_from_csv(self, csv_file: StrPath | bytes | ty.TextIO) -> int | None: |
| 222 | """[DEPRECATED] DO NOT USE |
| 223 | |
| 224 | Load all metrics stored in a CSV file into the StatsManager instance. Will be removed in a |
| 225 | future release after becoming a no-op. |
| 226 | |
| 227 | Arguments: |
| 228 | csv_file: A file handle opened in read mode (e.g. open('...', 'r')) or a path as str. |
| 229 | |
| 230 | Returns: |
| 231 | int or None: Number of frames/rows read from the CSV file, or None if the |
| 232 | input file was blank or could not be found. |
| 233 | |
| 234 | Raises: |
| 235 | StatsFileCorrupt: Stats file is corrupt and can't be loaded, or wrong file |
| 236 | was specified. |
| 237 | |
| 238 | :meta private: |
| 239 | """ |
| 240 | # TODO: Make this an error, then make load_from_csv() a no-op, and finally, remove it. |
| 241 | logger.warning("load_from_csv() is deprecated and will be removed in a future release.") |
| 242 | |
| 243 | # If we get a path instead of an open file handle, check that it exists, and if so, |
| 244 | # recursively call ourselves again but with file set instead of path. |
| 245 | if isinstance(csv_file, (str, bytes, os.PathLike)): |
| 246 | if os.path.exists(csv_file): |
| 247 | with open(csv_file) as file: |
| 248 | return self.load_from_csv(csv_file=file) |
| 249 | # Path doesn't exist. |
| 250 | return None |
| 251 | |
| 252 | # If we get here, file is a valid file handle in read-only text mode. |
| 253 | csv_reader = csv.reader(csv_file, lineterminator="\n") |
| 254 | num_cols = None |
| 255 | num_metrics = None |
| 256 | num_frames = None |
| 257 | # First Row: Frame Num, Timecode, [metrics...] |
| 258 | try: |
| 259 | row = next(csv_reader) |
| 260 | # Backwards compatibility for previous versions of statsfile |
| 261 | # which included an additional header row. |
| 262 | if not self.valid_header(row): |
| 263 | row = next(csv_reader) |
| 264 | except StopIteration: |
| 265 | # If the file is blank or we couldn't decode anything, assume the file was empty. |
| 266 | return None |
| 267 | if not self.valid_header(row): |
| 268 | raise StatsFileCorrupt() |
| 269 | num_cols = len(row) |
| 270 | num_metrics = num_cols - 2 |
| 271 | if not num_metrics > 0: |
| 272 | raise StatsFileCorrupt("No metrics defined in CSV file.") |
| 273 | loaded_metrics = list(row[2:]) |
| 274 | num_frames = 0 |
| 275 | for row in csv_reader: |
| 276 | metric_dict = {} |
| 277 | if not len(row) == num_cols: |
| 278 | raise StatsFileCorrupt("Wrong number of columns detected in stats file row.") |