Included package data in data/csvs and data/xmls. Note this only works for sdists. If bdist is needed, research "manifest.in" python setup files.

pull/1/head
titusquah 5 years ago
parent f07fb03b93
commit 77e4cefd2c

@ -1,6 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="Titus">
<words>
<w>cantera</w>
<w>coeffs</w>
<w>conc</w>
<w>csvs</w>
@ -12,11 +13,15 @@
<w>ftol</w>
<w>kmol</w>
<w>lmse</w>
<w>matplotlib</w>
<w>maxiter</w>
<w>molality</w>
<w>ndarray</w>
<w>pred</w>
<w>quah</w>
<w>reeps</w>
<w>scipy</w>
<w>seaborn</w>
<w>slsqp</w>
<w>thermo</w>
<w>xmls</w>

@ -4,10 +4,13 @@
<list default="true" id="f4439dc0-6756-4612-8f7d-596d8949f300" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/dictionaries/Titus.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dictionaries/Titus.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/data/xmls/twophase.xml" beforeDir="false" afterPath="$PROJECT_DIR$/data/xmls/twophase.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/docs/Examples/1_getting_started.ipynb" beforeDir="false" afterPath="$PROJECT_DIR$/docs/Examples/1_getting_started.ipynb" afterDir="false" />
<change beforePath="$PROJECT_DIR$/reeps.py" beforeDir="false" afterPath="$PROJECT_DIR$/reeps.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/reeps.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/setup.py" beforeDir="false" afterPath="$PROJECT_DIR$/setup.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tests/__init__.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/tests/test_multi_reeps.py" beforeDir="false" afterPath="$PROJECT_DIR$/tests/test_multi_reeps.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tests/test_reeps.py" beforeDir="false" afterPath="$PROJECT_DIR$/tests/test_reeps.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -18,10 +21,16 @@
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
<option value="Setup Script" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="PREVIOUS_COMMIT_AUTHORS">
<list>
<option value="AN" />
</list>
</option>
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="1cb4dMPbzwchdRM32ufpLaZ6Ir0" />
@ -33,12 +42,13 @@
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="com.intellij.ide.scratch.LRUPopupBuilder$1/New Scratch File" value="Python" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../../parameter estimation/parameter estimation/fit.py" />
<property name="run.code.analysis.last.selected.profile" value="aDefault" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
<property name="settings.editor.selected.configurable" value="preferences.sourceCode" />
</component>
<component name="RunManager" selected="Python.li_data_fit">
<configuration name="formatting_methods" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<component name="RunManager" selected="Python.test_multi_reeps">
<configuration name="li_data_fit" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="parameter-estimation" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
@ -50,7 +60,7 @@
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/../../anl_box/Box Sync/titus/one_rare_earth_fit/formatting_methods.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/../../anl_box/Box Sync/titus/one_rare_earth_fit/li_data_fit.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
@ -59,7 +69,7 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="gensurfdata" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<configuration name="scratch_1" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="parameter-estimation" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
@ -67,29 +77,32 @@
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../../anl_box/Box Sync/titus/onboarding/codes/multi/1_fit/01_Surface/2_linearfits" />
<option name="WORKING_DIRECTORY" value="$APPLICATION_CONFIG_DIR$/scratches" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/../../anl_box/Box Sync/titus/onboarding/codes/multi/1_fit/01_Surface/2_linearfits/gensurfdata.py" />
<option name="SCRIPT_NAME" value="$APPLICATION_CONFIG_DIR$/scratches/scratch_1.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="li_data_fit" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<configuration name="test_multi_reeps" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="parameter-estimation" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../../anl_box/Box Sync/titus/one_rare_earth_fit" />
<option name="IS_MODULE_SDK" value="false" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/tests" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/../../anl_box/Box Sync/titus/one_rare_earth_fit/li_data_fit.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/tests/test_multi_reeps.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
@ -135,11 +148,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.test_multi_reeps" />
<item itemvalue="Python.test_reeps" />
<item itemvalue="Python.test_reeps" />
<item itemvalue="Python.li_data_fit" />
<item itemvalue="Python.li_data_fit" />
<item itemvalue="Python.formatting_methods" />
<item itemvalue="Python.formatting_methods" />
<item itemvalue="Python.li_data_fit" />
<item itemvalue="Python.test_multi_reeps" />
</list>
</recent_temporary>
</component>
@ -260,9 +273,10 @@
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state x="-1330" y="212" key="#Inspections/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1590787654691" />
<state x="552" y="179" width="816" height="729" key="#Intentions" timestamp="1591030394128">
<screen x="0" y="0" width="1920" height="1040" />
<state x="-1368" y="181" width="816" height="729" key="#Intentions" timestamp="1591805787309">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state x="-1368" y="181" width="816" height="729" key="#Intentions/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591805787309" />
<state x="552" y="179" width="816" height="729" key="#Intentions/0.0.1920.1040@0.0.1920.1040" timestamp="1591030394128" />
<state x="-1506" y="178" width="1092" height="706" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1590784453144">
<screen x="-1920" y="2" width="1920" height="1040" />
@ -272,11 +286,11 @@
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state x="-1213" y="379" key="ANALYSIS_DLG_com.intellij.analysis.BaseAnalysisAction$1/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1590787657711" />
<state x="556" y="115" key="CommitChangelistDialog2" timestamp="1591388457261">
<state x="169" y="115" key="CommitChangelistDialog2" timestamp="1591972425540">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="-1364" y="117" key="CommitChangelistDialog2/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591212137077" />
<state x="556" y="115" key="CommitChangelistDialog2/0.0.1920.1040@0.0.1920.1040" timestamp="1591388457261" />
<state x="169" y="115" key="CommitChangelistDialog2/0.0.1920.1040@0.0.1920.1040" timestamp="1591972425540" />
<state x="-1828" y="94" width="1736" height="856" key="DiffContextDialog" timestamp="1591048879404">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
@ -285,57 +299,63 @@
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state x="-1180" y="278" key="FileChooserDialogImpl/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1590786964173" />
<state width="1899" height="281" key="GridCell.Tab.0.bottom" timestamp="1591403861494">
<screen x="0" y="0" width="1920" height="1040" />
<state width="1899" height="280" key="GridCell.Tab.0.bottom" timestamp="1591920388062">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state width="1899" height="281" key="GridCell.Tab.0.bottom/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591310049025" />
<state width="1899" height="280" key="GridCell.Tab.0.bottom/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591920388062" />
<state width="1897" height="281" key="GridCell.Tab.0.bottom/0.0.1536.824/-1920.2.1920.1040@0.0.1536.824" timestamp="1590795386435" />
<state width="1899" height="281" key="GridCell.Tab.0.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1591403861494" />
<state width="1899" height="281" key="GridCell.Tab.0.center" timestamp="1591403861493">
<screen x="0" y="0" width="1920" height="1040" />
<state width="1899" height="280" key="GridCell.Tab.0.center" timestamp="1591920388062">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state width="1899" height="281" key="GridCell.Tab.0.center/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591310049023" />
<state width="1899" height="280" key="GridCell.Tab.0.center/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591920388062" />
<state width="1897" height="281" key="GridCell.Tab.0.center/0.0.1536.824/-1920.2.1920.1040@0.0.1536.824" timestamp="1590795386434" />
<state width="1899" height="281" key="GridCell.Tab.0.center/0.0.1920.1040@0.0.1920.1040" timestamp="1591403861493" />
<state width="1899" height="281" key="GridCell.Tab.0.left" timestamp="1591403861491">
<screen x="0" y="0" width="1920" height="1040" />
<state width="1899" height="280" key="GridCell.Tab.0.left" timestamp="1591920388061">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state width="1899" height="281" key="GridCell.Tab.0.left/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591310049022" />
<state width="1899" height="280" key="GridCell.Tab.0.left/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591920388061" />
<state width="1897" height="281" key="GridCell.Tab.0.left/0.0.1536.824/-1920.2.1920.1040@0.0.1536.824" timestamp="1590795386433" />
<state width="1899" height="281" key="GridCell.Tab.0.left/0.0.1920.1040@0.0.1920.1040" timestamp="1591403861491" />
<state width="1899" height="281" key="GridCell.Tab.0.right" timestamp="1591403861493">
<screen x="0" y="0" width="1920" height="1040" />
<state width="1899" height="280" key="GridCell.Tab.0.right" timestamp="1591920388062">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state width="1899" height="281" key="GridCell.Tab.0.right/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591310049024" />
<state width="1899" height="280" key="GridCell.Tab.0.right/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591920388062" />
<state width="1897" height="281" key="GridCell.Tab.0.right/0.0.1536.824/-1920.2.1920.1040@0.0.1536.824" timestamp="1590795386434" />
<state width="1899" height="281" key="GridCell.Tab.0.right/0.0.1920.1040@0.0.1920.1040" timestamp="1591403861493" />
<state x="-1460" y="164" key="SettingsEditor" timestamp="1590784300386">
<state x="-1460" y="164" key="SettingsEditor" timestamp="1591824476757">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state x="-1460" y="164" key="SettingsEditor/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1590784300386" />
<state x="-1460" y="164" key="SettingsEditor/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591824476757" />
<state width="498" height="446" key="SwitcherDM" timestamp="1591822611305">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state width="498" height="446" key="SwitcherDM/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591822611305" />
<state x="-1368" y="256" key="Vcs.Push.Dialog.v2" timestamp="1591047662716">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state x="-1368" y="256" key="Vcs.Push.Dialog.v2/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591047662716" />
<state x="579" y="298" key="com.intellij.ide.util.TipDialog" timestamp="1591627942552">
<state x="579" y="298" key="com.intellij.ide.util.TipDialog" timestamp="1591971684146">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="-1341" y="300" key="com.intellij.ide.util.TipDialog/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591192469960" />
<state x="-1341" y="300" key="com.intellij.ide.util.TipDialog/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591885589059" />
<state x="463" y="236" key="com.intellij.ide.util.TipDialog/0.0.1536.824/-1920.2.1920.1040@0.0.1536.824" timestamp="1590784072879" />
<state x="579" y="298" key="com.intellij.ide.util.TipDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1591627942552" />
<state x="769" y="438" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1591037545358">
<screen x="0" y="0" width="1920" height="1040" />
<state x="579" y="298" key="com.intellij.ide.util.TipDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1591971684146" />
<state x="-1151" y="440" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1591821870917">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state x="-1151" y="440" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591821870917" />
<state x="769" y="438" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2/0.0.1920.1040@0.0.1920.1040" timestamp="1591037545358" />
<state x="-1219" y="166" key="refactoring.ChangeSignatureDialog" timestamp="1591059193684">
<screen x="-1920" y="2" width="1920" height="1040" />
<state x="701" y="164" key="refactoring.ChangeSignatureDialog" timestamp="1591641371174">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="-1219" y="166" key="refactoring.ChangeSignatureDialog/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591059193684" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1591393246078">
<screen x="0" y="0" width="1920" height="1040" />
<state x="701" y="164" key="refactoring.ChangeSignatureDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1591641371174" />
<state x="-1297" y="227" width="672" height="678" key="search.everywhere.popup" timestamp="1591739392623">
<screen x="-1920" y="2" width="1920" height="1040" />
</state>
<state x="-1297" y="227" width="672" height="678" key="search.everywhere.popup/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591113024379" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/0.0.1920.1040@0.0.1920.1040" timestamp="1591393246078" />
<state x="-1297" y="227" width="672" height="678" key="search.everywhere.popup/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591739392623" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/0.0.1920.1040@0.0.1920.1040" timestamp="1591649653766" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>

@ -50,7 +50,7 @@
<const_cp Tmax="300.0" Tmin="298.0">
<t0 units="K">298.14999999999998</t0>
<h0 units="J/mol" updated="Updated at 14:33 6-5-2020">-4704699.156668724</h0>
<h0 units="J/mol" updated="Updated at 16:49 6-11-2020">-4704703.645715787</h0>
<s0 units="J/mol/K"> 1117.965 </s0>
<cp0 units="J/mol/K">0.0</cp0>
</const_cp>

File diff suppressed because one or more lines are too long

@ -1,572 +0,0 @@
from datetime import datetime
import cantera as ct
import pandas as pd
import numpy as np
from scipy.optimize import minimize
# noinspection PyPep8Naming
import xml.etree.ElementTree as ET
import seaborn as sns
import matplotlib.pyplot as plt
import shutil
import copy
from inspect import signature
import os
sns.set()
sns.set(font_scale=1.6)
class REEPS:
"""REEPS (Rare earth extraction parameter searcher)
Takes in experimental data
Returns parameters for GEM
Only good for 1 rare earth and 1 extractant
:param exp_csv_filename: (str) csv file name with experimental data
:param phases_xml_filename: (str) xml file with parameters for equilibrium calc
:param opt_dict: (dict) optimize info {species:{thermo_prop:guess}
:param phase_names: (list) names of phases in xml file
:param aq_solvent_name: (str) name of aqueous solvent in xml file
:param extractant_name: (str) name of extractant in xml file
:param diluant_name: (str) name of diluant in xml file
:param complex_name: (str) name of complex in xml file
:param rare_earth_ion_name: (str) name of rare earth ion in xml file
:param aq_solvent_rho: (float) density of solvent (g/L)
:param extractant_rho: (float) density of extractant (g/L)
:param diluant_rho: (float) density of diluant (g/L)
If no density is given, molar volume/molecular weight is used from xml
:param objective_function: (function) function to compute objective
By default, the objective function is log mean squared error
of distribution ratio np.log10(re_org/re_aq)
Function needs to take inputs:
objective_function(predicted_dict, measured_df, **kwargs)
**kwargs is optional
Below is the guide for referencing predicted values
| To access | Use |
|------------------------------------- |--------------------------|
| predicted rare earth eq conc in aq | predicted_dict['re_aq'] |
| predicted rare earth eq conc in org | predicted_dict['re_org'] |
| predicted hydrogen ion conc in aq | predicted_dict['h'] |
| predicted extractant conc in org | predicted_dict['z'] |
| predicted rare earth distribution ratio | predicted_dict['re_d'] |
For measured values, use the column names in the experimental data file
:param optimizer: (function) function to perform optimization
By default, the optimizer is scipy's optimize function with
default_kwargs= {"method": 'SLSQP',
"bounds": [(1e-1, 1e1)*len(x_guess)],
"constraints": (),
"options": {'disp': True, 'maxiter': 1000, 'ftol': 1e-6}}
Function needs to take inputs:
optimizer(objective_function, x_guess, **kwargs)
**kwargs is optional
:param temp_xml_file_path: (str) path to temporary xml file
default is local temp folder
"""
def __init__(self,
exp_csv_filename,
phases_xml_filename,
opt_dict,
phase_names,
aq_solvent_name,
extractant_name,
diluant_name,
complex_name,
rare_earth_ion_name,
aq_solvent_rho=None,
extractant_rho=None,
diluant_rho=None,
objective_function='Log-MSE',
optimizer='SLSQP',
temp_xml_file_path=None
):
self._built_in_obj_list = ['Log-MSE']
self._built_in_opt_list = ['SLSQP']
self._exp_csv_filename = exp_csv_filename
self._phases_xml_filename = phases_xml_filename
self._opt_dict = opt_dict
self._phase_names = phase_names
self._aq_solvent_name = aq_solvent_name
self._extractant_name = extractant_name
self._diluant_name = diluant_name
self._complex_name = complex_name
self._rare_earth_ion_name = rare_earth_ion_name
self._aq_solvent_rho = aq_solvent_rho
self._extractant_rho = extractant_rho
self._diluant_rho = diluant_rho
self._objective_function = None
self.set_objective_function(objective_function)
self._optimizer = None
self.set_optimizer(optimizer)
if temp_xml_file_path is None:
temp_xml_file_path = '{0}\\temp.xml'.format(os.getenv('TEMP'))
self._temp_xml_file_path = temp_xml_file_path
shutil.copyfile(phases_xml_filename, self._temp_xml_file_path)
self._phases = ct.import_phases(phases_xml_filename, phase_names)
self._exp_df = pd.read_csv(self._exp_csv_filename)
self._in_moles = None
self._aq_ind = None
self._org_ind = None
self.set_in_moles(feed_vol=1)
self._predicted_dict = None
self.update_predicted_dict()
@staticmethod
def log_mean_squared_error(predicted_dict, meas_df):
meas = meas_df.values[:, 2]
pred = predicted_dict['re_org'] / predicted_dict['re_aq']
log_pred = np.log10(pred)
log_meas = np.log10(meas)
obj = np.sum((log_pred - log_meas) ** 2)
return obj
@staticmethod
def slsqp_optimizer(objective, x_guess):
optimizer_kwargs = {"method": 'SLSQP',
"bounds": [(1e-1, 1e1)] * len(x_guess),
"constraints": (),
"options": {'disp': True, 'maxiter': 1000, 'ftol': 1e-6}}
res = minimize(objective, x_guess, **optimizer_kwargs)
est_parameters = res.x
return est_parameters
def get_exp_csv_filename(self) -> str:
return self._exp_csv_filename
def set_exp_csv_filename(self, exp_csv_filename):
self._exp_csv_filename = exp_csv_filename
self._exp_df = pd.read_csv(self._exp_csv_filename)
self.update_predicted_dict()
return None
def get_phases(self) -> list:
return self._phases
def set_phases(self, phases_xml_filename, phase_names):
"""Change xml and phase names
Also runs set_in_mole to set initial moles to 1 g/L"""
self._phases_xml_filename = phases_xml_filename
self._phase_names = phase_names
shutil.copyfile(phases_xml_filename, self._temp_xml_file_path)
self._phases = ct.import_phases(phases_xml_filename, phase_names)
self.set_in_moles(feed_vol=1)
self.update_predicted_dict()
return None
def get_opt_dict(self) -> dict:
return self._opt_dict
def set_opt_dict(self, opt_dict):
self._opt_dict = opt_dict
return None
def get_aq_solvent_name(self) -> str:
return self._aq_solvent_name
def set_aq_solvent_name(self, aq_solvent_name):
self._aq_solvent_name = aq_solvent_name
return None
def get_extractant_name(self) -> str:
return self._extractant_name
def set_extractant_name(self, extractant_name):
self._extractant_name = extractant_name
return None
def get_diluant_name(self) -> str:
return self._diluant_name
def set_diluant_name(self, diluant_name):
self._diluant_name = diluant_name
return None
def get_complex_name(self) -> str:
return self._complex_name
def set_complex_name(self, complex_name):
self._complex_name = complex_name
return None
def get_rare_earth_ion_name(self) -> str:
return self._rare_earth_ion_name
def set_rare_earth_ion_name(self, rare_earth_ion_name):
self._rare_earth_ion_name = rare_earth_ion_name
return None
def get_aq_solvent_rho(self) -> str:
return self._aq_solvent_rho
def set_aq_solvent_rho(self, aq_solvent_rho):
self._aq_solvent_rho = aq_solvent_rho
return None
def get_extractant_rho(self) -> str:
return self._extractant_rho
def set_extractant_rho(self, extractant_rho):
self._extractant_rho = extractant_rho
return None
def get_diluant_rho(self) -> str:
return self._diluant_rho
def set_diluant_rho(self, diluant_rho):
self._diluant_rho = diluant_rho
return None
def set_in_moles(self, feed_vol):
"""Function that initializes mole fractions
:param feed_vol: (float) feed volume of mixture (g/L)"""
phases_copy = self._phases.copy()
exp_df = self._exp_df.copy()
solvent_name = self._aq_solvent_name
extractant_name = self._extractant_name
diluant_name = self._diluant_name
solvent_rho = self._aq_solvent_rho
extractant_rho = self._extractant_rho
diluant_rho = self._diluant_rho
re_name = self._rare_earth_ion_name
mixed = ct.Mixture(phases_copy)
aq_ind = None
solvent_ind = None
for ind, phase in enumerate(phases_copy):
if solvent_name in phase.species_names:
aq_ind = ind
solvent_ind = phase.species_names.index(solvent_name)
if aq_ind is None:
raise Exception('Solvent "{0}" not found \
in xml file'.format(solvent_name))
if aq_ind == 0:
org_ind = 1
else:
org_ind = 0
self._aq_ind = aq_ind
self._org_ind = org_ind
extractant_ind = phases_copy[org_ind].species_names.index(
extractant_name)
diluant_ind = phases_copy[org_ind].species_names.index(diluant_name)
re_ind = phases_copy[aq_ind].species_names.index(re_name)
re_charge = phases_copy[aq_ind].species(re_ind).charge
mix_aq = mixed.phase(aq_ind)
mix_org = mixed.phase(org_ind)
solvent_mw = mix_aq.molecular_weights[solvent_ind] # g/mol
extractant_mw = mix_org.molecular_weights[extractant_ind]
diluant_mw = mix_org.molecular_weights[diluant_ind]
if solvent_rho is None:
solvent_rho = mix_aq(aq_ind).partial_molar_volumes[
solvent_ind] / solvent_mw * 1e6 # g/L
self._aq_solvent_rho = solvent_rho
if extractant_rho is None:
extractant_rho = mix_org(org_ind).partial_molar_volumes[
extractant_ind] / extractant_mw * 1e6
self._extractant_rho = extractant_rho
if diluant_rho is None:
diluant_rho = mix_org(org_ind).partial_molar_volumes[
extractant_ind] / extractant_mw * 1e6
self._diluant_rho = diluant_rho
in_moles_data = []
aq_phase_solvent_moles = feed_vol * solvent_rho / solvent_mw
for row in exp_df.values:
h_plus_moles = feed_vol * row[0]
hydroxide_ions = 0
rare_earth_moles = feed_vol * row[6]
chlorine_moles = re_charge * rare_earth_moles + h_plus_moles
extractant_moles = feed_vol * row[3]
extractant_vol = extractant_moles * extractant_mw / extractant_rho
diluant_vol = feed_vol - extractant_vol
diluant_moles = diluant_vol * diluant_rho / diluant_mw
complex_moles = 0
species_moles = [aq_phase_solvent_moles,
h_plus_moles,
hydroxide_ions,
chlorine_moles,
rare_earth_moles,
extractant_moles,
diluant_moles,
complex_moles,
]
in_moles_data.append(species_moles)
self._in_moles = pd.DataFrame(in_moles_data, columns=mixed.species_names)
self.update_predicted_dict()
return None
def get_in_moles(self) -> pd.DataFrame:
return self._in_moles
def set_objective_function(self, objective_function):
"""Set objective function. see class docstring for instructions"""
if not callable(objective_function) \
and objective_function not in self._built_in_obj_list:
raise Exception(
"objective_function must be a function "
"or in this strings list: {0}".format(self._built_in_obj_list))
if callable(objective_function):
if len(signature(objective_function).parameters) < 2:
raise Exception(
"objective_function must be a function "
"with at least 3 arguments:"
" f(predicted_dict, experimental_df,**kwargs)")
if objective_function == 'Log-MSE':
objective_function = self.log_mean_squared_error
self._objective_function = objective_function
return None
def get_objective_function(self):
return self._objective_function
def set_optimizer(self, optimizer):
if not callable(optimizer) \
and optimizer not in self._built_in_opt_list:
raise Exception(
"optimizer must be a function "
"or in this strings list: {0}".format(self._built_in_opt_list))
if callable(optimizer):
if len(signature(optimizer).parameters) < 2:
raise Exception(
"optimizer must be a function "
"with at least 2 arguments: "
"f(objective_func,x_guess,**kwargs)")
if optimizer == 'SLSQP':
optimizer = self.slsqp_optimizer
self._optimizer = optimizer
return None
def get_optimizer(self):
return self._optimizer
def update_predicted_dict(self, phases_xml_filename=None):
if phases_xml_filename is None:
phases_xml_filename = self._phases_xml_filename
phase_names = self._phase_names
aq_ind = self._aq_ind
org_ind = self._org_ind
complex_name = self._complex_name
extractant_name = self._extractant_name
rare_earth_ion_name = self._rare_earth_ion_name
in_moles = self._in_moles
phases_copy = ct.import_phases(phases_xml_filename, phase_names)
mix = ct.Mixture(phases_copy)
predicted_dict = {"re_aq": [],
"re_org": [],
"h": [],
"z": []
}
for row in in_moles.values:
mix.species_moles = row
mix.equilibrate('TP', log_level=0)
re_org = mix.species_moles[mix.species_index(
org_ind, complex_name)]
re_aq = mix.species_moles[mix.species_index(
aq_ind, rare_earth_ion_name)]
hydrogen_ions = mix.species_moles[mix.species_index(aq_ind, 'H+')]
extractant = mix.species_moles[mix.species_index(
org_ind, extractant_name)]
predicted_dict['re_aq'].append(re_aq)
predicted_dict['re_org'].append(re_org)
predicted_dict['h'].append(hydrogen_ions)
predicted_dict['z'].append(extractant)
predicted_dict['re_aq'] = np.array(predicted_dict['re_aq'])
predicted_dict['re_org'] = np.array(predicted_dict['re_org'])
predicted_dict['h'] = np.array(predicted_dict['h'])
predicted_dict['z'] = np.array(predicted_dict['z'])
self._predicted_dict = predicted_dict
return None
def get_predicted_dict(self):
return self._predicted_dict
def _internal_objective(self, x, kwargs=None):
"""default Log mean squared error between measured and predicted data
:param x: (list) thermo properties varied to minimize LMSE
:param kwargs: (list) arguments for objective_function
"""
temp_xml_file_path = self._temp_xml_file_path
exp_df = self._exp_df
objective_function = self._objective_function
opt_dict = copy.deepcopy(self._opt_dict)
i = 0
for species_name in opt_dict.keys():
for _ in opt_dict[species_name].keys():
i += 1
x = np.array(x)
if len(x.shape) == 1:
xs = np.array([x])
vectorized_x = False
else:
vectorized_x = True
xs = x
objective_values = []
for x in xs:
i = 0
for species_name in opt_dict.keys():
for thermo_prop in opt_dict[species_name].keys():
opt_dict[species_name][thermo_prop] *= x[i]
i += 1
self.update_xml(opt_dict, temp_xml_file_path)
self.update_predicted_dict(temp_xml_file_path)
predicted_dict = self.get_predicted_dict()
if kwargs is None:
# noinspection PyCallingNonCallable
obj = objective_function(predicted_dict, exp_df)
else:
# noinspection PyCallingNonCallable
obj = objective_function(predicted_dict, exp_df, **kwargs)
objective_values.append(obj)
if vectorized_x:
objective_values = np.array(objective_values)
else:
objective_values = objective_values[0]
return objective_values
def fit(self, objective_function=None, optimizer=None, objective_kwargs=None, optimizer_kwargs=None) -> dict:
"""Fits experimental to modeled data by minimizing objective function
Returns estimated complex enthalpy in J/mol
:param objective_function: (function) function to compute objective
:param optimizer: (function) function to perform optimization
:param optimizer_kwargs: (dict) arguments for optimizer
:param objective_kwargs: (dict) arguments for objective function
"""
if objective_function is not None:
self.set_objective_function(objective_function)
if optimizer is not None:
self.set_optimizer(optimizer)
def objective(x):
return self._internal_objective(x, objective_kwargs)
optimizer = self._optimizer
opt_dict = copy.deepcopy(self._opt_dict)
i = 0
for species_name in opt_dict.keys():
for _ in opt_dict[species_name].keys():
i += 1
x_guess = np.ones(i)
if optimizer_kwargs is None:
# noinspection PyCallingNonCallable
est_parameters = optimizer(objective, x_guess)
else:
# noinspection PyCallingNonCallable
est_parameters = optimizer(objective, x_guess, **optimizer_kwargs)
i = 0
for species_name in opt_dict.keys():
for thermo_prop in opt_dict[species_name].keys():
opt_dict[species_name][thermo_prop] *= est_parameters[i]
i += 1
self.update_predicted_dict()
return opt_dict
def update_xml(self,
info_dict,
phases_xml_filename=None):
"""updates xml file with info_dict
:param info_dict: (dict) info in {species_names:{thermo_prop:val}}
:param phases_xml_filename: (str) xml filename if editing other xml
"""
if phases_xml_filename is None:
phases_xml_filename = self._phases_xml_filename
tree = ET.parse(phases_xml_filename)
root = tree.getroot()
# Update xml file
for species_name in info_dict.keys():
for thermo_prop in info_dict[species_name].keys():
for species in root.iter('species'):
if species.attrib['name'] == species_name:
for changed_prop in species.iter(thermo_prop):
changed_prop.text = str(
info_dict[species_name][thermo_prop])
now = datetime.now()
changed_prop.set('updated',
'Updated at {0}:{1} {2}-{3}-{4}'
.format(now.hour, now.minute,
now.month, now.day,
now.year))
tree.write(phases_xml_filename)
if phases_xml_filename == self._phases_xml_filename:
self.set_phases(self._phases_xml_filename, self._phase_names)
return None
# noinspection PyUnusedLocal
def parity_plot(self, species='re_aq', save_path=None, print_r_squared=False):
"""Parity plot between measured and predicted rare earth composition"""
phases_copy = self._phases.copy()
mix = ct.Mixture(phases_copy)
aq_ind = self._aq_ind
exp_df = self._exp_df
in_moles = self._in_moles
rare_earth_ion_name = self._rare_earth_ion_name
pred = []
for row in in_moles.values:
mix.species_moles = row
mix.equilibrate('TP', log_level=0)
re_aq = mix.species_moles[mix.species_index(
aq_ind, rare_earth_ion_name)]
pred.append(re_aq)
pred = np.array(pred)
meas = exp_df.values[:, 1]
min_data = np.min([pred, meas])
max_data = np.max([pred, meas])
min_max_data = np.array([min_data, max_data])
fig, ax = plt.subplots()
re_element = ''
n_plus = 0
for char in self._rare_earth_ion_name:
if char.isalpha():
re_element = '{0}{1}'.format(re_element, char)
else:
n_plus += 1
re_ion_name = '$%s^{%d+}$' % (re_element, n_plus)
p1 = sns.scatterplot(meas, pred, color="r",
label="{0} eq. conc. (mol/L)".format(re_ion_name),
legend=False)
p2 = sns.lineplot(min_max_data, min_max_data, color="b", label="")
if print_r_squared:
p1.text(min_max_data[0], min_max_data[1]*0.9, '$R^2$={0:.2f}'.format(self.r_squared()))
plt.legend(loc='lower right')
else:
plt.legend()
ax.set(xlabel='Measured', ylabel='Predicted')
plt.show()
if save_path is not None:
plt.savefig(save_path, bbox_inches='tight')
return None
def r_squared(self):
"""r-squared value comparing measured and predicted rare earth composition"""
phases_copy = self._phases.copy()
mix = ct.Mixture(phases_copy)
aq_ind = self._aq_ind
exp_df = self._exp_df
in_moles = self._in_moles
rare_earth_ion_name = self._rare_earth_ion_name
pred = []
for row in in_moles.values:
mix.species_moles = row
mix.equilibrate('TP', log_level=0)
re_aq = mix.species_moles[mix.species_index(
aq_ind, rare_earth_ion_name)]
pred.append(re_aq)
predicted_y = np.array(pred)
actual_y = exp_df.values[:, 1]
num = sum((actual_y - predicted_y) ** 2)
den = sum((actual_y - np.mean(actual_y)) ** 2)
r_2 = (1 - num / den)
return r_2

@ -2,11 +2,23 @@ from setuptools import setup
setup(
name='reeps',
version='1.0.0',
packages=['tests'],
version='0.0.0',
packages=['reeps'],
package_data={
'csvs': ['data/csvs/*.csv'],
'xmls': ['data/xmls/*.xml']
},
include_package_data=True,
zip_safe=False,
url='',
license='',
author='Titus Quah',
author_email='',
description='Rare earth element parameter searcher'
description='Rare earth element parameter searcher',
install_requires=['cantera==2.4.0',
'pandas==1.0.3',
'numpy==1.15.4',
'scipy==1.4.1',
'seaborn==0.10.1',
'matplotlib==3.1.3']
)

@ -1,33 +1,15 @@
import json
import numpy as np
import pyswarms as ps
import sys
sys.path.append('../')
from reeps import REEPS1
from reeps import REEPS
with open('multi_ree_settings.txt') as file:
testing_params = json.load(file)
beaker = REEPS1(**testing_params)
# def new_obj(predicted_dict, meas_df, epsilon):
# meas_cols = list(meas_df)
# pred_keys = list(predicted_dict.keys())
# meas = meas_df[meas_cols[2]]
# pred = (predicted_dict['re_org'] + epsilon) / (predicted_dict['re_aq'] + epsilon)
# log_pred = np.log10(pred)
# log_meas = np.log10(meas)
# obj = np.sum((log_pred - log_meas) ** 2)
# return obj
# #
# #
# # def new_obj(ping):
# # print(ping)
# beaker.set_objective_function(new_obj)
# objective_kwargs = {"epsilon": 1e-14}
# beaker.set
# noinspection PyUnusedLocal
beaker = REEPS(**testing_params)
def optimizer(func, x_guess):
lb = np.array([1e-1])
ub = np.array([1e1])
@ -48,6 +30,6 @@ minimizer_kwargs = {"method": 'SLSQP',
est_enthalpy = beaker.fit()
print(est_enthalpy)
# beaker.update_xml(est_enthalpy)
# beaker.parity_plot()
# print(beaker.r_squared())
beaker.update_xml(est_enthalpy)
# beaker.parity_plot('Nd_d_eq', print_r_squared=True)
print(beaker.r_squared())

@ -1,14 +1,14 @@
import json
from unittest import TestCase
import numpy as np
import pyswarms as ps
import sys
sys.path.append('../')
from reeps import REEPS
from reeps import REEPS1
with open('one_ree_settings.txt') as file:
testing_params = json.load(file)
beaker = REEPS(**testing_params)
beaker = REEPS1(**testing_params)
# def new_obj(predicted_dict, meas_df, epsilon):
@ -48,6 +48,118 @@ minimizer_kwargs = {"method": 'SLSQP',
est_enthalpy = beaker.fit()
print(est_enthalpy)
# beaker.update_xml(est_enthalpy)
beaker.update_xml(est_enthalpy)
# beaker.parity_plot()
# print(beaker.r_squared())
print(beaker.r_squared())
# class TestREEPS1(TestCase):
# def test_slsqp_optimizer(self):
# self.fail()
#
# def test_log_mean_squared_error(self):
# self.fail()
#
# def test_get_exp_df(self):
# self.fail()
#
# def test_set_exp_df(self):
# self.fail()
#
# def test_get_phases(self):
# self.fail()
#
# def test_set_phases(self):
# self.fail()
#
# def test_get_opt_dict(self):
# self.fail()
#
# def test_set_opt_dict(self):
# self.fail()
#
# def test_get_aq_solvent_name(self):
# self.fail()
#
# def test_set_aq_solvent_name(self):
# self.fail()
#
# def test_get_extractant_name(self):
# self.fail()
#
# def test_set_extractant_name(self):
# self.fail()
#
# def test_get_diluant_name(self):
# self.fail()
#
# def test_set_diluant_name(self):
# self.fail()
#
# def test_get_complex_names(self):
# self.fail()
#
# def test_set_complex_names(self):
# self.fail()
#
# def test_get_rare_earth_ion_names(self):
# self.fail()
#
# def test_set_rare_earth_ion_names(self):
# self.fail()
#
# def test_get_aq_solvent_rho(self):
# self.fail()
#
# def test_set_aq_solvent_rho(self):
# self.fail()
#
# def test_get_extractant_rho(self):
# self.fail()
#
# def test_set_extractant_rho(self):
# self.fail()
#
# def test_get_diluant_rho(self):
# self.fail()
#
# def test_set_diluant_rho(self):
# self.fail()
#
# def test_set_in_moles(self):
# self.fail()
#
# def test_get_in_moles(self):
# self.fail()
#
# def test_set_objective_function(self):
# self.fail()
#
# def test_get_objective_function(self):
# self.fail()
#
# def test_set_optimizer(self):
# self.fail()
#
# def test_get_optimizer(self):
# self.fail()
#
# def test_update_predicted_dict(self):
# self.fail()
#
# def test_get_predicted_dict(self):
# self.fail()
#
# def test__internal_objective(self):
# self.fail()
#
# def test_fit(self):
# self.fail()
#
# def test_update_xml(self):
# self.fail()
#
# def test_parity_plot(self):
# self.fail()
#
# def test_r_squared(self):
# self.fail()

Loading…
Cancel
Save