From 6ce52145876e5afaeaf8c3388b89fbcdd91efa55 Mon Sep 17 00:00:00 2001 From: Joris Snellenburg Date: Sat, 8 Jun 2024 12:02:08 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Port=20study=5Ffluorescence=20to=20not?= =?UTF-8?q?ebook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global_and_target_analysis.ipynb | 330 ++++++++++++++++++ 1 file changed, 330 insertions(+) create mode 100644 pyglotaran_examples/study_fluorescence/global_and_target_analysis.ipynb diff --git a/pyglotaran_examples/study_fluorescence/global_and_target_analysis.ipynb b/pyglotaran_examples/study_fluorescence/global_and_target_analysis.ipynb new file mode 100644 index 0000000..8b6e9b7 --- /dev/null +++ b/pyglotaran_examples/study_fluorescence/global_and_target_analysis.ipynb @@ -0,0 +1,330 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0", + "metadata": {}, + "source": [ + "# Global and target analysis of time-resolved fluorescence data\n", + "\n", + "From a single experiment on Photosystem I\n", + "\n", + "### Reference\n", + "\n", + "DOI: " + ] + }, + { + "cell_type": "markdown", + "id": "1", + "metadata": {}, + "source": [ + "### Imports" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "# imports\n", + "from pyglotaran_extras.io import setup_case_study\n", + "\n", + "from glotaran.io import load_dataset, load_model, load_parameters\n", + "from glotaran.optimization.optimize import optimize\n", + "from glotaran.project.scheme import Scheme" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "# extra import for plotting\n", + "import matplotlib.pyplot as plt\n", + "from pyglotaran_extras.plotting.plot_overview import plot_overview\n", + "from pyglotaran_extras.plotting.style import PlotStyle" + ] + }, + { + "cell_type": "markdown", + "id": "4", + "metadata": {}, + "source": [ + "### Location of data files\n", + "\n", + "Note that for the sake of reproduciblity we seperate the model used for global analysis from the model used for target analysis. \n", + "\n", + "We will use the same data files for both analyses.\n", + "\n", + "Note: when comparing the results with previous results, we only use at the target analysis." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "# paths\n", + "\n", + "GA_MODEL_PATH = \"models/model.yaml\"\n", + "GA_PARAMETERS_PATH = \"models/parameters.yaml\"\n", + "\n", + "results_folder, script_folder = setup_case_study(\n", + " output_folder_name=\"pyglotaran_examples_results\",\n", + ")\n", + "\n", + "data_path = script_folder.joinpath(\"data/data.ascii\")\n", + "model_path = script_folder.joinpath(GA_MODEL_PATH)\n", + "parameter_path = script_folder.joinpath(GA_PARAMETERS_PATH)" + ] + }, + { + "cell_type": "markdown", + "id": "6", + "metadata": {}, + "source": [ + "### Loading in data files" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7", + "metadata": {}, + "outputs": [], + "source": [ + "# load the data\n", + "experiment_data = {\"dataset1\": load_dataset(data_path)}\n", + "# load the (model) scheme (library)\n", + "model = load_model(model_path, format_name=\"yml\")\n", + "# load the parameters\n", + "parameters = load_parameters(parameter_path)\n", + "# attach the data to the scheme\n", + "scheme = Scheme(\n", + " model,\n", + " parameters,\n", + " experiment_data,\n", + " maximum_number_function_evaluations=6, # 6 for TRF, 46 for LM\n", + " # optimization_method=\"Levenberg-Marquardt\", #lm needs nfev=46\n", + ")\n", + "\n", + "scheme.validate()" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, + "source": [ + "### Optimizing the global fit" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9", + "metadata": {}, + "outputs": [], + "source": [ + "# Run the optimizer\n", + "result = optimize(scheme, verbose=True)\n", + "\n", + "result # shows result as markdown formatted table" + ] + }, + { + "cell_type": "markdown", + "id": "10", + "metadata": {}, + "source": [ + "### Saving results to file\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11", + "metadata": {}, + "outputs": [], + "source": [ + "# Save the results\n", + "result.save(results_folder, allow_overwrite=True)" + ] + }, + { + "cell_type": "markdown", + "id": "12", + "metadata": {}, + "source": [ + "### Inspection of results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13", + "metadata": {}, + "outputs": [], + "source": [ + "# Inspect the results\n", + "res = result.data[\"dataset1\"]\n", + "\n", + "res" + ] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, + "source": [ + "### Plotting of results\n", + "\n", + "This requires pyglotaran-extras\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15", + "metadata": {}, + "outputs": [], + "source": [ + "# Plot the results\n", + "\n", + "# %% Set subsequent plots to the glotaran style\n", + "plot_style = PlotStyle()\n", + "plt.rc(\"axes\", prop_cycle=plot_style.cycler)\n", + "\n", + "# %%\n", + "fig, _ = plot_overview(res, linlog=False)\n", + "# note species concentration plot still needs work to match styles between the two locatable axis\n", + "\n", + "# %%\n", + "figure_output_path = results_folder / f\"plot_overview_{results_folder.name}.pdf\"\n", + "fig.savefig(str(figure_output_path), bbox_inches=\"tight\")\n", + "print(results_folder)" + ] + }, + { + "cell_type": "markdown", + "id": "16", + "metadata": {}, + "source": [ + "## Target analysis" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "17", + "metadata": {}, + "outputs": [], + "source": [ + "TA_MODEL_PATH = \"models/model-target.yaml\"\n", + "TA_PARAMETERS_PATH = \"models/parameters-target.yaml\"\n", + "\n", + "results_folder, script_folder = setup_case_study(\n", + " output_folder_name=\"pyglotaran_examples_results\",\n", + ")\n", + "results_folder / \"study_fluorescence\"\n", + "\n", + "data_path = script_folder.joinpath(\"data/data.ascii\")\n", + "model_path = script_folder.joinpath(TA_MODEL_PATH)\n", + "parameter_path = script_folder.joinpath(TA_PARAMETERS_PATH)\n", + "\n", + "experiment_data = {\"dataset1\": load_dataset(data_path)}\n", + "model = load_model(model_path, format_name=\"yml\")\n", + "parameters = load_parameters(parameter_path)\n", + "scheme = Scheme(\n", + " model,\n", + " parameters,\n", + " experiment_data,\n", + " maximum_number_function_evaluations=6, # 6 for TRF, 46 for LM\n", + " # optimization_method=\"Levenberg-Marquardt\", #lm needs nfev=46\n", + ")\n", + "\n", + "result = optimize(scheme, verbose=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18", + "metadata": {}, + "outputs": [], + "source": [ + "result.optimized_parameters" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19", + "metadata": {}, + "outputs": [], + "source": [ + "result.save(results_folder, allow_overwrite=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20", + "metadata": {}, + "outputs": [], + "source": [ + "res = result.data[\"dataset1\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21", + "metadata": {}, + "outputs": [], + "source": [ + "# Plot the results\n", + "\n", + "# %% Set subsequent plots to the glotaran style\n", + "plot_style = PlotStyle()\n", + "plt.rc(\"axes\", prop_cycle=plot_style.cycler)\n", + "\n", + "# %%\n", + "fig, _ = plot_overview(res, linlog=False)\n", + "# note species concentration plot still needs work to match styles between the two locatable axis\n", + "\n", + "# %%\n", + "figure_output_path = results_folder / f\"plot_overview_{results_folder.name}.pdf\"\n", + "fig.savefig(str(figure_output_path), bbox_inches=\"tight\")\n", + "print(results_folder)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "pygta_staging", + "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" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}