classes

Some useful helper classes.

swem.utils.classes.ClfMetricTracker

Class for keeping track of the metrics corresponding to a single label in a classification task.

swem.utils.classes.KeyDependentDefaultdict

Defaultdict where the default factory can depend on the key.

ClfMetricTracker

class swem.utils.classes.ClfMetricTracker(name: str | int | None = None, tp: int = 0, fp: int = 0, fn: int = 0)

Class for keeping track of the metrics corresponding to a single label in a classification task.

Parameters
  • name (str | int | None) – The label this instance is keeping track of.

  • tp (int) – Start value for true positives. Defaults to 0.

  • fp (int) – Start-value for false positives. Defaults to 0.

  • fn (int) – Start-value for false negatives. Defaults to 0.

Return type

None

support

Number of instances for the label that were encountered.

recall

Current value for the recall metric.

precision

Current value for the precision metric.

f1_score

Current value for the f1_score metric.

Examples

>>> from swem.utils.classes import ClfMetricTracker
>>> tracker = ClfMetricTracker(name="Label_1", tp=5, fp=1, fn=3)
>>> tracker
ClfMetricTracker(name='Label_1', tp=5, fp=1, fn=3)
>>> tracker.support
8
>>> tracker.recall
0.625
>>> tracker.precision
0.8333333333333334
>>> tracker.f1_score
0.7142857142857143

KeyDependentDefaultdict

class swem.utils.classes.KeyDependentDefaultdict(*args, **kwargs)

Defaultdict where the default factory can depend on the key.

Parameters

default_factory (callable) – Default factory called when a missing key is encountered. The call will be default_factory(key) so the callable should take exactly one argument.

Examples

>>> d = KeyDependentDefaultdict(lambda key: {"name": key})
>>> d["a"]["b"] = 1
>>> d
{'a': {'name': 'a', 'b': 1}}