Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correcting path construction #195

Closed
wants to merge 9 commits into from
11 changes: 10 additions & 1 deletion src/acom_music_box/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def from_config_JSON(

initial_conditions_path = os.path.dirname(
path_to_json) + "/" + list(config_JSON['initial conditions'].keys())[0]
logger.info("initial_conditions_pathh = {}".format(initial_conditions_path))
reaction_rates = Conditions.read_initial_rates_from_file(
initial_conditions_path, reaction_list)

Expand Down Expand Up @@ -212,6 +213,7 @@ def read_initial_rates_from_file(cls, file_path, reaction_list):

reaction_rates = []

logger.info("Conditions file_pathhh = {}".format(file_path))
with open(file_path, 'r') as csv_file:
initial_conditions = list(csv.reader(csv_file))

Expand Down Expand Up @@ -293,12 +295,19 @@ def update_conditions(self, new_conditions):
self.pressure = new_conditions.pressure
if new_conditions.temperature is not None:
self.temperature = new_conditions.temperature
logger.info("Before self.species_concentrations = {}".format(self.species_concentrations))
for conc in new_conditions.species_concentrations:
logger.info("concc = {}".format(vars(conc)))
match = filter(
lambda x: x.species.name == conc.species.name,
self.species_concentrations)
for item in list(match):

match = list(match)
logger.info("Before matchh = {}".format(match))
for item in match:
item.concentration = conc.concentration
logger.info("After matchh = {}".format(match))
logger.info("After self.species_concentrations = {}".format(self.species_concentrations))

for rate in new_conditions.reaction_rates:

Expand Down
24 changes: 17 additions & 7 deletions src/acom_music_box/evolving_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from .species_concentration import SpeciesConcentration
from .reaction_rate import ReactionRate

import logging
logger = logging.getLogger(__name__)


class EvolvingConditions:
"""
Expand Down Expand Up @@ -129,11 +132,11 @@ def from_config_JSON(
if 'evolving conditions' in config_JSON:
if len(config_JSON['evolving conditions'].keys()) > 0:
# Construct the path to the evolving conditions file
evolving_conditions_path = (
os.path.dirname(path_to_json) +
"/" +
list(
config_JSON['evolving conditions'].keys())[0])

evolving_conditions_path = os.path.join(
os.path.dirname(path_to_json),
list(config_JSON['evolving conditions'].keys())[0])

evolving_conditions = EvolvingConditions.read_conditions_from_file(
evolving_conditions_path, species_list, reaction_list)

Expand Down Expand Up @@ -163,6 +166,7 @@ def read_conditions_from_file(cls, file_path, species_list, reaction_list):
conditions = []

# Open the evolving conditions file and read it as a CSV
logger.info("Evolving conditions file_pathhh = {}".format(file_path))
with open(file_path, 'r') as csv_file:
evolving_conditions = list(csv.reader(csv_file))

Expand All @@ -173,6 +177,7 @@ def read_conditions_from_file(cls, file_path, species_list, reaction_list):
# Iterate over the remaining rows of the CSV
for i in range(1, len(evolving_conditions)):
# The first column of each row is a time value
logger.info("first columnn = {}".format(evolving_conditions[i][0]))
times.append(float(evolving_conditions[i][0]))

# Initialize pressure and temperature as None
Expand All @@ -197,13 +202,18 @@ def read_conditions_from_file(cls, file_path, species_list, reaction_list):
# For each concentration header, find the matching species
# and append its concentration to the list
for j in range(len(concentration_headers)):
speciesName = concentration_headers[j].split('.')[1]
match = filter(
lambda x: x.name == concentration_headers[j].split('.')[1],
species_list.species)
lambda x: x.name == speciesName, species_list.species)
species = next(match, None)
concentration = float(
evolving_conditions[i][headers.index(concentration_headers[j])])

logger.info("speciess = {} {}".format(speciesName, species))
if not species:
logger.warning("Unrecognized species {} in {}"
.format(speciesName, file_path))
continue
concentrations.append(
SpeciesConcentration(
species, concentration))
Expand Down
3 changes: 2 additions & 1 deletion src/acom_music_box/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def main():

# create and load a MusicBox object
myBox = MusicBox()
logging.info("configuration file = {}".format(musicBoxConfigFile))
myBox.readConditionsFromJson(musicBoxConfigFile)
logger.info("myBox = {}".format(myBox))

Expand All @@ -87,7 +88,7 @@ def main():
myBox.create_solver(campConfig)
logger.info("myBox.solver = {}".format(myBox.solver))
mySolution = myBox.solve(os.path.join(musicBoxOutputDir, "mySolution.csv"))
logger.info("mySolution = {}".format(mySolution))
# logger.info("mySolution = {}".format(mySolution))

logger.info("End time: {}".format(datetime.datetime.now()))
sys.exit(0)
Expand Down
61 changes: 59 additions & 2 deletions src/acom_music_box/music_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ def solve(self, output_path=None):
# runs the simulation at each timestep

while (curr_time <= self.box_model_options.simulation_length):
logger.info("curr_time = {}".format(curr_time))

# outputs to output_array if enough time has elapsed
if (next_output_time <= curr_time):
Expand All @@ -516,6 +517,7 @@ def solve(self, output_path=None):
next_conditions is not None and next_conditions_time <= curr_time):

curr_conditions.update_conditions(next_conditions)
logger.info("Updatedd conditionss = {}".format(curr_conditions.species_concentrations))

# iterates next_conditions if there are remaining evolving
# conditions
Expand Down Expand Up @@ -631,6 +633,8 @@ def readFromUIJsonString(self, data):
# Set evolving conditions
self.evolving_conditions = EvolvingConditions.from_UI_JSON(
data, self.species_list, self.reaction_list)

return

def readConditionsFromJson(self, path_to_json):
"""
Expand Down Expand Up @@ -665,6 +669,51 @@ def readConditionsFromJson(self, path_to_json):
# Set initial conditions
self.evolving_conditions = EvolvingConditions.from_config_JSON(
path_to_json, data, self.species_list, self.reaction_list)

# read initial concentrations from CSV
initial_concentrations_csv = self.readSetInitialConcentrations(data, path_to_json)

# append csv values to self.initial_conditions.species_concentrations
# CSV values take precedence over JSON values by overwriting them.
if (True):
for concentration in self.initial_conditions.species_concentrations:
logger.info("concentrationn = {}".format(concentration))
self.initial_conditions.species_concentrations.extend(initial_concentrations_csv)

return


def readSetInitialConcentrations(self, config_JSON, path_to_json):
"""
Retrieves initial concentrations from CSV file and assigns to vars.

Args:
config_JSON = already loaded and parsed.
Look here for "initial conditions".

Returns:
"""
if (not 'initial conditions' in config_JSON):
return([])
if (len(list(config_JSON['initial conditions'].keys())) == 0):
return([])

initial_conditions_path = os.path.dirname(
path_to_json) + "/" + list(config_JSON['initial conditions'].keys())[0]
logger.info("initial_conditions_pathh = {}".format(initial_conditions_path))
initial_concentrations = EvolvingConditions.read_conditions_from_file(
initial_conditions_path, self.species_list, self.reaction_list)
logger.info("Initial_concentrationss = {}"
.format(initial_concentrations.conditions[0].species_concentrations))

# caller will assign initial concentrations to the variables
specValues = initial_concentrations.conditions[0].species_concentrations
if (True):
for specValue in specValues:
logger.info("Initial {} from CSV".format(specValue))

return(specValues)


def speciesOrdering(self):
"""
Expand Down Expand Up @@ -705,18 +754,25 @@ def order_reaction_rates(self, curr_conditions, rate_constant_ordering):
"""
rate_constants = {}
for rate in curr_conditions.reaction_rates:
if (not rate.reaction):
continue

key = None
if (rate.reaction.reaction_type == "PHOTOLYSIS"):
key = "PHOTO." + rate.reaction.name
elif (rate.reaction.reaction_type == "FIRST_ORDER_LOSS"):
key = "LOSS." + rate.reaction.name
elif (rate.reaction.reaction_type == "EMISSION"):
key = "EMIS." + rate.reaction.name
rate_constants[key] = rate.rate
logger.info("keyy = {} rate.ratee = {}".format(key, rate.rate))

if key:
rate_constants[key] = rate.rate

ordered_rate_constants = len(rate_constants.keys()) * [0.0]
ordered_rate_constants = (len(rate_constants.keys()) + 1) * [0.0] # bogus + 1
for key, value in rate_constants.items():
ordered_rate_constants[rate_constant_ordering[key]] = float(value)

return ordered_rate_constants

@classmethod
Expand All @@ -732,5 +788,6 @@ def order_species_concentrations(
ordered_concentrations = len(concentrations.keys()) * [0.0]

for key, value in concentrations.items():
logger.info("keyy = {} valuee = {}".format(key, value))
ordered_concentrations[species_constant_ordering[key]] = value
return ordered_concentrations
5 changes: 3 additions & 2 deletions src/acom_music_box/reaction_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def from_config_JSON(cls, path_to_json, config_JSON, species_list):
list_name = None

# gets config file path
config_file_path = os.path.dirname(
path_to_json) + "/" + config_JSON['model components'][0]['configuration file']
config_file_path = os.path.join(
os.path.dirname(path_to_json),
config_JSON['model components'][0]['configuration file'])

# opnens config path to read reaction file
with open(config_file_path, 'r') as json_file:
Expand Down
6 changes: 6 additions & 0 deletions tests/configs/ts1_config/camp_data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"camp-files": [
"species.json",
"reactions.json"
]
}
Loading
Loading