copasi / basico

A simplified python interface to COPASI.
https://basico.readthedocs.io/
Artistic License 2.0
19 stars 5 forks source link

Plots of steady-state experiments #23

Open DeepaMahm opened 2 years ago

DeepaMahm commented 2 years ago

Hello @fbergmann ,

I tried to make a few changes in the function def plot_per_dependent_variable to create plots for the steady-state data and I would like to share the modified function (please see below) with you.

Thanks so much for the new release, this helps me a lot in obtaining the steady-state data in the required format.

def plot_per_dependent_variable_ss(**kwargs):
    """
    This function creates a figure for each dependent variable, with traces for all experiments.

    :param kwargs:

    - | `model`: to specify the data model to be used (if not specified
      | the one from :func:`.get_current_model` will be taken)

    :return: array of tuples (fig, ax) for each figure created
    """
    dm = model_io.get_model_from_dict_or_default(kwargs)

    task = dm.getTask(TASK_PARAMETER_ESTIMATION)
    assert (isinstance(task, COPASI.CFitTask))

    problem = task.getProblem()
    assert (isinstance(problem, COPASI.CFitProblem))

    experiments = problem.getExperimentSet()
    assert (isinstance(experiments, COPASI.CExperimentSet))

    result = []
    num_experiments = experiments.getExperimentCount()
    if num_experiments == 0:
        return result

    data = get_simulation_results(**kwargs)
    exp_data = data[0]  # experimental data
    sim_data = data[1]  # simulation data

    dependent_variables = {}

    for i in range(num_experiments):
        experiment = experiments.getExperiment(i)
        mapping = get_experiment_mapping(experiment)

        # set independent values for that experiment
        independent = mapping[mapping.type == 'independent']
        xname = independent.iloc[0].mapping

        # set dependent values for that experiment
        dependent = mapping[mapping.type == 'dependent']
        num_dependent = dependent.shape[0]
        for j in range(num_dependent):
            name = dependent.iloc[j].mapping
            if name not in dependent_variables:
                dependent_variables[name] = []
            dependent_variables[name].append(i)

    for dependent in dependent_variables:
        fig, ax = plt.subplots()
        cycler = plt.cycler("color", plt.cm.tab20c.colors)()
        ax.set_title(dependent)
        experiment_indices = dependent_variables[dependent]

        for i in experiment_indices:
            experiment = experiments.getExperiment(i)
            exp_name = experiment.getObjectName()
            nextval = next(cycler)['color']
            name = dependent
            if name not in sim_data[i].columns:
                name = name[1:-1]

            sim_data[i][xname] = exp_data[i][xname]
            # print(sim_data[i])

            sim_data[i].reset_index().plot(x=xname, y=name,
                                           label="{0} Fit".format(exp_name), ax=ax, color=nextval)
            exp_data[i].plot.scatter(x=xname, y=dependent, ax=ax, color=nextval,
                                     label='{0} Measured'.format(exp_name))
        # plt.savefig('ss.png')
        result.append((fig, ax))

    return result
fbergmann commented 2 years ago

thank you so very much for this submission!

fbergmann commented 2 years ago

I'll leave this open for now, as i need to find a way to make this work:

i'll make commits onto this issue as i make progress.

DeepaMahm commented 2 years ago

Hi @fbergmann ,

I would like to suggest one more use case i.e steady state data with multiple independent data.

I look forward to use these implementations.

Thanks so much.