Wide To Long
Rewrites a wide Output.csv in the long layout. A wide file has one row for
each Run and variable, and one column for each time step. A long file has one
row for each time step, and a value column. The script keeps every context
column, so a later script can still tell the Runs and the datasets apart. It
does one task. It computes nothing.
The app bundles this script as Wide_To_Long.R and Wide_To_Long.py. Type the bare file name as a Script’s File, or pick it with Bundled scripts… on a Script row. A copy beside the Experiment file runs in place of the bundled one. See Run your own R or Python. Back to the Scripts.
Wide_To_Long.py needs python3 only. Wide_To_Long.R needs Rscript only.
Both write the same bytes from the same input, which the test suite checks.
Requirements
| Language | Needs |
|---|---|
| Python | Python 3.11 or later. No package. |
| R | R 4.3 or later. No package. |
The test suite runs on these versions in GitHub Actions. Older versions are not tested.
Inputs and Outputs
| Input | Source | Read as |
|---|---|---|
| The folder | First command-line argument, then EXPERIMENTER_OUTPUT_DIR | The folder that holds Output.csv |
| The file name | EXPERIMENTER_OUTPUT_NAME, else Output.csv, then output.csv | The collated output file |
| The layout | EXPERIMENTER_OUTPUT_LAYOUT, else the header | wide, or long when a value column exists |
| The time column name | EXPERIMENTER_TIME_COLUMN, else Time | The header of the new time column |
| The dataset | EXPERIMENTER_DATASET | Part of the output file name, empty outside a sweep |
| Output | Written to | Holds |
|---|---|---|
Wide_To_Long.csv | The folder | The long table |
Wide_To_Long_<dataset>.csv | The folder | The same table, when EXPERIMENTER_DATASET is set |
python3 Wide_To_Long.py /path/to/outputsCore Mechanics
The long file has the context columns the input uses, in the order Experimenter
writes them (sequence, dataset, level, group, source, source_kind, engine, started_at, sample),
then the
time column, variable and value. Every column of the wide file that is not
a context column or variable is a time step. Its header becomes the time.
Rows run Run by Run, in the order each Run first appears. Inside one Run they run by time step, then by variable. An empty cell in the wide file has no row in the long file. The script copies every cell as text, so a value reaches the long file as Experimenter wrote it, with no rounding.
A file that is already long is copied to the same output name without a
change. A chain that runs on both layouts then needs no branch. A
EXPERIMENTER_OUTPUT_LAYOUT value that does not match the file stops the
script.
Where It Runs
Put it in the run order below the Runs whose output it should convert. The
scripts that follow read Wide_To_Long.csv by name:
model: Model.stmx
runs:
fit: {}
to-long:
script: "python3 Wide_To_Long.py"Under a sweep, the script runs once for each dataset and writes
Wide_To_Long_<dataset>.csv into that dataset’s folder.
Collect_Datasets --stem Wide_To_Long stacks those files into
Wide_To_Long_All.csv. The chaining rules
set the file-name convention.
Likely Issues and Error Types
Every failure below stops the script with a non-zero exit and a message on standard error. No output file is written.
- No folder is given, or the folder does not exist.
- No
Output.csvis in the folder. Run the model first. EXPERIMENTER_OUTPUT_LAYOUTis notwideorlong, or it does not match the file.- The file has no
sourceorsource_kindcolumn. The message names the missing column. A file with aruncolumn and nosourceis an older layout, and the script refuses it. - The wide file has no
variablecolumn. It is not an ExperimenterOutput.csv. - The wide file has no time step column.
- A row has more or fewer cells than the header.
Version Policy
The script was written for Experimenter 0.5.0. It names no column except the
context columns and variable. Any other column of a wide file is a time step.