Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Edit TOML on the fly
#1
I would like to edit a local TOML file and save it again to be used in the same Python script. In this sense, to be able to change a given parameter in loop. You can see an example of file, here.

https://bitbucket.org/robmoss/particle-f...ation.toml

So far, I could load the file but I don't find how to change a parameter value.
import toml
data = toml.load("scenario.toml")
buran write Jan-27-2021, 02:07 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
What do you want to change? e.g. with the file from your link, after load, you get nested dict structure like this

Output:
{'components': {'model': 'pypfilt.examples.predation.LotkaVolterra', 'summary': 'pypfilt.summary.HDF5', 'time': 'pypfilt.Scalar'}, 'model': {'bounds': {'alpha': {'max': 2, 'min': 0}, 'beta': {'max': 2, 'min': 0}, 'delta': {'max': 2, 'min': 0}, 'gamma': {'max': 2, 'min': 0}, 'x': {'max': 2, 'min': 0}, 'y': {'max': 2, 'min': 0}}, 'priors': {'alpha': {'args': {'high': 0.8, 'low': 0.6}, 'function': 'uniform'}, 'beta': {'args': {'high': 1.4, 'low': 1.2}, 'function': 'uniform'}, 'delta': {'args': {'high': 1.1, 'low': 0.9}, 'function': 'uniform'}, 'gamma': {'args': {'high': 1.1, 'low': 0.9}, 'function': 'uniform'}, 'x': {'args': {'high': 1.5, 'low': 0.5}, 'function': 'uniform'}, 'y': {'args': {'high': 0.4, 'low': 0.2}, 'function': 'uniform'}}}, 'parameters': {'data_dir': '.', 'fresh_cache': True, 'max_days': 14, 'out_dir': '.', 'particles': 1000, 'prng_seed': 42, 'remove_cache': True, 'resample': {'regularisation': True, 'threshold': 0.25}, 'steps_per_unit': 1, 'summary': {'from_first_day': True, 'meta': {'packages': ['matplotlib']}, 'tables': {'model_cints': {'credible_intervals': [0, 50, 95]}}}, 'time': {'start': 0.0, 'until': 15.0}}, 'scenario': {'example': {'name': 'Example Scenario', 'observations': {'x': {'file': 'predation-counts-x.ssv', 'format': {'sdev': '0.1f'}, 'init': {'obs_period': 0}, 'model': 'pypfilt.examples.predation.ObsModel', 'name': {'sdev': 'sdev'}, 'parameters': {'sdev': 0.2}}, 'y': {'file': 'predation-counts-y.ssv', 'format': {'sdev': '0.1f'}, 'init': {'obs_period': 0}, 'model': 'pypfilt.examples.predation.ObsModel', 'name': {'sdev': 'sdev'}, 'parameters': {'sdev': 0.2}}}}}, 'summary': {'monitors': {'expected_obs': {'model': 'pypfilt.summary.ExpectedObsMonitor'}}, 'tables': {'forecasts': {'init': {'exp_obs_monitor': 'expected_obs'}, 'model': 'pypfilt.summary.PredictiveCIs'}, 'model_cints': {'model': 'pypfilt.summary.ModelCIs'}, 'obs': {'model': 'pypfilt.summary.Obs'}}}}
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I would like to change all possible parameters since it will enrich the study. However, the main ones are particles, prng_seed and time (start and until).
Reply
#4
(Jan-27-2021, 03:05 PM)C3PO Wrote: I would like to change all possible parameters since it will enrich the study.
again, it's a nested structure - dicts of dicts (or lists). Do you know how to work with dicts?


e.g.

import toml
data = toml.load('scenario.toml')

print(data['parameters']['particles'])
data['parameters']['particles'] = 2000
print(data['parameters']['particles'])
Output:
1000 2000
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020