From 8d9b371af4de2b9ef837211332dd26c616871683 Mon Sep 17 00:00:00 2001 From: Hunter Date: Mon, 29 Jul 2024 17:25:40 -0400 Subject: [PATCH] update data structures to allow for experiment creation --- .gitignore | 2 ++ experiment.py | 49 +++++++++++++++++++++++++++++++++++++++-- moisture.py | 31 +++++++++++++++----------- shelling.py | 60 ++++++++++++++++++++++++++++++--------------------- 4 files changed, 104 insertions(+), 38 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..372c13e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ + diff --git a/experiment.py b/experiment.py index 7cb8712..fe867d3 100644 --- a/experiment.py +++ b/experiment.py @@ -30,11 +30,56 @@ class Experiment: experiment: str date: str - measurements: List[Measurement] = field(default_factory=list) - variables: List[Variable] = field(default_factory=list) + measurements: dict[str, Measurement] = field(default_factory=dict) + variables: dict[str, Variable] = field(default_factory=dict) def to_json(self): # Convert the data class to a dictionary, which can be easily converted to JSON. import json return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4) + + def from_json(self, json_str): + # Convert a JSON string to a data class. + import json + + data = json.loads(json_str) + self.__dict__.update(data) + + def add_measurement(self, measurement: Measurement): + # Add a measurement to the experiment. + if measurement.name in self.measurements: + raise ValueError( + f"Measurement {measurement.name} already exists in the experiment." + ) + self.measurements[measurement.name] = measurement + + def add_measurements(self, measurements: List[Measurement]): + # Add a list of measurements to the experiment. + for measurement in measurements: + self.add_measurement(measurement) + + def take_measurement(self, source: str, value: float): + # Add a measurement to the experiment. + if source not in self.measurements: + raise ValueError(f"Measurement {source} not found in the experiment.") + self.measurements[source].values.append(value) + + def add_variable(self, variable: Variable): + # Add a variable to the experiment. + if variable.name in self.variables: + raise ValueError( + f"Variable {variable.name} already exists in the experiment." + ) + self.variables[variable.name] = variable + + def add_variables(self, variables: List[Variable]): + # Add a list of variables to the experiment. + for variable in variables: + self.add_variable(variable) + + def set_variable(self, source: str, value: Any): + # Set a variable in the experiment. + if source not in self.variables: + raise ValueError(f"Variable {source} not found in the experiment.") + self.variables[source].value = value diff --git a/moisture.py b/moisture.py index d86dd81..b50bb93 100644 --- a/moisture.py +++ b/moisture.py @@ -5,15 +5,22 @@ from experiment import Experiment, Measurement, Variable @dataclass class MoistureExperiment(Experiment): - def __init__(self, date: str): - super().__init__( - experiment="Moisture Analysis", - date=date, - variables=[ - Variable("cold-soak-time", "hour", (5, 45)), - Variable("cold-soak-temp", "degF", (69, 72)), - Variable("hot-soak-time", "minute", (5, 20)), - Variable("hot-soak-temp", "degF", (180, 200)), - ], - measurements=[Measurement("moisture-content", "%", (0.06, 0.08))], - ) + def __init__( + self, + date: str, + cold_soak_time: float | None, + cold_soak_temp: float | None, + hot_soak_time: float | None, + hot_soak_temp: float | None, + ): + super().__init__(experiment="Moisture Analysis", date=date) + variables = [ + Variable("cold-soak-time", "hour", (5, 45), cold_soak_time), + Variable("cold-soak-temp", "degF", (69, 72), cold_soak_temp), + Variable("hot-soak-time", "minute", (5, 20), hot_soak_time), + Variable("hot-soak-temp", "degF", (180, 200), hot_soak_temp), + ] + self.add_variables(variables) + + measurement = Measurement("moisture-content", "%", (0.06, 0.08)) + self.add_measurement(measurement) diff --git a/shelling.py b/shelling.py index 0ffcd40..c512dea 100644 --- a/shelling.py +++ b/shelling.py @@ -5,27 +5,39 @@ from experiment import Experiment, Measurement, Variable @dataclass class ShellingExperiment(Experiment): - def __init__(self, date: str): - super().__init__( - experiment="Shelling Analysis", - date=date, - variables=[ - Variable("drum-rpm", "rpm", (30, 40)), - Variable("paddle-shaft-rpm", "rpm", (400, 800)), - Variable("ring-gap", "in"), - Variable("tilt-angle", "deg", (2, 5)), - Variable("feed-rate", "lb/hr", (300, 500), 500), - Variable("moisture-content", "%", (5, 9)), - Variable("pecan-variety", "", None, "desirable"), - ], - measurements=[ - Measurement("bin1-weight", "lb"), - Measurement("bin2-weight", "lb"), - Measurement("bin3-weight", "lb"), - Measurement("recirculated-weight", "lb"), - Measurement("final-discharge-weight", "lb"), - Measurement("bin1-half-yield", "%"), - Measurement("bin2-half-yield", "%"), - Measurement("bin3-half-yield", "%"), - ], - ) + def __init__( + self, + date: str, + drum_rpm: int, + paddle_shaft_rpm: int, + ring_gap: float, + tilt_angle: float, + moisture_content: float, + feed_rate: int = 500, + pecan_variety: str = "desirable", + ): + + super().__init__(experiment="Shelling Analysis", date=date) + + variables = [ + Variable("drum-rpm", "rpm", (30, 40), drum_rpm), + Variable("paddle-shaft-rpm", "rpm", (400, 800), paddle_shaft_rpm), + Variable("ring-gap", "in", value=ring_gap), + Variable("tilt-angle", "deg", (2, 5), tilt_angle), + Variable("feed-rate", "lb/hr", (300, 500), feed_rate), + Variable("moisture-content", "%", (5, 9), moisture_content), + Variable("pecan-variety", "", value=pecan_variety), + ] + self.add_variables(variables) + + measurements = [ + Measurement("bin1-weight", "lb"), + Measurement("bin2-weight", "lb"), + Measurement("bin3-weight", "lb"), + Measurement("recirculated-weight", "lb"), + Measurement("final-discharge-weight", "lb"), + Measurement("bin1-half-yield", "%"), + Measurement("bin2-half-yield", "%"), + Measurement("bin3-half-yield", "%"), + ] + self.add_measurements(measurements)