Skip to content

Commit

Permalink
add core functions
Browse files Browse the repository at this point in the history
  • Loading branch information
samirdarouich committed Mar 5, 2024
1 parent 45b9b9a commit f4f90b6
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
Binary file modified FAIRFlowChemistry/.DS_Store
Binary file not shown.
23 changes: 23 additions & 0 deletions FAIRFlowChemistry/core/calibration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sdRDM
import numpy as np

from typing import List, Optional
from uuid import uuid4
Expand Down Expand Up @@ -66,3 +67,25 @@ class Calibration(
_commit: Optional[str] = PrivateAttr(
default="238a0547367fc736463730403ca8c1b7c46e9422"
)

def calibrate(self):
"""
Calibrate the regression model on seen data
"""

self.regression_coefficients = np.polynomial.polynomial.polyfit(
self.peak_areas.values, self.concentrations.values, self.degree
).tolist()

def predict(self, x: list) -> np.ndarray:
"""
Predict with regression model
Args:
x (1D list): New locations for which predictions should be made
Returns:
(1D numpy array): Predicted data at new locations
"""

return np.polynomial.Polynomial(self.regression_coefficients)(np.array(x))
95 changes: 94 additions & 1 deletion FAIRFlowChemistry/core/experiment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sdRDM
import yaml
import pandas as pd

from typing import List, Optional
from uuid import uuid4
Expand All @@ -13,10 +15,11 @@
from .component import Component
from .measurement import Measurement
from .data import Data
from .datatype import DataType
from .plantsetup import PlantSetup
from .metadata import Metadata
from .measurementtype import MeasurementType

from .quantity import Quantity

@forge_signature
class Experiment(
Expand Down Expand Up @@ -146,3 +149,93 @@ def add_to_species_data(
self.species_data.append(SpeciesData(**params))

return self.species_data[-1]

def initialize_species_from_yaml(self, yaml_file: str):
"""
Function that initializes species from a yaml file
Args:
yaml_file (str): Path to the yaml file
"""

with open(yaml_file) as f:
species_data = yaml.safe_load(f)

for species, item in species_data.items():

if not "calibration" in item.keys():
raise KeyError(f"No calibration data provided for species: {species}!")
else:
if not "peak_areas" in item["calibration"].keys():
raise KeyError(
f"No peak areas provided for calibration of species: {species}!"
)
if not "concentrations" in item["calibration"].keys():
raise KeyError(
"No concentrations provided for calibration of species:"
f" {species}!"
)

if not "chemical_formula" in item.keys():
raise KeyError(f"No chemical formula provided for species: {species}!")

if not "correction_factor" in item.keys():
raise KeyError(f"No correction factor provided for species: {species}!")

if not "electron_transfer" in item.keys():
raise KeyError(f"No electron transfer provided for species: {species}!")

# Create Calibration object and fit it to the given data
calibration = Calibration(
peak_areas=Data(
quantity="Peak area", values=item["calibration"]["peak_areas"]
),
concentrations=Data(
quantity=Quantity.CONCENTRATION.value,
values=item["calibration"]["concentrations"],
),
)
calibration.calibrate()

# Add species
self.add_to_species_data(
species=species,
chemical_formula=item["chemical_formula"],
calibration=calibration,
correction_factor=item["correction_factor"],
electron_transfer=item["electron_transfer"],
)

@property
def volumetric_flow_time_course(self) -> list:
"""This property extracts the volumetric flow time as well as the flow it self from the experiment class
Returns:
list: Datetime list and flow value list
"""
volumetric_flow_datetime_list = []
volumetric_flow_values_list = []

mfm_measurements = self.get(
"measurements", "measurement_type", "MFM measurement"
)[0]
for mfm_measurement in mfm_measurements:
volumetric_flow_datetime_list.extend(
mfm_measurement.get(
"experimental_data", "quantity", Quantity.DATETIME.value
)[0][0].values
)
volumetric_flow_values_list.extend(
mfm_measurement.get(
"experimental_data", "quantity", Quantity.VOLUMETRICFLOWRATE.value
)[0][0].values
)

# If data is directly read in from the experiment, it is the correct format, if read from json dataset, it is a string and needs to be converted
if not type(volumetric_flow_datetime_list[0]) == DataType.DATETIME.value:
volumetric_flow_datetime_list = [
pd.to_datetime(timestamp, format="%Y-%m-%dT%H:%M:%S").to_pydatetime()
for timestamp in volumetric_flow_datetime_list
]

return [volumetric_flow_datetime_list, volumetric_flow_values_list]

0 comments on commit f4f90b6

Please sign in to comment.