Skip to content

Commit

Permalink
finished overwork
Browse files Browse the repository at this point in the history
  • Loading branch information
samirdarouich committed Mar 6, 2024
1 parent ba18651 commit 4183fa9
Show file tree
Hide file tree
Showing 10 changed files with 40,366 additions and 50,256 deletions.
Binary file modified FAIRFlowChemistry/.DS_Store
Binary file not shown.
27 changes: 27 additions & 0 deletions FAIRFlowChemistry/core/plantsetup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sdRDM

import networkx as nx
import matplotlib.pyplot as plt
from typing import List, Optional
from pydantic import PrivateAttr
from uuid import uuid4
Expand Down Expand Up @@ -100,3 +102,28 @@ def add_to_components(
params["id"] = id
self.components.append(Component(**params))
return self.components[-1]

def visualize(self, save_path: str=""):
"""
Function that visualize the plantsetup as graph.
Args:
save_path (str, optional): Path to save the graph (if wanted). Defaults to "".
"""
# Create a directed graph
G = nx.DiGraph()

# Add nodes and edges
for component in self.components:
G.add_node(component.component_id)
for connection in component.connections:
G.add_edge(component.component_id, connection)

# Draw the graph
plt.figure()
pos = nx.spring_layout(G, seed=42)
nx.draw(G, pos, with_labels=True, node_size=2000, node_color="skyblue", font_size=10, font_weight="bold", arrowsize=20, linewidths=2)
plt.title('Plantsetup')
if save_path: plt.savefig( save_path )
plt.show()
plt.close()
1 change: 0 additions & 1 deletion FAIRFlowChemistry/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .initialize import initialize_dataset
from .acquisition import reading_raw_data_widget
from .analysis import analyzing_raw_data_widget
from .darus_upload import DaRUS_upload


181 changes: 93 additions & 88 deletions FAIRFlowChemistry/tools/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import ipywidgets as widgets

from pathlib import Path
from typing import List, Callable
from IPython.display import display
from typing import List
from IPython.display import display, clear_output

# Import general tools and objects of this datamodel

Expand Down Expand Up @@ -60,47 +60,31 @@ def update_component_list(self, component_list: List[str]):

class reading_raw_data_widget():

def measurement_tabs(self):

# Define tab widget
self.tabs = widgets.Tab( [obj.full_layout for obj in self.measurement_objects] )

# Set title of the tabs
for i,title in enumerate( [ obj.name for obj in self.measurement_objects ] ):
self.tabs.set_title(i, title)

heading = widgets.Label(value="Files for measurements:")

with self.tab_output:
self.tab_output.clear_output(wait=False)
display( widgets.VBox([ heading, self.tabs ]))
def dataset_input_handler(self,_):
try:
with open(self.dataset_dropdown.value) as f:
self.dataset = Dataset.from_json(f)
self.experiments.value = [ exp.id for exp in self.dataset.experiments ]
self.plant = self.dataset.experiments[0].plant_setup if self.dataset.experiments else PlantSetup()

# Update component list
self.component_list = [ pl.component_id for pl in self.plant.components ]

def read_pid(self,_):
# Function that reads in DEXPI PID file and generates the PlantSetup
self.plant = DEXPI2sdRDM( self.pid_file.value )
if self.component_list:
with self.pid_output:
clear_output(wait=False)
print("PID taken from first experiment of dataset!\n")
else:
with self.pid_output:
clear_output(wait=False)
print("")

# Call measurement input handler to update
self.measurement_input_handler(None)

with self.pid_output:
self.pid_output.clear_output(wait=False)
print("PID sucessfully read out!\n")
except:
raise KeyError("\nChoosen dataset cannot be interpreted!\n")

# Update component list
self.component_list = [ pl.component_id for pl in self.plant.components ]

# Call tab widget
self.measurement_input_handler(None)

def visualize_pid(self,_):
# Function that visualizes the PID as graph
# If plant is just initialized read in PID first
if not self.plant.components:
self.read_pid(None)
self.plant.visualize()

def save_dataset(self,_):
# Function to save dataset
with open(self.dataset_dropdown.value, "w") as f: f.write(self.dataset.json())
print("Dataset saved.")

def add_file(self, category: str, file: str ):
# Function that adds a file to a chosen category
# The file is added to the selected measurement tab to the selected children (galvanostat, gas chromatography, etc.)
Expand All @@ -124,7 +108,67 @@ def add_file(self, category: str, file: str ):

elif category == "P&ID":
self.pid_file.value = file

def read_pid(self,_):
# Function that reads in DEXPI PID file and generates the PlantSetup
self.plant = DEXPI2sdRDM( self.pid_file.value )

with self.pid_output:
self.pid_output.clear_output(wait=False)
print("PID sucessfully read out!\n")

# Update component list
self.component_list = [ pl.component_id for pl in self.plant.components ]

# Call tab widget
self.measurement_input_handler(None)

def visualize_pid(self,_):
# Function that visualizes the PID as graph
# If plant is just initialized read in PID first
if not self.plant.components:
self.read_pid(None)
self.plant.visualize()

def measurement_input_handler(self,_):

# Delete measurement object that are not in the measurements widget anymore
del_idx = [ i for i,obj in enumerate(self.measurement_objects) if not obj.name in self.measurements.value ]
del_idx.sort( reverse = True)

for idx in del_idx:
del self.measurement_objects[idx]

# Get names of current experiments
measurement_names = [ obj.name for obj in self.measurement_objects ]

# Add new measurement objects if they are not already there
for i, measurement in enumerate(self.measurements.value):
if not measurement in measurement_names:
self.measurement_objects.insert( i, measurement_object( name = measurement, component_list = self.component_list ) )

# Update component list of all exisiting measurements
for obj in self.measurement_objects:
obj.update_component_list( self.component_list )

# Call measurement tab widget
self.measurement_tabs()

def measurement_tabs(self):

# Define tab widget
self.tabs = widgets.Tab( [obj.full_layout for obj in self.measurement_objects] )

# Set title of the tabs
for i,title in enumerate( [ obj.name for obj in self.measurement_objects ] ):
self.tabs.set_title(i, title)

heading = widgets.Label(value="Files for measurements:")

with self.tab_output:
self.tab_output.clear_output(wait=False)
display( widgets.VBox([ heading, self.tabs ]))

def add_experiment(self,_):

## Read in selected raw data and save it in Experiment class ##
Expand Down Expand Up @@ -180,50 +224,9 @@ def add_experiment(self,_):
self.experiments.value = [ exp.id for exp in self.dataset.experiments ]

# Empty files widget
self.measurements.value = [ "Measurement 1" ]
self.measurements.value = []
self.experiment_name.value = ""

def dataset_input_handler(self,_):
try:
with open(self.dataset_dropdown.value) as f:
self.dataset = Dataset.from_json(f)
self.experiments.value = [ exp.id for exp in self.dataset.experiments ]
self.plant = self.dataset.experiments[0].plant_setup if self.dataset.experiments else PlantSetup()

# Update component list
self.component_list = [ pl.component_id for pl in self.plant.components ]

if self.component_list:
with self.pid_output:
self.pid_output.clear_output(wait=False)
print("PID taken from first experiment of dataset!\n")
except:
raise KeyError("\nChoosen dataset cannot be interpreted!\n")

def measurement_input_handler(self,_):

# Delete measurement object that are not in the measurements widget anymore
del_idx = [ i for i,obj in enumerate(self.measurement_objects) if not obj.name in self.measurements.value ]
del_idx.sort( reverse = True)

for idx in del_idx:
del self.measurement_objects[idx]

# Get names of current experiments
measurement_names = [ obj.name for obj in self.measurement_objects ]

# Add new measurement objects if they are not already there
for i, measurement in enumerate(self.measurements.value):
if not measurement in measurement_names:
self.measurement_objects.insert( i, measurement_object( name = measurement, component_list = self.component_list ) )

# Update component list of all exisiting measurements
for obj in self.measurement_objects:
obj.update_component_list( self.component_list )

# Call measurement tab widget
self.measurement_tabs()

def experiment_input_handler(self,_):

# Delete experiment objects that are not in the experiment widget anymore
Expand All @@ -233,7 +236,11 @@ def experiment_input_handler(self,_):
for idx in del_idx:
del self.dataset.experiments[idx]


def save_dataset(self,_):
# Function to save dataset
with open(self.dataset_dropdown.value, "w") as f: f.write(self.dataset.json())
print("Dataset saved.")

def choose_data(self, root: Path, dataset_directory: str) -> None:

self.librarian = Librarian(root_directory=root)
Expand All @@ -251,7 +258,7 @@ def choose_data(self, root: Path, dataset_directory: str) -> None:
add_file_callalbe = self.add_file )

# Define all widgets
self.dataset_dropdown = widgets.Dropdown( options=[(path.parts[-1],path) for _,path in datasets.items()],
self.dataset_dropdown = widgets.Dropdown( options=[("",Path(""))]+[(path.parts[-1],path) for _,path in datasets.items()],
description="Choose dataset",
layout=widgets.Layout(width='auto'),
style={'description_width': 'auto'})
Expand All @@ -275,7 +282,7 @@ def choose_data(self, root: Path, dataset_directory: str) -> None:

self.measurements = widgets.TagsInput( allow_duplicates=False )

self.button_save = widgets.Button( description='Save dataset as: %s'%self.dataset_dropdown.value.name,
self.button_save = widgets.Button( description=f'Save dataset as: {self.dataset_dropdown.value.name}',
layout=widgets.Layout(width="30%"),
style={"button_color": 'lightblue'})

Expand All @@ -300,9 +307,7 @@ def choose_data(self, root: Path, dataset_directory: str) -> None:
self.experiments.observe(self.experiment_input_handler,names="value")

# Initialize several objects
self.dataset_input_handler(None)
self.measurement_objects = [ ]
self.measurements.value = [ "Measurement 1" ]
self.measurement_objects = []

# Display the widgets

Expand Down
6 changes: 3 additions & 3 deletions FAIRFlowChemistry/tools/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ def calculate_faraday_efficiencies(self, gc_measurement: Measurement):
faraday_efficiency_df.dropna(inplace=True)

# Write into logger
logger.info( self.volumetric_fractions_df.to_string() )
logger.info( (self.material_flow_df*60*1000).rename( columns={"Material_flow [mol/s]":"Material_flow [mmol/min]"}).to_string() )
logger.info( (self.theoretical_material_flow_df*60*1000).rename( columns={"Theoretical_material_flow [mol/s]":"Theoretical_material_flow [mmol/min]"}).to_string() )
logger.info( "\n"+self.volumetric_fractions_df.to_string() + "\n" )
logger.info( (self.material_flow_df*60*1000).rename( columns={"Material_flow [mol/s]":"Material_flow [mmol/min]"}).to_string() + "\n" )
logger.info( (self.theoretical_material_flow_df*60*1000).rename( columns={"Theoretical_material_flow [mol/s]":"Theoretical_material_flow [mmol/min]"}).to_string() + "\n" )

return faraday_efficiency_df
Loading

0 comments on commit 4183fa9

Please sign in to comment.