{ "cells": [ { "cell_type": "markdown", "id": "7cc22fcd", "metadata": {}, "source": [ "## Parameter Estimation (import / export)\n", "This example describes how to export the parameter estimation setup to a YaML file (or string), so that it can be easily modified later, and then loaded back (or applied to a different model). \n", "\n", "### The Format\n", "When saving the file, it will be exported as a sequence, of experiments of the following form: \n", "\n", "```\n", "name: Experiment\n", " filename: data.txt\n", " type: Time-Course\n", " separator: \"\\t\"\n", " first_row: 1\n", " last_row: 102\n", " header_row: 1\n", " weight_method: Mean Square\n", " normalize_per_experiment: true\n", " mapping:\n", " - column: '# Time'\n", " type: time\n", " - column: Values[F16BP_obs]\n", " type: dependent\n", " cn: CN=Root, ...\n", " object: '[Fru1,6-P2]'\n", "```\n", "\n", "the individual fields are: \n", "\n", "* `name`: the name of the experiment\n", "* `type`: `Time-Course` for time course data (requires a mapping of type `time` to be specified), or `Steady-State` for steady state data. \n", "* `separator`: the separator being used\n", "* `first_row`: the beginning of the experiment in the file (1 based)\n", "* `last_row`: the last row of the experiment \n", "* `header_row`: (optional) row with header information that can be later used in the `column` field of the mappings. \n", "* `weight_method`: one of: `Mean`, `Mean Square`, `Standard Deviation` or `Value Scaling`.\n", "* `normalize_per_experiment`: boolean indicating whether experiments should be scaled individual (`True`) or over all defined experimentes (`False`). \n", "* `mapping`: sequence of column mappings described as follows. \n", "\n", "The mapping descriptions contain the fields: \n", "\n", "* `column`: either an integer index (zero based), describing which column the mapping applies to. If the experiment has header information, the column may be a string with the (case sensitive) header the mapping applies to. \n", "* `type`: the type of the mapping for this column. One of `ignored`, `time`, `dependent` or `independent`.\n", "* `object`: display name of the element to map to\n", "* `cn`: (optional) the CN to the reference to map to.\n", " \n", " For columns of type `dependent` or `independent` at least on of `object` or `cn` needs to be defined. (The `cn` value takes preference). \n", "\n", "* `weight`: may be used for columns of type `dependent` to customize the scale to be applied to the column. If not specified it will be automatically calculated based on the selected `weight_method`. \n", "\n", "### Example\n", "We start by importing basico as usual (should that fail for you just `!pip install copasi-basico`: " ] }, { "cell_type": "code", "execution_count": 1, "id": "d132bc0b", "metadata": {}, "outputs": [], "source": [ "from basico import *" ] }, { "cell_type": "markdown", "id": "9b012d4c", "metadata": {}, "source": [ "here we load an existing parameter estimation example, included with the distribution as example: " ] }, { "cell_type": "code", "execution_count": 2, "id": "30aedd66", "metadata": {}, "outputs": [], "source": [ "dm = load_example('PK')" ] }, { "cell_type": "markdown", "id": "c6cd86aa", "metadata": {}, "source": [ "now we can directly export the setup of the experimental data files as yaml string (or if you supply a filename to the function, it will be saved as file): " ] }, { "cell_type": "code", "execution_count": 3, "id": "55f44c03", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- name: Experiment\n", " filename: e:/development/basico/basico/data\\data_2.txt\n", " type: Time-Course\n", " separator: \"\\t\"\n", " first_row: 1\n", " last_row: 102\n", " weight_method: Mean Square\n", " normalize_per_experiment: true\n", " header_row: 1\n", " mapping:\n", " - column: '# Time'\n", " type: time\n", " - column: Values[F16BP_obs]\n", " type: dependent\n", " cn: CN=Root,Model=Pritchard2002_glycolysis,Vector=Compartments[cytosol],Vector=Metabolites[Fru1\\,6-P2],Reference=Concentration\n", " object: '[Fru1,6-P2]'\n", " - column: Values[Glu_obs]\n", " type: dependent\n", " cn: CN=Root,Model=Pritchard2002_glycolysis,Vector=Compartments[cytosol],Vector=Metabolites[Glc(int)],Reference=Concentration\n", " object: '[Glc(int)]'\n", " - column: Values[Pyr_obs]\n", " type: dependent\n", " cn: CN=Root,Model=Pritchard2002_glycolysis,Vector=Compartments[cytosol],Vector=Metabolites[pyruvate],Reference=Concentration\n", " object: '[pyruvate]'\n" ] } ], "source": [ "yaml_str = save_experiments_to_yaml()\n", "print(\"\\n\".join(yaml_str.split('\\n')[:24])) # just restricting the amount of yaml to be printed here to the first experiment" ] }, { "cell_type": "markdown", "id": "88a132cf", "metadata": {}, "source": [ "at this point you'd make modifications to you'd want to it. Remember, that the `cn` is optional. The key points to keep in mind: \n", "\n", "* ensure that the row number for the experiment are consistent with the changes you make\n", "* if it is a time course experiment, ensure you have a column of type `time`. \n", "* the column specifier is either the index of the column, or the name if the experiment has headers. " ] }, { "cell_type": "markdown", "id": "353f3988", "metadata": {}, "source": [ "once the changes are made, you can load the setup back into the model, using the `load_experiments_from_yaml` function. This will remove all existing experiments from the file first. " ] }, { "cell_type": "code", "execution_count": 4, "id": "7aa6c837", "metadata": {}, "outputs": [], "source": [ "load_experiments_from_yaml(yaml_str)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 5 }