mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Batchrunner does not handle exotic input parameters?

Open jwalzberg opened this issue 2 years ago • 9 comments

My model class takes inputs such as below:

class ExampleModel(Model): def init(self, seed: int = 0, input_data: Type[pd.DataFrame] = pd.read_csv('InputData.csv'), scenario: str = 'baseline', social_network_parameters: dict = { "average_degree": 10, "rewiring_probability": 0.1}, year_period: int = 30, etc. ) -> None:

It would be great if we could have a way to iterate through a list of strings for instance. In my case, the scenario parameter will select part of the Data Frame input based on the scenario string that is given. Ideally, I would like to give batch_run a list such as: ['baseline', 'scenario1', 'scenario2'].

It seems that because the batch_run function wants to launch batches based on iterables it takes the name of my first Data Frame column as an input for the input_data parameter or the first letter of my string (i.e., 'b' in 'baseline' for the example above) for the scenario parameter.

Although it was already not possible to do too many exotic things with input parameters to iterate through in the old BatchRunner classes, at least we could have strings, dictionaries, and so on as input parameters because of the "fixed_parameters" input of the BatchRunner classes. Now it seems batch_run will base its batch of simulations based on any iterable. I am not sure how that could be fixed but I think it could improve Mesa's functionalities.

PS: I do love the simplicity of the new batch_run function, so I am not advocating going back to the old BatchRunner classes but just improving batch_run.

jwalzberg avatar Apr 14 '22 16:04 jwalzberg

I found a workaround: treat the batch_run parameters input as a "variable_parameters" input. In other words, only input your variable parameters (e.g., in a list) and have the fixed parameters as default inputs of the class. (In my case I would have "parameters={['baseline', 'scenario1', 'scenario2']}" and I set the other parameters as the default of the model class. But maybe there is a better way?

jwalzberg avatar Apr 14 '22 16:04 jwalzberg

I think being able to pass in a DataFrame as params to batch_run is a useful feature.

rht avatar Apr 20 '22 04:04 rht

Hi @jwalzberg. I have the same type of issue, a str passed as a parameter to batch_run is then interpreted character by character, not ideal! Some parameters might not be able to be put as default args in the constructor of the model class. Then what is best practice? The same thing goes with @rht 's argument of Dataframes. I think these are important additions to the library!

Axeln78 avatar Apr 22 '22 11:04 Axeln78

I have the same type of issue, a str passed as a parameter to batch_run is then interpreted character by character, not ideal!

I actually don't fully understand this problem. Shouldn't it work if you wrap the string inside a list? Here is an example

from mesa.batchrunner import _make_model_kwargs
print(_make_model_kwargs({"key": "value"}))
print(_make_model_kwargs({"key": ["value"]}))

which outputs

[{'key': 'v'}, {'key': 'a'}, {'key': 'l'}, {'key': 'u'}, {'key': 'e'}]
[{'key': 'value'}]

rht avatar Apr 22 '22 12:04 rht

[{'key': 'v'}, {'key': 'a'}, {'key': 'l'}, {'key': 'u'}, {'key': 'e'}]

This is definitely a gotcha, but this is something that can be quickly fixed (into Mesa main branch).

rht avatar Apr 22 '22 12:04 rht

With #1289 (just merged), the output will be [{'key': 'value'}] instead of [{'key': 'v'}, {'key': 'a'}, {'key': 'l'}, {'key': 'u'}, {'key': 'e'}]. However, since the DataFrame as an input is not yet implemented, this issue must remain open.

rht avatar Apr 23 '22 10:04 rht

Thanks a lot @rht !

Axeln78 avatar Apr 27 '22 13:04 Axeln78

Is there any timeline for the implementation of DataFrames as inputs?

Axeln78 avatar Apr 29 '22 08:04 Axeln78

I'd prioritize implementing #1263 over this issue, but you are welcome to help to implement the DF input.

rht avatar May 01 '22 02:05 rht