Skip to content

Blog#

Merging experiments

You don't always know all the parameters and variants you will evaluate upfront, or executing each run takes so much time you prefer to split the experiment into smaller ones.

In these cases, merging experiments and unifying their analysis is desirable. That's what we do in this example, merging experiments e1 and e2 into e12.

Warning

As we merge runs from different experiments, their runs might not be aligned anymore. E.g., a run might have a field named a but not b, and vice versa. You are in charge of managing these differences.

Tip

  • The runs attribute of Experiment objects is a specialized dictionary with run IDs as keys and Run objects as values. You can iterate, add, and remove runs with regular dict operations.
  • You can use the | and |= operators directly on experiments to merge them: e = e1 | e2.

Union of runs

from mltraq import create_experiment
from mltraq.steps.init_fields import init_fields

# First experiment
e1 = create_experiment().execute(init_fields(a=1, c=10))

# Second experiment
e2 = create_experiment().execute(init_fields(b=2, c=20))

# Merge of experiments `e1` and `e2`
e12 = create_experiment().merge_runs(e1.runs | e2.runs)

print("Experiment runs:")
print(e12.runs)
print("--\n")

print("Run fields:")
print(e12.runs.df())
Output
Experiment runs:
Runs(keys(2)=["d65df69e-1175-44a5-be2f-2232765703b9", "d65df69e-1175-44a5-be2f-2232765703bb"])
--

Run fields:
                                 id_run    a    b   c
0  d65df69e-1175-44a5-be2f-2232765703b9  1.0  NaN  10
1  d65df69e-1175-44a5-be2f-2232765703bb  NaN  2.0  20