update data structures to allow for experiment creation

This commit is contained in:
Hunter 2024-07-29 17:25:40 -04:00
parent 42b3a9cf12
commit 8d9b371af4
4 changed files with 104 additions and 38 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__/

View File

@ -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

View File

@ -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)

View File

@ -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)