Skip to content

Commit

Permalink
adaption in widget for species data
Browse files Browse the repository at this point in the history
  • Loading branch information
samirdarouich committed Mar 4, 2024
1 parent 41eead0 commit 8cd2a32
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 306 deletions.
53 changes: 52 additions & 1 deletion FAIRFlowChemistry/core/experiment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import yaml
import sdRDM

import pandas as pd

from typing import Dict, List, Optional
from pydantic import PrivateAttr, model_validator
from uuid import uuid4
Expand Down Expand Up @@ -137,6 +138,56 @@ 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(f"No concentrations provided for calibration of species: {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
Expand Down
27 changes: 10 additions & 17 deletions FAIRFlowChemistry/tools/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ def add_experiment(self,_):

experiment.measurements = [ *potentiostatic_measurement, *mfm_measurement, *gc_measurements_list ]

# Read in parameters such as calibration, correction factors and farraday coefficients and save it in Experiment class #
experiment.calibrate_from_json( self.calib_files.value[0], degree=1 )
experiment.read_correction_factors( self.correction_files.value[0] )
experiment.read_faraday_coefficients( self.faraday_files.value[0] )
# Initialize species data such as calibration, correction factors and transfering eletron number
for species_file in self.species_file.value:
experiment.initialize_species_from_yaml( species_file )

# Append new experiment to current dataset #
self.dataset.experiments.append( experiment )
Expand All @@ -80,13 +79,11 @@ def add_experiment(self,_):
self.experiments.value = [ exp.id for exp in self.dataset.experiments ]

# Empty files widget #
self.galvano_files.value = []
self.GC_files.value = []
self.MFM_files.value = []
self.calib_files.value = []
self.correction_files.value = []
self.faraday_files.value = []
self.experiment_name.value = ""
self.galvano_files.value = []
self.GC_files.value = []
self.MFM_files.value = []
self.species_file.value = []
self.experiment_name.value = ""

def folder_dropdown_option_handler(self,_):
# If no subdirectories exist, then the parent folder is simply the first parent, otherwise it is the 2nd parent
Expand Down Expand Up @@ -199,9 +196,7 @@ def choose_data(self, root: Path, dataset_directory: str) -> None:
self.galvano_files = widgets.TagsInput(allow_duplicates=False)
self.GC_files = widgets.TagsInput(allow_duplicates=False)
self.MFM_files = widgets.TagsInput(allow_duplicates=False)
self.calib_files = widgets.TagsInput(allow_duplicates=False)
self.correction_files = widgets.TagsInput(allow_duplicates=False)
self.faraday_files = widgets.TagsInput(allow_duplicates=False)
self.species_file = widgets.TagsInput(allow_duplicates=False)
self.experiments = widgets.TagsInput(allow_duplicates=False)

self.file_folder = root
Expand Down Expand Up @@ -239,9 +234,7 @@ def choose_data(self, root: Path, dataset_directory: str) -> None:
widgets6 = widgets.VBox([widgets.Label(value='Files for galvanostat:'), self.galvano_files])
widgets7 = widgets.VBox([widgets.Label(value='Files for gas chromatography:'), self.GC_files])
widgets8 = widgets.VBox([widgets.Label(value='Files for mass flow meter:'), self.MFM_files])
widgets9 = widgets.HBox([widgets.VBox([widgets.Label(value='File for calibration:'), self.calib_files]),
widgets.VBox([widgets.Label(value='File for correction factors:'), self.correction_files]),
widgets.VBox([widgets.Label(value='File for transferring electrons:'), self.faraday_files])])
widgets9 = widgets.HBox([widgets.VBox([widgets.Label(value='Files for species initialization:'), self.species_file])])
widgets10 = widgets.VBox([widgets.VBox([widgets.Label(value='After selecting all necessary files for an experiment, add the experiment to the chosen dataset.'),
self.experiment_name, self.button_add_exp]),
widgets.VBox([widgets.Label(value='Experiments:'),self.experiments])])
Expand Down
2 changes: 0 additions & 2 deletions FAIRFlowChemistry/tools/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from FAIRFlowChemistry.core import ComponentType




def gc_parser(metadata_path: Path, experimental_data_path: Path):
"""
Function that reads in a file from a gas chromotography. Important information that is extracted is the
Expand Down
200 changes: 2 additions & 198 deletions Main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e377279908cb44689997b04c468c918b",
"model_id": "6a1e56240df044bdac1e179b94c5dd3f",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -137,209 +137,13 @@
"rrdw.choose_data( root = root, dataset_directory = \"datasets\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"## Change initalizing species data!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Carbon dioxide': {'chemical_formula': 'CO2',\n",
" 'calibration': {'concentrations': [0, 50], 'peak_areas': [0, 38653]},\n",
" 'correction_factor': 0.74,\n",
" 'electron_transfer': 2},\n",
" 'Carbon monoxide': {'chemical_formula': 'CO',\n",
" 'calibration': {'concentrations': [0.5, 1, 5],\n",
" 'peak_areas': [797, 1328, 7223]},\n",
" 'correction_factor': 1.0,\n",
" 'electron_transfer': 2},\n",
" 'Hydrogen': {'chemical_formula': 'H2',\n",
" 'calibration': {'concentrations': [5, 10, 20], 'peak_areas': [71, 153, 330]},\n",
" 'correction_factor': 1.01,\n",
" 'electron_transfer': 2},\n",
" 'Ethane': {'chemical_formula': 'C2H6',\n",
" 'calibration': {'concentrations': [0, 5], 'peak_areas': [0, 12168]},\n",
" 'correction_factor': None,\n",
" 'electron_transfer': 16},\n",
" 'Ethene': {'chemical_formula': 'C2H4',\n",
" 'calibration': {'concentrations': [0.5, 2, 3],\n",
" 'peak_areas': [1122, 4864, 7297]},\n",
" 'correction_factor': None,\n",
" 'electron_transfer': 12},\n",
" 'Methane': {'chemical_formula': 'CH4',\n",
" 'calibration': {'concentrations': [5, 10], 'peak_areas': [5727, 11991]},\n",
" 'correction_factor': 0.76,\n",
" 'electron_transfer': 8}}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import yaml\n",
"from FAIRFlowChemistry.core import Calibration, Data, Quantity, Experiment\n",
"\n",
"\n",
"with open(\"data/additional_data/species_data.yaml\") as f:\n",
" species_data = yaml.safe_load(f)\n",
"species_data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"test = Experiment()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[4mCalibration\u001b[0m\n",
"├── \u001b[94mid\u001b[0m = bcef0a33-0954-40e8-84ce-812c2aa34286\n",
"├── \u001b[94mpeak_areas\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = 6dbccfbd-2c00-478e-89d9-f17a37cdad92\n",
"│ ├── \u001b[94mquantity\u001b[0m = Peak area\n",
"│ └── \u001b[94mvalues\u001b[0m = [0.0, 38653.0, ...]\n",
"├── \u001b[94mconcentrations\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = 7824671b-6ad3-48e5-a08e-75fda85486bd\n",
"│ ├── \u001b[94mquantity\u001b[0m = Concentration\n",
"│ └── \u001b[94mvalues\u001b[0m = [0.0, 50.0, ...]\n",
"├── \u001b[94mregression_coefficients\u001b[0m = [-4.466559424312742e-15, 0.0012935606550591155, ...]\n",
"└── \u001b[94mdegree\u001b[0m = 1\n",
"\n",
"\u001b[4mCalibration\u001b[0m\n",
"├── \u001b[94mid\u001b[0m = 66c51884-1dae-451e-b103-e6825139a3d4\n",
"├── \u001b[94mpeak_areas\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = 23ce97b3-17cc-461d-9611-007fb871847e\n",
"│ ├── \u001b[94mquantity\u001b[0m = Peak area\n",
"│ └── \u001b[94mvalues\u001b[0m = [797.0, 1328.0, 7223.0, ...]\n",
"├── \u001b[94mconcentrations\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = aac491a0-c4f5-4089-bb3b-e6da4fa87d5b\n",
"│ ├── \u001b[94mquantity\u001b[0m = Concentration\n",
"│ └── \u001b[94mvalues\u001b[0m = [0.5, 1.0, 5.0, ...]\n",
"├── \u001b[94mregression_coefficients\u001b[0m = [0.012656829292048657, 0.0006912740171292097, ...]\n",
"└── \u001b[94mdegree\u001b[0m = 1\n",
"\n",
"\u001b[4mCalibration\u001b[0m\n",
"├── \u001b[94mid\u001b[0m = abcb5f13-bdc7-4184-81e3-72f43d209ba1\n",
"├── \u001b[94mpeak_areas\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = cf425f61-3655-40af-a425-e6b1d50b70df\n",
"│ ├── \u001b[94mquantity\u001b[0m = Peak area\n",
"│ └── \u001b[94mvalues\u001b[0m = [71.0, 153.0, 330.0, ...]\n",
"├── \u001b[94mconcentrations\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = 0531cb42-b9e1-4065-a1cf-2e564f28084a\n",
"│ ├── \u001b[94mquantity\u001b[0m = Concentration\n",
"│ └── \u001b[94mvalues\u001b[0m = [5.0, 10.0, 20.0, ...]\n",
"├── \u001b[94mregression_coefficients\u001b[0m = [1.0135636425894583, 0.05768828352388381, ...]\n",
"└── \u001b[94mdegree\u001b[0m = 1\n",
"\n",
"\u001b[4mCalibration\u001b[0m\n",
"├── \u001b[94mid\u001b[0m = 94115cf4-c6d5-4756-997c-e431378b28ac\n",
"├── \u001b[94mpeak_areas\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = 41a4c0e7-46f6-4102-b790-54d8ef169a1c\n",
"│ ├── \u001b[94mquantity\u001b[0m = Peak area\n",
"│ └── \u001b[94mvalues\u001b[0m = [0.0, 12168.0, ...]\n",
"├── \u001b[94mconcentrations\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = e6ae0432-d07f-4d02-b56b-265045b8404e\n",
"│ ├── \u001b[94mquantity\u001b[0m = Concentration\n",
"│ └── \u001b[94mvalues\u001b[0m = [0.0, 5.0, ...]\n",
"├── \u001b[94mregression_coefficients\u001b[0m = [3.146078039972546e-16, 0.0004109138724523339, ...]\n",
"└── \u001b[94mdegree\u001b[0m = 1\n",
"\n",
"\u001b[4mCalibration\u001b[0m\n",
"├── \u001b[94mid\u001b[0m = 683a26eb-79bc-4c6c-837f-617cb7f3577a\n",
"├── \u001b[94mpeak_areas\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = d894b18d-c1f7-4920-8553-64de6277e082\n",
"│ ├── \u001b[94mquantity\u001b[0m = Peak area\n",
"│ └── \u001b[94mvalues\u001b[0m = [1122.0, 4864.0, 7297.0, ...]\n",
"├── \u001b[94mconcentrations\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = 05af66ef-32ce-4a6e-86c7-d7e7353401ad\n",
"│ ├── \u001b[94mquantity\u001b[0m = Concentration\n",
"│ └── \u001b[94mvalues\u001b[0m = [0.5, 2.0, 3.0, ...]\n",
"├── \u001b[94mregression_coefficients\u001b[0m = [0.04225127219798618, 0.0004045205287514898, ...]\n",
"└── \u001b[94mdegree\u001b[0m = 1\n",
"\n",
"\u001b[4mCalibration\u001b[0m\n",
"├── \u001b[94mid\u001b[0m = 98bc4c48-7644-4fbd-bd1f-ef2092ca2a5e\n",
"├── \u001b[94mpeak_areas\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = a30301a1-a5fc-4c40-9e05-1448cb73baf5\n",
"│ ├── \u001b[94mquantity\u001b[0m = Peak area\n",
"│ └── \u001b[94mvalues\u001b[0m = [5727.0, 11991.0, ...]\n",
"├── \u001b[94mconcentrations\u001b[0m\n",
"│ └── \u001b[4mData\u001b[0m\n",
"│ ├── \u001b[94mid\u001b[0m = d38f9ca7-29bf-4547-a6d7-6899d29a4d33\n",
"│ ├── \u001b[94mquantity\u001b[0m = Concentration\n",
"│ └── \u001b[94mvalues\u001b[0m = [5.0, 10.0, ...]\n",
"├── \u001b[94mregression_coefficients\u001b[0m = [0.4286398467432958, 0.0007982120051085568, ...]\n",
"└── \u001b[94mdegree\u001b[0m = 1\n",
"\n"
]
}
],
"source": [
"for species,item in species_data.items():\n",
"\n",
" # Create Calibration object and fit it to the given data\n",
" calibration = Calibration(\n",
" peak_areas=Data(\n",
" quantity=\"Peak area\", values=item[\"calibration\"][\"peak_areas\"]\n",
" ),\n",
" concentrations=Data(\n",
" quantity=Quantity.CONCENTRATION.value,\n",
" values=item[\"calibration\"][\"concentrations\"],\n",
" ),\n",
" )\n",
" calibration.calibrate()\n",
" \n",
" test.add_to_species_data( species = species, \n",
" chemical_formula = item[\"chemical_formula\"],\n",
" calibration = calibration,\n",
" correction_factor = item[\"correction_factor\"],\n",
" electron_transfer = item[\"electron_transfer\"]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# widget that reads in PID and LINKS each measurement types to devices?\n",
"\n",
"# or first read PID and then provide the possibility to link each measurement to a device from dropdown!"
"# first read PID and then provide the possibility to link each measurement to a device from dropdown!"
]
},
{
Expand Down
Loading

0 comments on commit 8cd2a32

Please sign in to comment.