From 8cd2a321d0f28e24e41c7a3ac5d90aa738b1646d Mon Sep 17 00:00:00 2001 From: samirdarouich Date: Mon, 4 Mar 2024 14:30:23 +0100 Subject: [PATCH] adaption in widget for species data --- FAIRFlowChemistry/core/experiment.py | 53 ++++- FAIRFlowChemistry/tools/acquisition.py | 27 +-- FAIRFlowChemistry/tools/reader.py | 2 - Main.ipynb | 200 +----------------- data/additional_data/calibration.json | 74 ------- data/additional_data/correction_factors.json | 6 - .../additional_data/faraday_coefficients.json | 8 - 7 files changed, 64 insertions(+), 306 deletions(-) delete mode 100644 data/additional_data/calibration.json delete mode 100644 data/additional_data/correction_factors.json delete mode 100644 data/additional_data/faraday_coefficients.json diff --git a/FAIRFlowChemistry/core/experiment.py b/FAIRFlowChemistry/core/experiment.py index 537ca86..83d4334 100644 --- a/FAIRFlowChemistry/core/experiment.py +++ b/FAIRFlowChemistry/core/experiment.py @@ -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 @@ -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 diff --git a/FAIRFlowChemistry/tools/acquisition.py b/FAIRFlowChemistry/tools/acquisition.py index d0a55aa..9c95e3c 100644 --- a/FAIRFlowChemistry/tools/acquisition.py +++ b/FAIRFlowChemistry/tools/acquisition.py @@ -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 ) @@ -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 @@ -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 @@ -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])]) diff --git a/FAIRFlowChemistry/tools/reader.py b/FAIRFlowChemistry/tools/reader.py index 7d377cc..3913dc6 100644 --- a/FAIRFlowChemistry/tools/reader.py +++ b/FAIRFlowChemistry/tools/reader.py @@ -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 diff --git a/Main.ipynb b/Main.ipynb index c09a26e..8889c89 100644 --- a/Main.ipynb +++ b/Main.ipynb @@ -118,7 +118,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "e377279908cb44689997b04c468c918b", + "model_id": "6a1e56240df044bdac1e179b94c5dd3f", "version_major": 2, "version_minor": 0 }, @@ -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!" ] }, { diff --git a/data/additional_data/calibration.json b/data/additional_data/calibration.json deleted file mode 100644 index 7565e1c..0000000 --- a/data/additional_data/calibration.json +++ /dev/null @@ -1,74 +0,0 @@ -{ - "Hydrogen": { - "chemical_formula": "H2", - "peak_areas": [ - 71, - 153, - 330 - ], - "concentrations": [ - 5, - 10, - 20 - ] - }, - "Carbon monoxide": { - "chemical_formula": "CO", - "peak_areas": [ - 797, - 1328, - 7223 - ], - "concentrations": [ - 0.5, - 1, - 5 - ] - }, - "Carbon dioxide": { - "chemical_formula": "CO2", - "peak_areas": [ - 0, - 38653 - ], - "concentrations": [ - 0, - 50 - ] - }, - "Methane": { - "chemical_formula": "CH4", - "peak_areas": [ - 5727, - 11991 - ], - "concentrations": [ - 5, - 10 - ] - }, - "Ethene": { - "chemical_formula": "C2H4", - "peak_areas": [ - 1122, - 4864, - 7297 - ], - "concentrations": [ - 0.5, - 2, - 3 - ] - }, - "Ethane": { - "chemical_formula": "C2H6", - "peak_areas": [ - 0, - 12168 - ], - "concentrations": [ - 0, - 5 - ] - } -} \ No newline at end of file diff --git a/data/additional_data/correction_factors.json b/data/additional_data/correction_factors.json deleted file mode 100644 index ddfd358..0000000 --- a/data/additional_data/correction_factors.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "Hydrogen": 1.01, - "Carbon monoxide": 1.00, - "Carbon dioxide": 0.74, - "Methane": 0.76 -} \ No newline at end of file diff --git a/data/additional_data/faraday_coefficients.json b/data/additional_data/faraday_coefficients.json deleted file mode 100644 index d92c081..0000000 --- a/data/additional_data/faraday_coefficients.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Hydrogen": 2, - "Carbon monoxide": 2, - "Carbon dioxide": 2, - "Methane": 8, - "Ethene": 12, - "Ethane": 16 -} \ No newline at end of file