Skip to content

Commit 528b26e

Browse files
committed
Clean up: remove dead UUID code, fix method placement and spacing, deduplicate checkpoint key logic
1 parent 00e4f30 commit 528b26e

9 files changed

Lines changed: 49 additions & 50 deletions

File tree

src/pathsim/blocks/_block.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# IMPORTS ===============================================================================
1212

1313
import inspect
14-
from uuid import uuid4
1514
from functools import lru_cache
1615

1716
from ..utils.deprecation import deprecated
@@ -85,9 +84,6 @@ class definition for other blocks to be inherited.
8584

8685
def __init__(self):
8786

88-
#unique identifier for checkpointing and diagnostics
89-
self.id = uuid4().hex
90-
9187
#registers to hold input and output values
9288
self.inputs = Register(
9389
mapping=self.input_port_labels and self.input_port_labels.copy()

src/pathsim/blocks/fir.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def to_checkpoint(self, prefix, recordings=False):
120120
npz_data[f"{prefix}/fir_buffer"] = np.array(list(self._buffer))
121121
return json_data, npz_data
122122

123+
123124
def load_checkpoint(self, prefix, json_data, npz):
124125
"""Restore FIR state including input buffer."""
125126
super().load_checkpoint(prefix, json_data, npz)
@@ -131,4 +132,4 @@ def load_checkpoint(self, prefix, json_data, npz):
131132

132133
def __len__(self):
133134
"""This block has no direct passthrough"""
134-
return 0
135+
return 0

src/pathsim/blocks/kalman.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def to_checkpoint(self, prefix, recordings=False):
150150
npz_data[f"{prefix}/kf_P"] = self.P
151151
return json_data, npz_data
152152

153+
153154
def load_checkpoint(self, prefix, json_data, npz):
154155
"""Restore Kalman filter state estimate and covariance."""
155156
super().load_checkpoint(prefix, json_data, npz)

src/pathsim/blocks/noise.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,6 @@ class WhiteNoise(Block):
4444
random seed for reproducibility
4545
"""
4646

47-
def to_checkpoint(self, prefix, recordings=False):
48-
"""Serialize WhiteNoise state including current sample."""
49-
json_data, npz_data = super().to_checkpoint(prefix, recordings=recordings)
50-
json_data["_current_sample"] = float(self._current_sample)
51-
return json_data, npz_data
52-
53-
def load_checkpoint(self, prefix, json_data, npz):
54-
"""Restore WhiteNoise state including current sample."""
55-
super().load_checkpoint(prefix, json_data, npz)
56-
self._current_sample = json_data.get("_current_sample", 0.0)
57-
5847
input_port_labels = {}
5948
output_port_labels = {"out": 0}
6049

@@ -135,6 +124,19 @@ def update(self, t):
135124
pass
136125

137126

127+
def to_checkpoint(self, prefix, recordings=False):
128+
"""Serialize WhiteNoise state including current sample."""
129+
json_data, npz_data = super().to_checkpoint(prefix, recordings=recordings)
130+
json_data["_current_sample"] = float(self._current_sample)
131+
return json_data, npz_data
132+
133+
134+
def load_checkpoint(self, prefix, json_data, npz):
135+
"""Restore WhiteNoise state including current sample."""
136+
super().load_checkpoint(prefix, json_data, npz)
137+
self._current_sample = json_data.get("_current_sample", 0.0)
138+
139+
138140
class PinkNoise(Block):
139141
"""Pink noise (1/f noise) source using the Voss-McCartney algorithm.
140142
@@ -167,22 +169,6 @@ class PinkNoise(Block):
167169
random seed for reproducibility
168170
"""
169171

170-
def to_checkpoint(self, prefix, recordings=False):
171-
"""Serialize PinkNoise state including algorithm state."""
172-
json_data, npz_data = super().to_checkpoint(prefix, recordings=recordings)
173-
json_data["n_samples"] = self.n_samples
174-
json_data["_current_sample"] = float(self._current_sample)
175-
npz_data[f"{prefix}/octave_values"] = self.octave_values
176-
return json_data, npz_data
177-
178-
def load_checkpoint(self, prefix, json_data, npz):
179-
"""Restore PinkNoise state including algorithm state."""
180-
super().load_checkpoint(prefix, json_data, npz)
181-
self.n_samples = json_data.get("n_samples", 0)
182-
self._current_sample = json_data.get("_current_sample", 0.0)
183-
if f"{prefix}/octave_values" in npz:
184-
self.octave_values = npz[f"{prefix}/octave_values"]
185-
186172
input_port_labels = {}
187173
output_port_labels = {"out": 0}
188174

@@ -295,4 +281,22 @@ def sample(self, t, dt):
295281

296282

297283
def update(self, t):
298-
pass
284+
pass
285+
286+
287+
def to_checkpoint(self, prefix, recordings=False):
288+
"""Serialize PinkNoise state including algorithm state."""
289+
json_data, npz_data = super().to_checkpoint(prefix, recordings=recordings)
290+
json_data["n_samples"] = self.n_samples
291+
json_data["_current_sample"] = float(self._current_sample)
292+
npz_data[f"{prefix}/octave_values"] = self.octave_values
293+
return json_data, npz_data
294+
295+
296+
def load_checkpoint(self, prefix, json_data, npz):
297+
"""Restore PinkNoise state including algorithm state."""
298+
super().load_checkpoint(prefix, json_data, npz)
299+
self.n_samples = json_data.get("n_samples", 0)
300+
self._current_sample = json_data.get("_current_sample", 0.0)
301+
if f"{prefix}/octave_values" in npz:
302+
self.octave_values = npz[f"{prefix}/octave_values"]

src/pathsim/blocks/rng.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def to_checkpoint(self, prefix, recordings=False):
103103
json_data["_sample"] = float(self._sample)
104104
return json_data, npz_data
105105

106+
106107
def load_checkpoint(self, prefix, json_data, npz):
107108
"""Restore RNG state including current sample."""
108109
super().load_checkpoint(prefix, json_data, npz)

src/pathsim/blocks/switch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def to_checkpoint(self, prefix, recordings=False):
8787
json_data["switch_state"] = self.switch_state
8888
return json_data, npz_data
8989

90+
9091
def load_checkpoint(self, prefix, json_data, npz):
9192
super().load_checkpoint(prefix, json_data, npz)
9293
self.switch_state = json_data.get("switch_state", None)

src/pathsim/events/_event.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
import numpy as np
1313

14-
from uuid import uuid4
15-
1614
from .. _constants import EVT_TOLERANCE
1715

1816

@@ -66,9 +64,6 @@ def __init__(
6664
tolerance=EVT_TOLERANCE
6765
):
6866

69-
#unique identifier for checkpointing and diagnostics
70-
self.id = uuid4().hex
71-
7267
#event detection function
7368
self.func_evt = func_evt
7469

src/pathsim/subsystem.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,16 @@ def reset(self):
462462
block.reset()
463463

464464

465+
@staticmethod
466+
def _checkpoint_key(type_name, type_counts):
467+
"""Generate a deterministic checkpoint key from block/event type
468+
and occurrence index (e.g. 'Integrator_0', 'Scope_1').
469+
"""
470+
idx = type_counts.get(type_name, 0)
471+
type_counts[type_name] = idx + 1
472+
return f"{type_name}_{idx}"
473+
474+
465475
def to_checkpoint(self, prefix, recordings=False):
466476
"""Serialize subsystem state by recursively checkpointing internal blocks.
467477
@@ -494,10 +504,7 @@ def to_checkpoint(self, prefix, recordings=False):
494504
#checkpoint internal blocks by type + insertion order
495505
type_counts = {}
496506
for block in self.blocks:
497-
type_name = block.__class__.__name__
498-
idx = type_counts.get(type_name, 0)
499-
type_counts[type_name] = idx + 1
500-
key = f"{prefix}/{type_name}_{idx}"
507+
key = f"{prefix}/{self._checkpoint_key(block.__class__.__name__, type_counts)}"
501508
b_json, b_npz = block.to_checkpoint(key, recordings=recordings)
502509
b_json["_key"] = key
503510
json_data["blocks"].append(b_json)
@@ -545,10 +552,7 @@ def load_checkpoint(self, prefix, json_data, npz):
545552
block_data = {b["_key"]: b for b in json_data.get("blocks", [])}
546553
type_counts = {}
547554
for block in self.blocks:
548-
type_name = block.__class__.__name__
549-
idx = type_counts.get(type_name, 0)
550-
type_counts[type_name] = idx + 1
551-
key = f"{prefix}/{type_name}_{idx}"
555+
key = f"{prefix}/{self._checkpoint_key(block.__class__.__name__, type_counts)}"
552556
if key in block_data:
553557
block.load_checkpoint(key, block_data[key], npz)
554558

tests/pathsim/test_checkpoint.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,6 @@ def test_cross_instance_load(self):
277277
dt=0.01
278278
)
279279

280-
#UUIDs differ
281-
assert src1.id != src2.id
282-
assert integ1.id != integ2.id
283-
284280
sim2.load_checkpoint(path)
285281
assert sim2.time == saved_time
286282
assert np.allclose(integ2.state, saved_state)

0 commit comments

Comments
 (0)