MCPcopy
hub / github.com/microsoft/qlib / CsvWriter

Class CsvWriter

qlib/rl/utils/log.py:470–508  ·  view source on GitHub ↗

Dump all episode metrics to a ``result.csv``. This is not the correct implementation. It's only used for first iteration.

Source from the content-addressed store, hash-verified

468
469
470class CsvWriter(LogWriter):
471 """Dump all episode metrics to a ``result.csv``.
472
473 This is not the correct implementation. It's only used for first iteration.
474 """
475
476 SUPPORTED_TYPES = (float, str, pd.Timestamp)
477
478 all_records: List[Dict[str, Any]]
479
480 # FIXME: save & reload
481
482 def __init__(self, output_dir: Path, loglevel: int | LogLevel = LogLevel.PERIODIC) -> None:
483 super().__init__(loglevel)
484 self.output_dir = output_dir
485 self.output_dir.mkdir(exist_ok=True)
486
487 def clear(self) -> None:
488 super().clear()
489 self.all_records = []
490
491 def log_episode(self, length: int, rewards: List[float], contents: List[Dict[str, Any]]) -> None:
492 # FIXME Same as ConsoleLogger, needs a refactor to eliminate code-dup
493 episode_wise_contents: Dict[str, list] = defaultdict(list)
494
495 for step_contents in contents:
496 for name, value in step_contents.items():
497 if isinstance(value, self.SUPPORTED_TYPES):
498 episode_wise_contents[name].append(value)
499
500 logs: Dict[str, float] = {}
501 for name, values in episode_wise_contents.items():
502 logs[name] = self.aggregation(values, name) # type: ignore
503
504 self.all_records.append(logs)
505
506 def on_env_all_done(self) -> None:
507 # FIXME: this is temporary
508 pd.DataFrame.from_records(self.all_records).to_csv(self.output_dir / "result.csv", index=False)
509
510
511# The following are not implemented yet.

Callers 5

test_twap_strategyFunction · 0.90
test_cn_ppo_strategyFunction · 0.90
test_simple_env_loggerFunction · 0.90
train_and_testFunction · 0.90

Calls

no outgoing calls

Tested by 4

test_twap_strategyFunction · 0.72
test_cn_ppo_strategyFunction · 0.72
test_simple_env_loggerFunction · 0.72