Skip to content

minrecord.manager

minrecord.manager

Contain a record manager implementation.

minrecord.manager.RecordManager

Implement a simple record manager.

This class proposes an approach to manage a group of records, but it is possible to use other approaches. If this class does not fit your needs, feel free to use another approach.

Parameters:

Name Type Description Default
records dict[str, BaseRecord[Any]] | None

The initial records to add to the manager.

None
Example
>>> from minrecord import RecordManager, MinScalarRecord
>>> manager = RecordManager()
>>> manager.add_record(MinScalarRecord("loss"))
>>> manager.get_record("loss")
MinScalarRecord(name=loss, max_size=10, size=0)
>>> manager.get_record("new_record")
Record(name=new_record, max_size=10, size=0)

minrecord.manager.RecordManager.add_record

add_record(
    record: BaseRecord[Any],
    key: str | None = None,
    exist_ok: bool = False,
) -> None

Add a record to the manager.

Parameters:

Name Type Description Default
record BaseRecord[Any]

The record to add to the manager.

required
key str | None

The key to store the record. If None, the name of the record is used.

None
exist_ok bool

If False, RuntimeError is raised if the key already exists. This parameter should be set to True to overwrite the record for this key.

False

Raises:

Type Description
RuntimeError

if a record is already registered for the key and exist_ok=False.

Example
>>> from minrecord import RecordManager, MinScalarRecord
>>> manager = RecordManager()
>>> manager.add_record(MinScalarRecord("loss"))
>>> manager
RecordManager(
  (loss): MinScalarRecord(name=loss, max_size=10, size=0)
)
>>> manager.add_record(MinScalarRecord("loss"), "my key")
>>> manager
RecordManager(
  (loss): MinScalarRecord(name=loss, max_size=10, size=0)
  (my key): MinScalarRecord(name=loss, max_size=10, size=0)
)

minrecord.manager.RecordManager.get_best_values

get_best_values(
    prefix: str = "", suffix: str = ""
) -> dict[str, Any]

Get the best value of each metric.

This method ignores the metrics with empty record and the non-comparable record.

Parameters:

Name Type Description Default
prefix str

The prefix used to create the dict of best values. The goal of this prefix is to generate a name which is different from the metric name to avoid confusion. By default, the returned dict uses the same name as the metric.

''
suffix str

The suffix used to create the dict of best values. The goal of this suffix is to generate a name which is different from the metric name to avoid confusion. By default, the returned dict uses the same name as the metric.

''

Returns:

Type Description
dict[str, Any]

The dict with the best value of each metric.

Example
>>> from minrecord import RecordManager, MaxScalarRecord
>>> manager = RecordManager()
>>> manager.add_record(MaxScalarRecord("accuracy"))
>>> manager.get_record("accuracy").add_value(42.0)
>>> manager.get_best_values()
{'accuracy': 42.0}
>>> manager.get_best_values(prefix="best/")
{'best/accuracy': 42.0}
>>> manager.get_best_values(suffix="/best")
{'accuracy/best': 42.0}

minrecord.manager.RecordManager.get_record

get_record(key: str) -> BaseRecord[Any]

Get the record associated to a key.

Parameters:

Name Type Description Default
key str

The key of the record to retrieve.

required

Returns:

Type Description
BaseRecord[Any]

The record if it exists, otherwise it returns an empty record. The created empty record is a Record object.

Example
>>> from minrecord import RecordManager, MinScalarRecord
>>> manager = RecordManager()
>>> manager.add_record(MinScalarRecord("loss"))
>>> manager.get_record("loss")
MinScalarRecord(name=loss, max_size=10, size=0)
>>> manager.get_record("new_record")
Record(name=new_record, max_size=10, size=0)

minrecord.manager.RecordManager.get_records

get_records() -> dict[str, BaseRecord[Any]]

Get all the records.

Returns:

Type Description
dict[str, BaseRecord[Any]]

The records with their associated keys.

Example
>>> from minrecord import RecordManager, MinScalarRecord
>>> manager = RecordManager()
>>> manager.add_record(MinScalarRecord("loss"))
>>> manager.get_records()
{'loss': MinScalarRecord(name=loss, max_size=10, size=0)}

minrecord.manager.RecordManager.has_record

has_record(key: str) -> bool

Indicate if the engine has a record for the given key.

Parameters:

Name Type Description Default
key str

The key of the record to check.

required

Returns:

Type Description
bool

True if the record exists, False otherwise

Example
>>> from minrecord import RecordManager, MinScalarRecord
>>> manager = RecordManager()
>>> manager.add_record(MinScalarRecord("loss"))
>>> manager.has_record("loss")
True
>>> manager.has_record("missing")
False

minrecord.manager.RecordManager.load_state_dict

load_state_dict(state_dict: dict[str, Any]) -> None

Load the state values from a dict.

Parameters:

Name Type Description Default
state_dict dict[str, Any]

A dict with the new state values.

required
Example
>>> from minrecord import RecordManager, Record
>>> manager = RecordManager()
>>> manager.add_record(Record("value"))
>>> manager.load_state_dict({"value": {"state": {"record": ((0, 1), (1, 0.5), (2, 0.25))}}})
>>> manager.get_record("value").get_last_value()
0.25

minrecord.manager.RecordManager.state_dict

state_dict() -> dict[str, Any]

Return a dictionary containing state values of all the records.

Returns:

Type Description
dict[str, Any]

The dictionary containing state values of all the records.

Example
>>> from minrecord import RecordManager
>>> manager = RecordManager()
>>> manager.state_dict()
{}