Skip to content

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

LanguageNeeds
PythonPython 3.11 or later. No package.
RR 4.3 or later. No package.

The test suite runs on these versions in GitHub Actions. Older versions are not tested.

Inputs and Outputs

InputSourceRead as
The folderFirst command-line argument, then EXPERIMENTER_OUTPUT_DIRThe folder that holds Output.csv
The file nameEXPERIMENTER_OUTPUT_NAME, else Output.csv, then output.csvThe collated output file
The layoutEXPERIMENTER_OUTPUT_LAYOUT, else the headerwide, or long when a value column exists
The time column nameEXPERIMENTER_TIME_COLUMN, else TimeThe header of the new time column
The datasetEXPERIMENTER_DATASETPart of the output file name, empty outside a sweep
OutputWritten toHolds
Wide_To_Long.csvThe folderThe long table
Wide_To_Long_<dataset>.csvThe folderThe same table, when EXPERIMENTER_DATASET is set
python3 Wide_To_Long.py /path/to/outputs

Core 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.csv is in the folder. Run the model first.
  • EXPERIMENTER_OUTPUT_LAYOUT is not wide or long, or it does not match the file.
  • The file has no source or source_kind column. The message names the missing column. A file with a run column and no source is an older layout, and the script refuses it.
  • The wide file has no variable column. It is not an Experimenter Output.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.