Skip to content

Commit

Permalink
hash
Browse files Browse the repository at this point in the history
  • Loading branch information
shervin86 committed Jun 19, 2024
1 parent b218862 commit c68883c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
13 changes: 12 additions & 1 deletion libpyvinyl/BaseCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,25 @@ def __hash__(self) -> int:
The hash of the parameters can be accessed by hash(calc.parameters).
This logic allows to decouple the changes in the calculator from changes in the values of the parameters
The calculator_base_dir is removed from the hash calculation. It can then safely contain the ash in the path
The hash calculation ignores:
- calculator_base_dir
- input
- output
It can then safely contain the hash in the path input/output/base_dir paths
:return: hash of the calculator
"""
a = dill.copy(self)
a.parameters = None
a.__calc_hash = None
a.__params_hash = None
a.__calculator_base_dir = None
a.input = None
a.__output_keys = None
a.__output_data_types = None
a.__output_filenames = None

return int.from_bytes(hashlib.sha256(dill.dumps(a)).digest(), "big")

@property
Expand Down
27 changes: 24 additions & 3 deletions libpyvinyl/Instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def __init__(
self.__name: str = ""
self.__instrument_base_dir: str = ""
self.__parameters = InstrumentParameters()

self.__calculator_hashes = None
self.__parameter_hashes = None
self.name = name

self.__calculators: Dict[str, BaseCalculator] = {}
Expand Down Expand Up @@ -156,8 +157,28 @@ def run(self) -> None:
Run the entire simulation,
i.e. all the calculators in the order they have been provided
"""
for calculator in self.calculators.values():
calculator.backengine()
for calc in self.calculators.values():
if calc.is_calc_changed:
# need to recompile and rerun
print(f"Calculator {calc.name} changed")
calc._update_hash()
calc.calculator_base_dir = "-".join(
[calc.name, "_".join([str(calc.calc_hash), str(calc.params_hash)])]
)

calc.backengine()
elif calc.is_paramset_changed:
print(f"Parameters of calculator {calc.name} changed")
# need to re-run without recompiling
calc._update_hash()
calc.calculator_base_dir = "-".join(
[
calc.name,
"_".join([str(calc.__calc_hash), str(calc.__params_hash)]),
]
)

calc.backengine()

@property
def output(self) -> DataCollection:
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/test_BaseCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import shutil
from typing import Union
from pathlib import Path
import hashlib
import dill

from libpyvinyl.BaseCalculator import BaseCalculator
from libpyvinyl.BaseData import BaseData, DataCollection
Expand Down Expand Up @@ -310,6 +312,35 @@ def test_calculator_output_set_inconsistent(self):
"test", input1, output_keys=["result"], output_data_types=[]
)

def test_calculator_hash(self):
calculator = self.__default_calculator()
assert calculator.calc_hash == None
assert calculator.params_hash == None

assert calculator.is_calc_changed() == True
assert calculator.is_paramset_changed() == True
assert hash(calculator) == 1511185683683508563

calculator._update_hash()
assert calculator.is_calc_changed() == False
assert calculator.is_paramset_changed() == False
assert hash(calculator) == 1511185683683508563

calculator.calculator_base_dir = "/tmp/"
assert calculator.is_calc_changed() == False
assert calculator.is_paramset_changed() == False

calculator.input = None
assert calculator.input == None
assert calculator.is_calc_changed() == False
assert calculator.is_paramset_changed() == False

calculator.output_keys = ""
calculator.output_data_types = []
assert hashlib.sha256(dill.dumps(calculator.output).digest()) == hashlib.sha256(
dill.dumps(DataCollection()).digest()
)


if __name__ == "__main__":
unittest.main()

0 comments on commit c68883c

Please sign in to comment.