Skip to content

(I002) setInputsCSV() - #447

Merged
adeas31 merged 2 commits into
OpenModelica:masterfrom
syntron:I002-setInputsCSV
Aug 27, 2026
Merged

(I002) setInputsCSV()#447
adeas31 merged 2 commits into
OpenModelica:masterfrom
syntron:I002-setInputsCSV

Conversation

@syntron

@syntron syntron commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Read content from a CSV file and use it to define the time based input data.

See issue #353 - currently completly untested from my side!

@FaBiasch could you please tests?

@syntron

syntron commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

from PR #404:

But what against the following code:

    mod = ModelicaSystemRunner(work_directory='.')
    mod.setup(model_name=modelName)
    mod.setInputsCSV(csvfile=csvfile)
    mod.simulate(resultfile=resfilepathname)

So this creates a CSV file (as input for the model executable) from a CSV file (for setInputsCSV) that the user needs to provide. I would try to avoid this workflow if possible.

  • it adds unnecessary overhead (which may become relevant when calling ModelicaSystemRunner in huge batches)
  • we need means to customize the location of the intermediate CSV file and users may need to take care about this.
  • users will end up checking two input files in case some things go wrong
  • users will need to take care for additional cleaning & deleting input files

So I would currently prefer setting csvInput in simargs as you proposed above @syntron . At least for ModelicaSystemRunner there shouldn't be a problem with WSL, Docker, k8s & co.

On the other hand, I also like the idea of having a simple pythonic interface for timeseries inputs in OMPython. What do you think about

  • mod.setInputsSeries(series=dict or list of dicts). E.g. series=
    [{'time': 0.0, 'col1': 1, 'col2': 0.5}, {'time': 0.25, 'col1': 2, 'col2': 0.75}, {'time': 0.5, 'col1': 2.1, 'col2': 0.8}]
  • or mod.setInputsDataframe(dataframe=dataframe)

I think this would fit the current workflow of OMPython well. And in case the interfaces to the model executables is changed again in a future version, the users wouldn't need to worry about it.

I would not do it via a pandas dataframe as this would add the dependency to OMPython; but using the dictionary would work - at the end, the current code just converts the CSV file to a dictionary to apply it to the existing data.

@joewa would this work for you?

@joewa

joewa commented Mar 20, 2026

Copy link
Copy Markdown

Yes, that would work for me @syntron .

When using tables / DataFrames this helper function proved to be useful too:

def toInputs(data: dict[str, list[float]]) -> dict[str, list[tuple[float, float]]]:
    """
    Converts a dictionary of lists (from pandas DataFrame.to_dict(orient='list'))
    into the OMPython setInputs input format.

    Example: mod.setInputs(**toInputs(pdf.to_dict(orient='list')))
    
    Assumes the dictionary contains a key named 'time'.
    """
    if "time" not in data:
        raise ValueError("The provided data must contain a 'time' key.")

    time_series = data["time"]

    inputs = {
        var_name: list(zip(time_series, values))
        for var_name, values in data.items()
        if var_name != "time"
    }

    return inputs

Shall we add it as well?

@syntron

syntron commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

Yes, that would work for me @syntron .

When using tables / DataFrames this helper function proved to be useful too:

def toInputs(data: dict[str, list[float]]) -> dict[str, list[tuple[float, float]]]:
    """
    Converts a dictionary of lists (from pandas DataFrame.to_dict(orient='list'))
    into the OMPython setInputs input format.

    Example: mod.setInputs(**toInputs(pdf.to_dict(orient='list')))
    
    Assumes the dictionary contains a key named 'time'.
    """
    if "time" not in data:
        raise ValueError("The provided data must contain a 'time' key.")

    time_series = data["time"]

    inputs = {
        var_name: list(zip(time_series, values))
        for var_name, values in data.items()
        if var_name != "time"
    }

    return inputs

Shall we add it as well?

Looks good to me! This can be added as a staticmethod to ModelicaSystemABC. The check for numeric values is done in setInputs() ...

syntron added a commit to syntron/OMPython that referenced this pull request Apr 1, 2026
@joewa

joewa commented Apr 6, 2026

Copy link
Copy Markdown

Perfect!

@syntron
syntron force-pushed the I002-setInputsCSV branch from 03a50ab to 7822295 Compare May 11, 2026 18:53
@syntron
syntron force-pushed the I002-setInputsCSV branch 4 times, most recently from f7d2e68 to 5354c7d Compare June 30, 2026 19:50
@syntron
syntron force-pushed the I002-setInputsCSV branch 3 times, most recently from 7e96fd2 to c9ff882 Compare August 14, 2026 17:04
@syntron
syntron force-pushed the I002-setInputsCSV branch from c9ff882 to d8a554b Compare August 21, 2026 19:07
@syntron
syntron force-pushed the I002-setInputsCSV branch from d8a554b to 3531b58 Compare August 26, 2026 16:46
@syntron
syntron marked this pull request as ready for review August 26, 2026 17:04
@adeas31
adeas31 merged commit 05bfffa into OpenModelica:master Aug 27, 2026
10 checks passed
@syntron
syntron deleted the I002-setInputsCSV branch August 31, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants