{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial about fluopy - io" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we demonstrate input/output and serialization of datastructures in fluopy." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "import pickle\n", "import tempfile\n", "from pathlib import Path\n", "\n", "import numpy as np\n", "\n", "import fluopy\n", "import fluopy.analysis as an\n", "import fluopy.blinking as bl\n", "import fluopy.emissions as em\n", "import fluopy.fcs as fcs_p\n", "import fluopy.fluorophores as fl\n", "import fluopy.miscellaneous as mi\n", "import fluopy.prediction as pr\n", "import fluopy.simulation as si\n", "import fluopy.transitions as tr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ruff: noqa: S101, S301, S403" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "fluopy.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For random number generation a Generator is initialized." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rng = np.random.default_rng(seed=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Single dye simulation setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For further demonstration we set up a simple fluorophore system that contains a single Cy5 fluorophore." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "fluorophore = fl.Fluorophore(name=\"cy5_dna\", position=[0, 0])\n", "fluorophore_system = fl.FluorophoreSystem(fluorophores=[fluorophore])\n", "\n", "transitions = fluorophore_system.load_transitions(\n", " summarize=True,\n", " irradiance=2,\n", " wavelength=640,\n", " bleaching=False,\n", " energy_transfer=False,\n", " dstorm=False,\n", ")\n", "transition_set = tr.TransitionSet(transitions, fluorophore_system)\n", "transition_set = transition_set.remove_energy_transfers()\n", "transition_set.finalize()\n", "\n", "prediction = pr.Prediction(transition_set)\n", "simulation = si.Simulation(transition_set)\n", "simulation.run(start_at=None, size=1e6, end_time=None, seed=rng, use_memmap=None)\n", "\n", "analysis = an.Analysis(simulation=simulation)\n", "emissions = em.Emissions(frame_time=\"5ms\", seed=rng, bandpass=(600, 800))\n", "emissions.extract(simulation=simulation)\n", "fcs = fcs_p.FCS(emissions)\n", "blinks = bl.Blinking(emissions, threshold=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pickle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To save data temporarily in a python environment, the data classes can be serialized (pickled), saved and reloaded." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pickle TransitionSet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmpdirname:\n", " file_path = Path(tmpdirname) / \"transition_set.pickle\"\n", "\n", " with open(file_path, mode=\"wb\") as file:\n", " pickle.dump(transition_set, file=file, protocol=pickle.HIGHEST_PROTOCOL)\n", "\n", " with open(file_path, mode=\"rb\") as file:\n", " transition_set_new = pickle.load(file)\n", "\n", "assert transition_set.transition_df.equals(transition_set_new.transition_df)\n", "transition_set_new.transition_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pickle Prediction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmpdirname:\n", " file_path = Path(tmpdirname) / \"prediction.pickle\"\n", " print(\"created temporary\", file_path)\n", "\n", " with open(file_path, mode=\"wb\") as file:\n", " pickle.dump(prediction, file=file, protocol=pickle.HIGHEST_PROTOCOL)\n", "\n", " with open(file_path, mode=\"rb\") as file:\n", " prediction_new = pickle.load(file)\n", "\n", "assert np.array_equal(\n", " prediction.mean_transition_times, prediction_new.mean_transition_times\n", ")\n", "mi.print_class(prediction_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pickle Simulation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmpdirname:\n", " file_path = Path(tmpdirname) / \"simulation.pickle\"\n", " print(\"created temporary\", file_path)\n", "\n", " with open(file_path, mode=\"wb\") as file:\n", " pickle.dump(simulation, file=file, protocol=pickle.HIGHEST_PROTOCOL)\n", "\n", " with open(file_path, mode=\"rb\") as file:\n", " simulation_new = pickle.load(file)\n", "\n", "assert np.array_equal(simulation.transition_series, simulation_new.transition_series)\n", "mi.print_class(simulation_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pickle Emissions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmpdirname:\n", " file_path = Path(tmpdirname) / \"emissions.pickle\"\n", " print(\"created temporary\", file_path)\n", "\n", " with open(file_path, mode=\"wb\") as file:\n", " pickle.dump(emissions, file=file, protocol=pickle.HIGHEST_PROTOCOL)\n", "\n", " with open(file_path, mode=\"rb\") as file:\n", " emissions_new = pickle.load(file)\n", "\n", "\n", "assert emissions.event_time_series.equals(emissions_new.event_time_series)\n", "mi.print_class(emissions_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pickle Analysis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmpdirname:\n", " file_path = Path(tmpdirname) / \"analysis.pickle\"\n", " print(\"created temporary\", file_path)\n", "\n", " with open(file_path, mode=\"wb\") as file:\n", " pickle.dump(analysis, file=file, protocol=pickle.HIGHEST_PROTOCOL)\n", "\n", " with open(file_path, mode=\"rb\") as file:\n", " analysis_new = pickle.load(file)\n", "\n", "assert np.array_equal(\n", " analysis.mean_transition_times, analysis_new.mean_transition_times\n", ")\n", "mi.print_class(analysis_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pickle FCS" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmpdirname:\n", " file_path = Path(tmpdirname) / \"fcs.pickle\"\n", " print(\"created temporary\", file_path)\n", "\n", " with open(file_path, mode=\"wb\") as file:\n", " pickle.dump(fcs, file=file, protocol=pickle.HIGHEST_PROTOCOL)\n", "\n", " with open(file_path, mode=\"rb\") as file:\n", " fcs_new = pickle.load(file)\n", "\n", "assert np.array_equal(fcs.autocorrelation, fcs_new.autocorrelation)\n", "mi.print_class(fcs_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pickle Blinks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmpdirname:\n", " file_path = Path(tmpdirname) / \"blinks.pickle\"\n", " print(\"created temporary\", file_path)\n", "\n", " with open(file_path, mode=\"wb\") as file:\n", " pickle.dump(blinks, file=file, protocol=pickle.HIGHEST_PROTOCOL)\n", "\n", " with open(file_path, mode=\"rb\") as file:\n", " blinks_new = pickle.load(file)\n", "\n", "assert np.array_equal(blinks.on_periods, blinks_new.on_periods)\n", "mi.print_class(blinks_new)" ] } ], "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.14.4" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }