.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_data_cube_formula.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_data_cube_formula.py: .. _l-plot-data-cube-formula: Add a variable computed with a formula ====================================== The logs rarely contain every metric needed to compare experiments. Argument ``formulas`` of :class:`data_cube.DataCube` adds new columns when the data is loaded. Every formula is a function receiving a :class:`pandas.DataFrame` (the data loaded so far) and returning a series. The new columns are values: they can be shown in views and aggregated like any other value. Data ++++ Every row is one model exported with one exporter on one day. The latency of the exported model and the latency of the original model (the baseline) are measured, as well as the peak memory. One export failed (no latency) and one exported model is slower than the baseline. .. GENERATED FROM PYTHON SOURCE LINES 22-49 .. code-block:: Python import io import textwrap import numpy as np import pandas import matplotlib.pyplot as plt from data_cube import DataCube, CubeViewDef pandas.set_option("display.width", 250) pandas.set_option("display.max_columns", 30) df = pandas.read_csv(io.StringIO(textwrap.dedent(""" date,model_name,exporter,time_latency,time_baseline,mem_peak 2025/01/01,phi3,export,0.10,0.15,1200 2025/01/01,phi3,dynamo,0.12,0.15,1100 2025/01/01,phi4,export,0.20,0.26,2300 2025/01/01,phi4,dynamo,,0.26, 2025/01/01,llama,export,0.31,0.35,4100 2025/01/01,llama,dynamo,0.29,0.35,3900 2025/01/02,phi3,export,0.09,0.15,1210 2025/01/02,phi3,dynamo,0.11,0.15,1090 2025/01/02,phi4,export,0.21,0.26,2280 2025/01/02,phi4,dynamo,0.19,0.26,2250 2025/01/02,llama,export,0.38,0.35,4150 2025/01/02,llama,dynamo,0.28,0.35,3870 """))) .. GENERATED FROM PYTHON SOURCE LINES 50-61 Formulas ++++++++ Three variables are added: * ``speedup``: the baseline latency divided by the latency, * ``speedup_increase``: the relative gain in percent, it reuses ``speedup``, formulas are applied in the order of the dictionary and every formula sees the columns added by the previous ones, * ``faster``: 1 if the exported model is faster than the baseline, 0 if it is not, missing if the export failed. .. GENERATED FROM PYTHON SOURCE LINES 61-84 .. code-block:: Python def faster(data: pandas.DataFrame) -> pandas.Series: res = (data["time_latency"] < data["time_baseline"]).astype(float) res[data["time_latency"].isna()] = np.nan return res cube = DataCube( df, time="date", keys=["model_name", "exporter"], values=["time_.*", "mem_.*"], formulas={ "speedup": lambda d: d["time_baseline"] / d["time_latency"], "speedup_increase": lambda d: (d["speedup"] - 1) * 100, "faster": faster, }, ).load() print(cube.values) print(cube.data[["date", "model_name", "exporter", "speedup", "speedup_increase", "faster"]]) .. rst-class:: sphx-glr-script-out .. code-block:: none ['mem_peak', 'time_baseline', 'time_latency', 'speedup', 'speedup_increase', 'faster'] date model_name exporter speedup speedup_increase faster 0 2025-01-01 phi3 export 1.500000 50.000000 1.0 1 2025-01-01 phi3 dynamo 1.250000 25.000000 1.0 2 2025-01-01 phi4 export 1.300000 30.000000 1.0 3 2025-01-01 phi4 dynamo NaN NaN NaN 4 2025-01-01 llama export 1.129032 12.903226 1.0 5 2025-01-01 llama dynamo 1.206897 20.689655 1.0 6 2025-01-02 phi3 export 1.666667 66.666667 1.0 7 2025-01-02 phi3 dynamo 1.363636 36.363636 1.0 8 2025-01-02 phi4 export 1.238095 23.809524 1.0 9 2025-01-02 phi4 dynamo 1.368421 36.842105 1.0 10 2025-01-02 llama export 0.921053 -7.894737 0.0 11 2025-01-02 llama dynamo 1.250000 25.000000 1.0 .. GENERATED FROM PYTHON SOURCE LINES 85-86 The new variables are described like the others. .. GENERATED FROM PYTHON SOURCE LINES 86-89 .. code-block:: Python print(cube.describe()) .. rst-class:: sphx-glr-script-out .. code-block:: none dtype missing kind count min max mean sum n_values values name date datetime64[ns] 0 time 12 2025-01-01 00:00:00 2025-01-02 00:00:00 NaN NaN 2 2025-01-01 00:00:00 - 2025-01-02 00:00:00 model_name object 0 keys 12 NaN NaN NaN NaN 3 llama,phi3,phi4 exporter object 0 keys 12 NaN NaN NaN NaN 2 dynamo,export time_latency float64 1 values 11 0.09 0.38 0.207273 2.280000 11 NaN time_baseline float64 0 values 12 0.15 0.35 0.253333 3.040000 3 NaN mem_peak float64 1 values 11 1090.0 4150.0 2495.454545 27450.000000 11 NaN speedup float64 1 values 11 0.9210526315789473 1.6666666666666667 1.290346 14.193801 11 NaN speedup_increase float64 1 values 11 -7.8947368421052655 66.66666666666667 29.034552 319.380076 11 NaN faster float64 1 values 11 0.0 1.0 0.909091 10.000000 2 NaN .. GENERATED FROM PYTHON SOURCE LINES 90-94 A view on a formula +++++++++++++++++++ The models are the rows, the exporters and dates are the columns. .. GENERATED FROM PYTHON SOURCE LINES 94-98 .. code-block:: Python view = cube.view(CubeViewDef(key_index=["model_name"], values=["speedup_increase"])) print(view) .. rst-class:: sphx-glr-script-out .. code-block:: none METRICS speedup_increase exporter dynamo export date 2025-01-01 2025-01-02 2025-01-01 2025-01-02 model_name llama 20.689655 25.000000 12.903226 -7.894737 phi3 25.000000 36.363636 50.000000 66.666667 phi4 NaN 36.842105 30.000000 23.809524 .. GENERATED FROM PYTHON SOURCE LINES 99-104 Aggregation +++++++++++ The speedup is averaged over the models and ``faster`` is summed: it counts the models faster than the baseline. .. GENERATED FROM PYTHON SOURCE LINES 104-115 .. code-block:: Python agg = cube.view( CubeViewDef( key_index=["exporter"], values=["speedup", "faster"], key_agg=["model_name"], agg_args=lambda name: "sum" if name == "faster" else "mean", ) ) print(agg) .. rst-class:: sphx-glr-script-out .. code-block:: none date 2025-01-01 2025-01-02 exporter METRICS dynamo faster 2.000000 3.000000 speedup 1.228448 1.327352 export faster 3.000000 2.000000 speedup 1.309677 1.275272 .. GENERATED FROM PYTHON SOURCE LINES 116-118 Plot ++++ .. GENERATED FROM PYTHON SOURCE LINES 118-123 .. code-block:: Python ts = cube.view(CubeViewDef(key_index=["model_name", "exporter"], values=["speedup"])) ax = ts.T.droplevel(0).plot(title="speedup over time", marker="o") ax.set_ylabel("baseline / latency") plt.tight_layout() .. image-sg:: /auto_examples/images/sphx_glr_plot_data_cube_formula_001.png :alt: speedup over time :srcset: /auto_examples/images/sphx_glr_plot_data_cube_formula_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.159 seconds) .. _sphx_glr_download_auto_examples_plot_data_cube_formula.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_data_cube_formula.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_data_cube_formula.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_data_cube_formula.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_