Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
43e216e
Making train and trainer modules, needs typing and docstrings
lotif Jul 25, 2025
9d6458a
Adding docstrings to clava_clusteting
lotif Jul 25, 2025
4e58fec
Adding docstring to the train.py module
lotif Jul 28, 2025
08b9a10
fixing test, adding docstrings for the trainer module
lotif Jul 28, 2025
c7b5040
WIP
lotif Jul 31, 2025
0d8c499
Moving clustering code to its own module
lotif Jul 31, 2025
63b93d8
WIP
lotif Jul 31, 2025
a9d24e8
Adding module docstring
lotif Jul 31, 2025
cf74942
Merge branch 'clustering-module' into sampler-module
lotif Jul 31, 2025
53e562e
Moving sampler classes to their own module and some more additional t…
lotif Jul 31, 2025
6e0a3e4
1st round of Fatemeh's CR
lotif Aug 5, 2025
932e1af
Fatemeh's CR
lotif Aug 6, 2025
fdfc6f7
Merge branch 'train-module' into clustering-module
lotif Aug 6, 2025
e093592
removing print
lotif Aug 6, 2025
b60a3f9
Merge branch 'clustering-module' into sampler-module
lotif Aug 6, 2025
5b06bee
WIP needs docstrings
lotif Aug 6, 2025
9cc7421
WIP David's and Fatemeh's CR
lotif Aug 6, 2025
152ee17
Merge branch 'train-module' into clustering-module
lotif Aug 6, 2025
ce871de
Merge branch 'clustering-module' into sampler-module
lotif Aug 6, 2025
1ee81b1
Merge branch 'sampler-module' into data-module
lotif Aug 6, 2025
7b8e187
WIP merging parent
lotif Aug 6, 2025
fcc00e9
David's CR: renamings
lotif Aug 7, 2025
f20f482
David's CR: remaining comments
lotif Aug 7, 2025
9408819
Merge branch 'main' into train-module
lotif Sep 8, 2025
3213772
Removing unused ignore
lotif Sep 8, 2025
f4599ac
Merge branch 'train-module' into clustering-module
lotif Sep 8, 2025
98af2c7
Merge branch 'main' into clustering-module
lotif Sep 8, 2025
fa7c27a
Merge branch 'clustering-module' into sampler-module
lotif Sep 8, 2025
9541cce
Little refactorings
lotif Sep 12, 2025
0534b41
Addressing some more comments by David
lotif Sep 15, 2025
ed0a2d3
Merge branch 'main' into clustering-module
lotif Sep 15, 2025
756eaaa
Merge branch 'clustering-module' into sampler-module
lotif Sep 15, 2025
96ab05d
Addressing comments by David
lotif Sep 16, 2025
4aaed32
Merge branch 'sampler-module' into data-module
lotif Sep 16, 2025
8b0f214
Merge branch 'sampler-module' into data-module
lotif Sep 16, 2025
d1a51db
Adding docstrings
lotif Sep 17, 2025
c6db8fe
Moiving a few more things around, adding docstrings to the model.py file
lotif Sep 17, 2025
6b7a7f6
David's CR
lotif Sep 17, 2025
09e5dc8
Rewording docstring, replacing logger
lotif Sep 17, 2025
abaabac
Merge branch 'clustering-module' into sampler-module
lotif Sep 17, 2025
247679f
Merge branch 'sampler-module' into data-module
lotif Sep 17, 2025
9860dbe
Merge branch 'main' into sampler-module
lotif Sep 17, 2025
677dc9e
Merge branch 'sampler-module' into data-module
lotif Sep 17, 2025
e39db1f
Moving data_loaders.py to the clavaddpm function and adding missing d…
lotif Sep 17, 2025
bc462dd
David's last comments
lotif Sep 19, 2025
f77a1d9
Merge branch 'sampler-module' into data-module
lotif Sep 19, 2025
c137b4c
Merge branch 'main' into data-module
lotif Sep 19, 2025
ff2d434
Removing data_loaders module from the docs
lotif Sep 19, 2025
03232f2
WIP needs to fix bug
lotif Sep 19, 2025
af15979
Finished implementation, needs tests
lotif Sep 19, 2025
226c97c
Changing from Any to float
lotif Sep 19, 2025
941803e
Adding tests for the key value logger
lotif Sep 22, 2025
b15059e
Addressing David's comments
lotif Sep 23, 2025
aeb8eef
WIP Partially addressed comments
lotif Sep 23, 2025
4545a9a
Fixing the rest of David's comments
lotif Sep 25, 2025
e76741d
Merge branch 'main' into data-module
lotif Sep 25, 2025
75823f8
Fixing more code comments
lotif Sep 25, 2025
938199e
Merge branch 'data-module' into remove-logger
lotif Sep 25, 2025
79eed5b
Merge branch 'main' into data-module
lotif Sep 25, 2025
eedad3b
Addressing one more comment.
lotif Sep 26, 2025
bbbfa8d
Merge branch 'data-module' into remove-logger
lotif Sep 26, 2025
99617b8
Merge branch 'main' into remove-logger
lotif Sep 26, 2025
abcc479
Removing logger from docs
lotif Sep 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
show_root_heading: true
show_root_full_path: true

## Logger Module

::: midst_toolkit.core.logger
options:
show_root_heading: true
show_root_full_path: true

## Diffusion Utils Module

::: midst_toolkit.models.clavaddpm.diffusion_utils
Expand Down
99 changes: 99 additions & 0 deletions src/midst_toolkit/common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import sys
from collections import defaultdict
from io import StringIO
from logging import LogRecord
from pathlib import Path
Expand Down Expand Up @@ -174,3 +175,101 @@ def redirect_output(output_buffer: StringIO) -> None:
sys.stdout = output_buffer
sys.stderr = output_buffer
console_handler.stream = sys.stdout


class KeyValueLogger:
"""Logger for key-value pairs of numerical metrics."""

def __init__(self, log_level: int = logging.DEBUG):
"""
Initialize the key-value logger.

Args:
log_level: The log level to use when dumping the key-value pairs. Should match
one of the logging levels in the logging python module. Defaults to logging.DEBUG.
"""
self.key_to_value: defaultdict[str, float] = defaultdict(float)
self.key_to_count: defaultdict[str, int] = defaultdict(int)
self.log_level = log_level

def save_entry(self, key: str, value: float) -> None:
"""
Save an entry to the key-value logger.

Args:
key: The key to save.
value: The value to save.
"""
self.key_to_value[key] = value

def save_entry_mean(self, key: str, value: float) -> None:
"""
Save an entry to the key-value logger with mean calculation.

Args:
key: The key to save.
value: The value to add to the mean calculation.
"""
old_value = self.key_to_value[key]
count = self.key_to_count[key]
self.key_to_value[key] = old_value * count / (count + 1) + value / (count + 1)
self.key_to_count[key] = count + 1

def dump(self) -> None:
"""
Dump the key-value pairs at the log level specified in the constructor.

Will clear the key-value pairs after dumping.
"""
# Create strings for printing
key_to_string = {}
for key, value in sorted(self.key_to_value.items()):
value_string = "%-8.3g" % value
key_to_string[self._truncate(key)] = self._truncate(value_string)

if len(key_to_string) == 0:
log(self.log_level, "WARNING: tried to write empty key-value dict")
return

# Find maximum number of characters for the keys and values
key_max_width = max(map(len, key_to_string.keys()))
value_max_width = max(map(len, key_to_string.values()))

# Write out the data
dashes = "-" * (key_max_width + value_max_width + 7)
log(self.log_level, dashes)
sorted_key_to_string = sorted(key_to_string.items(), key=lambda kv: kv[0].lower())
for k, v in sorted_key_to_string:
line = f"| {k}{self._add_spacing(k, key_max_width)} | {v}{self._add_spacing(v, value_max_width)} |"
log(self.log_level, line)
log(self.log_level, dashes)

# Clear the key-value pairs
self.key_to_value.clear()
self.key_to_count.clear()

def _truncate(self, s: str) -> str:
"""
Truncate a string to a maximum length of 30 characters.

Args:
s: The string to truncate.

Returns:
The string truncated to 30 characters.
"""
max_length = 30
return s[: max_length - 3] + "..." if len(s) > max_length else s

def _add_spacing(self, element: str, max_width: int) -> str:
"""
Add spacing to an element to make it the same length as the maximum width.

Args:
element: The element to add spacing to.
max_width: The maximum width to add spacing to.

Returns:
The element with spacing added to make it the same length as the maximum width.
"""
return " " * (max_width - len(element))
1 change: 0 additions & 1 deletion src/midst_toolkit/core/__init__.py

This file was deleted.

Loading