From 148988157294024f5895e05cf33ce5a4d0878f2c Mon Sep 17 00:00:00 2001 From: ilkilic <10600022+ilkilic@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:52:35 +0100 Subject: [PATCH] Add simplecell example (#165) * add simplecell example * fix warning in evolution plot whith small number of gen * update example readme * add a warning when no RANGE variable is defined in mod file when plotting currentscape --- bluepyemodel/emodel_pipeline/plotting.py | 10 +- examples/README.md | 2 + examples/simplecell/.gitignore | 18 + examples/simplecell/LICENSE.txt | 9 + examples/simplecell/LICENSE_CC-BY-CA-SA-4.0 | 104 +++ .../extract_config/simplecell_config.json | 461 ++++++++++ .../config/features/simplecell.json | 214 +++++ examples/simplecell/config/params/simple.json | 24 + examples/simplecell/config/recipes.json | 25 + examples/simplecell/download_ephys_data.sh | 78 ++ examples/simplecell/final.json | 44 + examples/simplecell/morphologies/simple.swc | 4 + examples/simplecell/simplecell.ipynb | 854 ++++++++++++++++++ 13 files changed, 1846 insertions(+), 1 deletion(-) create mode 100644 examples/simplecell/.gitignore create mode 100644 examples/simplecell/LICENSE.txt create mode 100644 examples/simplecell/LICENSE_CC-BY-CA-SA-4.0 create mode 100644 examples/simplecell/config/extract_config/simplecell_config.json create mode 100644 examples/simplecell/config/features/simplecell.json create mode 100644 examples/simplecell/config/params/simple.json create mode 100644 examples/simplecell/config/recipes.json create mode 100755 examples/simplecell/download_ephys_data.sh create mode 100644 examples/simplecell/final.json create mode 100644 examples/simplecell/morphologies/simple.swc create mode 100644 examples/simplecell/simplecell.ipynb diff --git a/bluepyemodel/emodel_pipeline/plotting.py b/bluepyemodel/emodel_pipeline/plotting.py index f53c1138..9fe480fa 100644 --- a/bluepyemodel/emodel_pipeline/plotting.py +++ b/bluepyemodel/emodel_pipeline/plotting.py @@ -247,7 +247,11 @@ def evolution_parameters_density( max_n_gen = max(max_n_gen, run["generation"]) genealogies[checkpoint_path] = (run["history"].genealogy_history, seed) - gen_per_bin = 4 + if max_n_gen >= 8: + gen_per_bin = 4 + else: + gen_per_bin = 1 + pop_size = len(run["population"]) histo_bins = (int(max_n_gen / gen_per_bin), 20) normalization_factor = gen_per_bin * pop_size @@ -1973,6 +1977,10 @@ def plot_models( dendritic_feature_plots(mo, "rheobase", dest_leaf, figures_dir) if plot_currentscape: + logger.warning( + "If an ion channel mod file lacks RANGE current variable (e.g. RANGE ik, ina), " + "no associated current will be plotted for that SUFFIX in Currentscape." + ) config = access_point.pipeline_settings.currentscape_config figures_dir_currentscape = figures_dir / "currentscape" / dest_leaf currentscape( diff --git a/examples/README.md b/examples/README.md index c7112ea0..e4aa0463 100644 --- a/examples/README.md +++ b/examples/README.md @@ -4,6 +4,8 @@ This directory includes various examples demonstrating how to use BluePyEModel: - [**L5PC**](./L5PC/README.rst): Provides a guide for setting up and running the E-Model building pipeline locally on a personal computer or on a Slurm cluster. This is an example based on the juvenile (P14) rat somatosensory cortex layer 5 thick-tufted pyramidal cell (L5TPC) e-model optimisation of the [SSCxEModelExamples](https://github.com/BlueBrain/SSCxEModelExamples/tree/main/optimization) repository. +- [**simplecell**](./simplecell/simplecell.ipynb): Demonstrates creating a single-compartment neuron model with two parameters using rheobase independent optimisation. + - [**nexus**](./nexus/README.md): Demonstrates the use of BluePyEModel through the Nexus access point. - [**others**](./others/README.rst): Contains various examples demonstrating different functionalities and use cases. diff --git a/examples/simplecell/.gitignore b/examples/simplecell/.gitignore new file mode 100644 index 00000000..3f87f823 --- /dev/null +++ b/examples/simplecell/.gitignore @@ -0,0 +1,18 @@ +.ipynb_checkpoints/ +run/ +.DS_Store/ +checkpoints/ +logs/ +figures/ +docs/ +notebooks/ +temp/ +.ipython/ +.tmp/ +x86_64/ +core.* +recordings/ +__pycache__/ +ephys_data/ +myvenv/ +final_tmp.json \ No newline at end of file diff --git a/examples/simplecell/LICENSE.txt b/examples/simplecell/LICENSE.txt new file mode 100644 index 00000000..1fea2bfc --- /dev/null +++ b/examples/simplecell/LICENSE.txt @@ -0,0 +1,9 @@ +The CC-BY-NC-SA license applies, as indicated by headers in the +respective source files. + +https://creativecommons.org/licenses/by-nc-sa/4.0/ + +The detailed text is available here +https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode + +The Python code and cell morphology are licensed with the above mentioned CC-BY-NC-SA license. \ No newline at end of file diff --git a/examples/simplecell/LICENSE_CC-BY-CA-SA-4.0 b/examples/simplecell/LICENSE_CC-BY-CA-SA-4.0 new file mode 100644 index 00000000..043d4169 --- /dev/null +++ b/examples/simplecell/LICENSE_CC-BY-CA-SA-4.0 @@ -0,0 +1,104 @@ +Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License + +By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. + +Section 1 – Definitions. + + Adapted Material means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. + Adapter's License means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. + BY-NC-SA Compatible License means a license listed at creativecommons.org/compatiblelicenses, approved by Creative Commons as essentially the equivalent of this Public License. + Copyright and Similar Rights means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. + Effective Technological Measures means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. + Exceptions and Limitations means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. + License Elements means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution, NonCommercial, and ShareAlike. + Licensed Material means the artistic or literary work, database, or other material to which the Licensor applied this Public License. + Licensed Rights means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. + Licensor means the individual(s) or entity(ies) granting rights under this Public License. + NonCommercial means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange. + Share means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. + Sui Generis Database Rights means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. + You means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. + +Section 2 – Scope. + + License grant. + Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: + reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and + produce, reproduce, and Share Adapted Material for NonCommercial purposes only. + Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. + Term. The term of this Public License is specified in Section 6(a). + Media and formats; technical modifications allowed. The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. + Downstream recipients. + Offer from the Licensor – Licensed Material. Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. + Additional offer from the Licensor – Adapted Material. Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter’s License You apply. + No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. + No endorsement. Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). + + Other rights. + Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. + Patent and trademark rights are not licensed under this Public License. + To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes. + +Section 3 – License Conditions. + +Your exercise of the Licensed Rights is expressly made subject to the following conditions. + + Attribution. + + If You Share the Licensed Material (including in modified form), You must: + retain the following if it is supplied by the Licensor with the Licensed Material: + identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); + a copyright notice; + a notice that refers to this Public License; + a notice that refers to the disclaimer of warranties; + a URI or hyperlink to the Licensed Material to the extent reasonably practicable; + indicate if You modified the Licensed Material and retain an indication of any previous modifications; and + indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. + You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. + If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. + ShareAlike. + + In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. + The Adapter’s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-NC-SA Compatible License. + You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. + You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. + +Section 4 – Sui Generis Database Rights. + +Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: + + for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only; + if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and + You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. + +For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. + +Section 5 – Disclaimer of Warranties and Limitation of Liability. + + Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You. + To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You. + + The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. + +Section 6 – Term and Termination. + + This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. + + Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: + automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or + upon express reinstatement by the Licensor. + For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. + For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. + Sections 1, 5, 6, 7, and 8 survive termination of this Public License. + +Section 7 – Other Terms and Conditions. + + The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. + Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License. + +Section 8 – Interpretation. + + For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. + To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. + No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. + Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. \ No newline at end of file diff --git a/examples/simplecell/config/extract_config/simplecell_config.json b/examples/simplecell/config/extract_config/simplecell_config.json new file mode 100644 index 00000000..795bb360 --- /dev/null +++ b/examples/simplecell/config/extract_config/simplecell_config.json @@ -0,0 +1,461 @@ +{ + "emodel": "simplecell", + "ttype": null, + "files": [ + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDrest_ch0_326", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDrest": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_326.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch1_326.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDrest_ch0_327", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDrest": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_327.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch1_327.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDrest_ch0_328", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDrest": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_328.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch1_328.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDrest_ch0_329", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDrest": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_329.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch1_329.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDrest_ch0_330", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDrest": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_330.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDrest_ch1_330.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDthresh_ch0_349", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDthresh": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_349.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch1_349.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDthresh_ch0_350", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDthresh": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_350.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch1_350.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDthresh_ch0_351", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDthresh": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_351.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch1_351.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDthresh_ch0_352", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDthresh": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_352.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch1_352.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IDthresh_ch0_353", + "filepath": null, + "resource_id": null, + "ecodes": { + "IDthresh": { + "ljp": 14.0, + "ton": 700, + "toff": 2700 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_353.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IDthresh_ch1_353.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IV_ch0_266", + "filepath": null, + "resource_id": null, + "ecodes": { + "IV": { + "ljp": 14.0, + "ton": 20, + "toff": 1020 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch0_266.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch1_266.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IV_ch0_267", + "filepath": null, + "resource_id": null, + "ecodes": { + "IV": { + "ljp": 14.0, + "ton": 20, + "toff": 1020 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch0_267.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch1_267.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IV_ch0_268", + "filepath": null, + "resource_id": null, + "ecodes": { + "IV": { + "ljp": 14.0, + "ton": 20, + "toff": 1020 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch0_268.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch1_268.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IV_ch0_269", + "filepath": null, + "resource_id": null, + "ecodes": { + "IV": { + "ljp": 14.0, + "ton": 20, + "toff": 1020 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch0_269.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch1_269.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + }, + { + "cell_name": "C060109A1-SR-C1", + "filename": "X_IV_ch0_270", + "filepath": null, + "resource_id": null, + "ecodes": { + "IV": { + "ljp": 14.0, + "ton": 20, + "toff": 1020 + } + }, + "other_metadata": { + "i_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch0_270.ibw", + "v_file": "./ephys_data/C060109A1-SR-C1/X_IV_ch1_270.ibw", + "i_unit": "A", + "v_unit": "V", + "t_unit": "s" + }, + "species": null, + "brain_region": null, + "etype": null, + "id": null + } + ], + "targets": [ + { + "efeature": "Spikecount", + "protocol": "IDrest", + "amplitude": 0.2, + "tolerance": 0.1, + "efeature_name": null, + "weight": 1.0, + "efel_settings": { + "strict_stiminterval": true + } + }, + { + "efeature": "mean_frequency", + "protocol": "IDrest", + "amplitude": 0.2, + "tolerance": 0.1, + "efeature_name": null, + "weight": 1.0, + "efel_settings": { + "strict_stiminterval": true + } + }, + { + "efeature": "voltage_base", + "protocol": "IDrest", + "amplitude": 0.2, + "tolerance": 0.1, + "efeature_name": null, + "weight": 1.0, + "efel_settings": { + "strict_stiminterval": true + } + }, + { + "efeature": "Spikecount", + "protocol": "IDrest", + "amplitude": 0.4, + "tolerance": 0.1, + "efeature_name": null, + "weight": 1.0, + "efel_settings": { + "strict_stiminterval": true + } + }, + { + "efeature": "mean_frequency", + "protocol": "IDrest", + "amplitude": 0.4, + "tolerance": 0.1, + "efeature_name": null, + "weight": 1.0, + "efel_settings": { + "strict_stiminterval": true + } + }, + { + "efeature": "voltage_base", + "protocol": "IDrest", + "amplitude": 0.4, + "tolerance": 0.1, + "efeature_name": null, + "weight": 1.0, + "efel_settings": { + "strict_stiminterval": true + } + }, + { + "efeature": "voltage_base", + "protocol": "IV", + "amplitude": -0.1, + "tolerance": 0.1, + "efeature_name": null, + "weight": 1.0, + "efel_settings": { + "strict_stiminterval": true + } + }, + { + "efeature": "ohmic_input_resistance_vb_ssse", + "protocol": "IV", + "amplitude": -0.1, + "tolerance": 0.1, + "efeature_name": null, + "weight": 1.0, + "efel_settings": { + "strict_stiminterval": true + } + } + ], + "protocols_rheobase": [], + "auto_targets": null, + "additional_fitness_efeatures": null, + "additional_fitness_protocols": null, + "protocols_mapping": null +} \ No newline at end of file diff --git a/examples/simplecell/config/features/simplecell.json b/examples/simplecell/config/features/simplecell.json new file mode 100644 index 00000000..cac8962a --- /dev/null +++ b/examples/simplecell/config/features/simplecell.json @@ -0,0 +1,214 @@ +{ + "efeatures": [ + { + "efel_feature_name": "Spikecount", + "protocol_name": "IDrest_0.2", + "recording_name": "soma.v", + "threshold_efeature_std": null, + "default_std_value": 0.001, + "mean": 5.0, + "original_std": 3.605551275463989, + "sample_size": 4, + "efeature_name": "Spikecount", + "weight": 1.0, + "efel_settings": { + "interp_step": 0.025, + "strict_stiminterval": true, + "Threshold": -48.99374771118164 + } + }, + { + "efel_feature_name": "mean_frequency", + "protocol_name": "IDrest_0.2", + "recording_name": "soma.v", + "threshold_efeature_std": null, + "default_std_value": 0.001, + "mean": 3.9105121810617045, + "original_std": 1.2910808300604681, + "sample_size": 3, + "efeature_name": "mean_frequency", + "weight": 1.0, + "efel_settings": { + "interp_step": 0.025, + "strict_stiminterval": true, + "Threshold": -48.99374771118164 + } + }, + { + "efel_feature_name": "voltage_base", + "protocol_name": "IDrest_0.2", + "recording_name": "soma.v", + "threshold_efeature_std": null, + "default_std_value": 0.001, + "mean": -84.83373198951986, + "original_std": 0.28733612701960526, + "sample_size": 4, + "efeature_name": "voltage_base", + "weight": 1.0, + "efel_settings": { + "interp_step": 0.025, + "strict_stiminterval": true, + "Threshold": -48.99374771118164 + } + }, + { + "efel_feature_name": "Spikecount", + "protocol_name": "IDrest_0.4", + "recording_name": "soma.v", + "threshold_efeature_std": null, + "default_std_value": 0.001, + "mean": 13.0, + "original_std": 0.001, + "sample_size": 1, + "efeature_name": "Spikecount", + "weight": 1.0, + "efel_settings": { + "interp_step": 0.025, + "strict_stiminterval": true, + "Threshold": -50.11250305175781 + } + }, + { + "efel_feature_name": "mean_frequency", + "protocol_name": "IDrest_0.4", + "recording_name": "soma.v", + "threshold_efeature_std": null, + "default_std_value": 0.001, + "mean": 6.888329580058474, + "original_std": 0.001, + "sample_size": 1, + "efeature_name": "mean_frequency", + "weight": 1.0, + "efel_settings": { + "interp_step": 0.025, + "strict_stiminterval": true, + "Threshold": -50.11250305175781 + } + }, + { + "efel_feature_name": "voltage_base", + "protocol_name": "IDrest_0.4", + "recording_name": "soma.v", + "threshold_efeature_std": null, + "default_std_value": 0.001, + "mean": -85.36351310593709, + "original_std": 0.001, + "sample_size": 1, + "efeature_name": "voltage_base", + "weight": 1.0, + "efel_settings": { + "interp_step": 0.025, + "strict_stiminterval": true, + "Threshold": -50.11250305175781 + } + }, + { + "efel_feature_name": "voltage_base", + "protocol_name": "IV_-0.1", + "recording_name": "soma.v", + "threshold_efeature_std": null, + "default_std_value": 0.001, + "mean": -84.4690685507692, + "original_std": 0.08042238367019017, + "sample_size": 4, + "efeature_name": "voltage_base", + "weight": 1.0, + "efel_settings": { + "interp_step": 0.025, + "strict_stiminterval": true, + "Threshold": -64.51874923706055 + } + }, + { + "efel_feature_name": "ohmic_input_resistance_vb_ssse", + "protocol_name": "IV_-0.1", + "recording_name": "soma.v", + "threshold_efeature_std": null, + "default_std_value": 0.001, + "mean": 48.64694052877976, + "original_std": 0.8664384286999164, + "sample_size": 4, + "efeature_name": "ohmic_input_resistance_vb_ssse", + "weight": 1.0, + "efel_settings": { + "interp_step": 0.025, + "strict_stiminterval": true, + "Threshold": -64.51874923706055 + } + } + ], + "protocols": [ + { + "name": "IDrest_0.2", + "stimuli": [ + { + "delay": 700.0000000000001, + "amp": 0.23945312993600965, + "thresh_perc": null, + "duration": 2000.0, + "totduration": 3000.0, + "holding_current": -0.026718751061707735 + } + ], + "recordings_from_config": [ + { + "type": "CompRecording", + "name": "IDrest_0.2.soma.v", + "location": "soma", + "variable": "v" + } + ], + "validation": false, + "protocol_type": "Protocol", + "stochasticity": false + }, + { + "name": "IDrest_0.4", + "stimuli": [ + { + "delay": 700.0000000000001, + "amp": 0.3318749852478504, + "thresh_perc": null, + "duration": 2000.0, + "totduration": 3000.0, + "holding_current": -0.02656250074505806 + } + ], + "recordings_from_config": [ + { + "type": "CompRecording", + "name": "IDrest_0.4.soma.v", + "location": "soma", + "variable": "v" + } + ], + "validation": true, + "protocol_type": "Protocol", + "stochasticity": false + }, + { + "name": "IV_-0.1", + "stimuli": [ + { + "delay": 20.0, + "amp": -0.1265624992083758, + "thresh_perc": null, + "duration": 1000.0, + "totduration": 1320.0, + "holding_current": -0.02816406195051968 + } + ], + "recordings_from_config": [ + { + "type": "CompRecording", + "name": "IV_-0.1.soma.v", + "location": "soma", + "variable": "v" + } + ], + "validation": false, + "protocol_type": "Protocol", + "stochasticity": false + } + ] +} \ No newline at end of file diff --git a/examples/simplecell/config/params/simple.json b/examples/simplecell/config/params/simple.json new file mode 100644 index 00000000..45ba5721 --- /dev/null +++ b/examples/simplecell/config/params/simple.json @@ -0,0 +1,24 @@ +{ + "mechanisms": { + "somatic": + {"mech":["hh"]} + }, + "distributions": { + }, + "parameters": { + "__comment": "define constants as single values and params to optimise as tuples of bounds: [lower, upper]", + "global": [ + {"name":"v_init", "val":-80}, + {"name":"celsius", "val":34} + ], + "somatic": [ + {"name":"Ra", "val":100}, + {"name":"cm", "val":1}, + {"name":"ena", "val":50}, + {"name":"ek", "val":-90}, + {"name":"gnabar_hh", "val":[0.05, 0.125]}, + {"name":"gkbar_hh", "val":[0.01, 0.075]} + + ] + } +} diff --git a/examples/simplecell/config/recipes.json b/examples/simplecell/config/recipes.json new file mode 100644 index 00000000..68a232f5 --- /dev/null +++ b/examples/simplecell/config/recipes.json @@ -0,0 +1,25 @@ +{ + "simplecell": { + "morph_path": "./morphologies/", + "morphology": [ + [ + "simple", + "simple.swc" + ] + ], + "params": "config/params/simple.json", + "features": "config/features/simplecell.json", + "pipeline_settings": { + "path_extract_config": "config/extract_config/simplecell_config.json", + "extract_absolute_amplitudes": true, + "optimiser": "SO-CMA", + "max_ngen": 5, + "optimisation_params": { + "offspring_size": 20 + }, + "validation_protocols": ["IDrest_0.4"], + "morph_modifiers": [], + "plot_currentscape": false + } + } +} \ No newline at end of file diff --git a/examples/simplecell/download_ephys_data.sh b/examples/simplecell/download_ephys_data.sh new file mode 100755 index 00000000..8c13a08f --- /dev/null +++ b/examples/simplecell/download_ephys_data.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +##################################################################### +# Copyright 2023-2024 Blue Brain Project / EPFL + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +##################################################################### + +# Download the input traces from the repository https://github.com/BlueBrain/SSCxEModelExamples/tree/main/feature_extraction/input-traces/C060109A1-SR-C1 +USER="BlueBrain" +REPO="SSCxEModelExamples" +BRANCH="main" +FOLDER="feature_extraction/input-traces/C060109A1-SR-C1" +BASE_API="https://api.github.com/repos/$USER/$REPO/git/trees/$BRANCH?recursive=1" +DEST_DIR="ephys_data/C060109A1-SR-C1" # should match the path in the targets.py file + +# List of files to download +FILES_TO_DOWNLOAD=( + "X_IDrest_ch0_326.ibw" + "X_IDrest_ch0_327.ibw" + "X_IDrest_ch0_328.ibw" + "X_IDrest_ch0_329.ibw" + "X_IDrest_ch0_330.ibw" + "X_IDrest_ch1_326.ibw" + "X_IDrest_ch1_327.ibw" + "X_IDrest_ch1_328.ibw" + "X_IDrest_ch1_329.ibw" + "X_IDrest_ch1_330.ibw" + "X_IDthresh_ch0_349.ibw" + "X_IDthresh_ch0_350.ibw" + "X_IDthresh_ch0_351.ibw" + "X_IDthresh_ch0_352.ibw" + "X_IDthresh_ch0_353.ibw" + "X_IDthresh_ch1_349.ibw" + "X_IDthresh_ch1_350.ibw" + "X_IDthresh_ch1_351.ibw" + "X_IDthresh_ch1_352.ibw" + "X_IDthresh_ch1_353.ibw" + "X_IV_ch0_266.ibw" + "X_IV_ch0_267.ibw" + "X_IV_ch0_268.ibw" + "X_IV_ch0_269.ibw" + "X_IV_ch0_270.ibw" + "X_IV_ch1_266.ibw" + "X_IV_ch1_267.ibw" + "X_IV_ch1_268.ibw" + "X_IV_ch1_269.ibw" + "X_IV_ch1_270.ibw" +) + +# Create destination directory if it doesn't exist +mkdir -p "$DEST_DIR" + +# Fetch the list of files from the GitHub API and filter for required files +FILES=$(curl -s "$BASE_API" | grep -oP "\"path\": \"$FOLDER/.*?\.ibw\"" | grep -oP "(?<=\"$FOLDER/)[^\"]+") + +# Download each file if it matches the list to download +for file in $FILES; do + filename=$(basename "$file") + + if [[ " ${FILES_TO_DOWNLOAD[*]} " == *" $filename "* ]]; then + if [ ! -f "$DEST_DIR/$filename" ]; then + wget "https://raw.githubusercontent.com/$USER/$REPO/$BRANCH/$FOLDER/$filename" -P "$DEST_DIR" + else + echo "$filename already exists. Skipping download." + fi + fi +done diff --git a/examples/simplecell/final.json b/examples/simplecell/final.json new file mode 100644 index 00000000..d2b7d254 --- /dev/null +++ b/examples/simplecell/final.json @@ -0,0 +1,44 @@ +{ + "simplecell__1": { + "emodel": "simplecell", + "etype": "cADpyr", + "ttype": null, + "mtype": null, + "species": "rat", + "brain_region": "SSCX", + "iteration": null, + "synapse_class": null, + "allen_notation": null, + "score": 510.2861723258447, + "parameters": { + "gnabar_hh.somatic": 0.05000000003749999, + "gkbar_hh.somatic": 0.07499999996750001 + }, + "fitness": { + "IDrest_0.2.soma.v.Spikecount": 1.1094003924504583, + "IDrest_0.2.soma.v.mean_frequency": 250.0, + "IDrest_0.2.soma.v.voltage_base": 6.86265875272435, + "IV_-0.1.soma.v.voltage_base": 2.3141131806698945, + "IV_-0.1.soma.v.ohmic_input_resistance_vb_ssse": 250.0 + }, + "features": { + "IDrest_0.2.soma.v.Spikecount": 1.0, + "IDrest_0.2.soma.v.mean_frequency": 1739.1304358817733, + "IDrest_0.2.soma.v.voltage_base": -82.86184220245485, + "IDrest_0.4.soma.v.Spikecount": 1.0, + "IDrest_0.4.soma.v.mean_frequency": 2105.263159505021, + "IDrest_0.4.soma.v.voltage_base": -82.71053576131624, + "IV_-0.1.soma.v.voltage_base": -84.28296205269712, + "IV_-0.1.soma.v.ohmic_input_resistance_vb_ssse": 1060.243113329033 + }, + "validation_fitness": { + "IDrest_0.4.soma.v.Spikecount": 250.0, + "IDrest_0.4.soma.v.mean_frequency": 250.0, + "IDrest_0.4.soma.v.voltage_base": 250.0 + }, + "validated": false, + "seed": 1, + "pdfs": [ + ] + } +} \ No newline at end of file diff --git a/examples/simplecell/morphologies/simple.swc b/examples/simplecell/morphologies/simple.swc new file mode 100644 index 00000000..cf106230 --- /dev/null +++ b/examples/simplecell/morphologies/simple.swc @@ -0,0 +1,4 @@ +# Dummy granule cell morphology +1 1 -5.0 0.0 0.0 5.0 -1 +2 1 0.0 0.0 0.0 5.0 1 +3 1 5.0 0.0 0.0 5.0 2 diff --git a/examples/simplecell/simplecell.ipynb b/examples/simplecell/simplecell.ipynb new file mode 100644 index 00000000..5debfb09 --- /dev/null +++ b/examples/simplecell/simplecell.ipynb @@ -0,0 +1,854 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Building an E-Model for a single-compartment cell\n", + "\n", + "This notebook provides a simple example on how to run the pipeline to create an e-model of a single-compartment cell with two free parameters. In this instance, we will use rheobase independent optimisation. \n", + "\n", + "When the optimisation is rheobase dependent, it means that we need to calculate the rheobase value (the minimum current required to trigger a neuron) to normalise the trace values. In this case, the normalisation is done by expressing the trace values as a percentage of the rheobase. This approach ensures that the traces are scaled relative to the neuron’s threshold current, providing a consistent basis for comparison. \n", + "\n", + "However, here we will present the rheobase independent approach which means that we use specific amplitude values like `IDRest_1.0` (indicating a fixed current of 1.0 nA) instead of `IDRest_100` (which represents 100% of the rheobase).\n", + "\n", + "For rheobase dependent optimisation or additional details, please refer to the [L5PC](./../L5PC/) example." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "\n", + "from bluepyemodel.emodel_pipeline.emodel_pipeline import EModel_pipeline\n", + "from bluepyemodel.efeatures_extraction.targets_configurator import TargetsConfigurator" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Clear any existing checkpoints to avoid conflicts with previous runs\n", + "!rm -r ./checkpoints" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setting up the pipeline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The pipeline setup involves six key steps: **extraction** of e-features from electrophysiological recordings; **optimisation** of a NEURON cell model based on these e-features; storing the optimised model parameters; **validating** the models against specified protocols; and **plotting** the results, including traces, e-feature scores, and parameter distributions." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The [recipes.json](./config/recipes.json) file (displayed below) contains the key settings for the various stages of the e-model building pipeline.\n", + "\n", + "* **``morph_path``**: Directory path for morphologies\n", + "* **``morphology``**: Contains a list with the morphology's arbitrary name and file name, located in `morph_path`.\n", + "* **``params``**: Specifies mechanisms, locations, distributions, and parameters.\n", + "* **``features``**: Path to the file with extraction outputs.\n", + "- **`pipeline_settings`**: Sets up the pipeline with several configuration options, including: \n", + " - `extract_absolute_amplitudes`: Set to `true` for performing optimisation independently of rheobase (threshold current). \n", + " - `optimiser`: Specifies the optimisation algorithm, set to `\"SO-CMA\"` (a single-objective Covariance Matrix Adaptation algorithm). \n", + " - `max_ngen`: Defines the maximum number of generations for the optimisation process, set to `5`. \n", + " - `optimisation_params`: Additional optimisation parameters, such as `offspring_size` set to `20`, indicating the number of solutions generated per generation. \n", + " - `validation_protocols`: Lists protocols used for validation, e.g., `[\"IDrest_0.4\"]`. \n", + " - `morph_modifiers`: Set to an empty list `[]`, meaning no specific modifications to morphologies are applied by default.\n", + " - `plot_currentscape`: We set this to False because we are using NEURON's built-in HH mechanism, where the currents are not defined as RANGE variables. As a result, Currentscape is unable to access and plot these currents." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"simplecell\": {\n", + " \"morph_path\": \"./morphologies/\",\n", + " \"morphology\": [\n", + " [\n", + " \"simple\",\n", + " \"simple.swc\"\n", + " ]\n", + " ],\n", + " \"params\": \"config/params/simple.json\",\n", + " \"features\": \"config/features/simplecell.json\",\n", + " \"pipeline_settings\": {\n", + " \"path_extract_config\": \"config/extract_config/simplecell_config.json\",\n", + " \"extract_absolute_amplitudes\": true,\n", + " \"optimiser\": \"SO-CMA\",\n", + " \"max_ngen\": 5,\n", + " \"optimisation_params\": {\n", + " \"offspring_size\": 20\n", + " },\n", + " \"validation_protocols\": [\n", + " \"IDrest_0.4\"\n", + " ],\n", + " \"morph_modifiers\": [],\n", + " \"plot_currentscape\": false\n", + " }\n", + " }\n", + "}\n" + ] + } + ], + "source": [ + "recipes_path = \"./config/recipes.json\"\n", + "with open(recipes_path, 'r') as file:\n", + " recipe = json.load(file)\n", + "\n", + "print(json.dumps(recipe, indent=4))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To begin, we need to instantiate the pipeline using the ``EModel_pipeline`` class. This class is responsible for loading the ``recipes.json`` file and configuring the pipeline settings based on its content. The following are the minimal required parameters:\n", + "\n", + "- **`emodel`**: Name of the e-model\n", + "\n", + "- **`etype`**: electrical type, `\"cADpyr\"`, indicating continuous adapting pyramidal cells.\n", + "\n", + "- **`species`**: Biological species, `\"rat\"`, for which the model is developed.\n", + "\n", + "- **`brain_region`**: Target brain region, `\"SSCX\"` (somatosensory cortex).\n", + "\n", + "- **`morphology`**: Filename of the morphology file located in the `./morphologies` folder, with a spherical shape characterized by a diameter and length of 10 micrometers.\n", + "\n", + "- **`morphology_format`**: Specifies the format of the morphology file, \"swc\", with support for SWC, ASC formats." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "emodel = \"simplecell\"\n", + "etype = \"cADpyr\"\n", + "species = \"rat\"\n", + "brain_region = \"SSCX\"\n", + "morphology = \"simple\"\n", + "morphology_format = \"swc\"\n", + "\n", + "pipeline = EModel_pipeline(\n", + " emodel=emodel,\n", + " etype=etype,\n", + " species=species,\n", + " brain_region=brain_region,\n", + " recipes_path=recipes_path,\n", + " data_access_point=\"local\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Extracting the features" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We need to download the required data using the script `./download_ephys_data.sh`. This dataset features continuous adapting pyramidal cells (cADpyr) e-type models from the rat somatosensory cortex. The data can be obtained from this [repository]()." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "!sh ./download_ephys_data.sh" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "filenames = [\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_326.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_327.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_328.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_329.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDrest_ch0_330.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_349.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_350.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_351.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_352.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IDthresh_ch0_353.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IV_ch0_266.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IV_ch0_267.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IV_ch0_268.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IV_ch0_269.ibw\",\n", + " \"./ephys_data/C060109A1-SR-C1/X_IV_ch0_270.ibw\"\n", + "]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can now define the targets, which include the protocols (ecodes) and the features. We define two protocols: `IDrest` and `IV` protocols.\n", + "\n", + "For the IDrest protocol, with an amplitude of 0.2 nA (and 0.4 nA for validation), we select Spikecount, mean_frequency, and voltage_base to capture the neuron’s spiking activity and resting potential. In the IV protocol, with an amplitude of -0.1 nA, voltage_base and ohmic_input_resistance_vb_ssse are used to assess the neuron’s baseline potential and input resistance\n", + "\n", + "The `tolerance` of 0.1 allows for a range of ±0.1 around each target amplitude for extracting e-features. For example, if the target amplitude is 0.4, the acceptable range would be from 0.3 to 0.5 Values, excluding 0.3 and 0.5." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "targets = {\n", + " \"IDrest\": {\n", + " \"amplitudes\": [0.2, 0.4],\n", + " \"efeatures\": [\n", + " \"Spikecount\",\n", + " \"mean_frequency\",\n", + " \"voltage_base\",\n", + " ],\n", + " },\n", + " \"IV\": {\n", + " \"amplitudes\": [-0.1],\n", + " \"efeatures\": [\n", + " \"voltage_base\",\n", + " \"ohmic_input_resistance_vb_ssse\",\n", + " ],\n", + " }\n", + "}\n", + "\n", + "tolerance = 0.1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `ecodes_metadata` dictionary defines parameters for each protocol: `ljp` is the liquid junction potential correction (14.0 mV). A [LJP](https://en.wikipedia.org/wiki/Liquid_junction_potential) value is subtracted from each voltage value. Here, the membrane potential will be shifted in the hyperpolarising direction by 14 mV. `ton` and `toff` represent the start and stop times (in ms) for current injection." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "ecodes_metadata = {\n", + " \"IDthresh\": {\"ljp\": 14.0, \"ton\": 700, \"toff\": 2700},\n", + " \"IDrest\": {\"ljp\": 14.0, \"ton\": 700, \"toff\": 2700},\n", + " \"IV\": {\"ljp\": 14.0, \"ton\": 20, \"toff\": 1020},\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We save this targets in an object called `ExtractionTargetConfigurator` (ETC), which will serve as the input for the feature extractor" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "files_metadata = []\n", + "for filename in filenames:\n", + " fn = filename.split(\"/\")[-1]\n", + " for ecode in ecodes_metadata:\n", + " if ecode in fn:\n", + " files_metadata.append(\n", + " {\n", + " \"cell_name\": filename.split(\"/\")[-2],\n", + " \"filename\": filename.split(\"/\")[-1].split(\".\")[0],\n", + " \"ecodes\": {ecode: ecodes_metadata[ecode]},\n", + " \"other_metadata\": {\n", + " \"i_file\": filename,\n", + " \"v_file\": filename.replace(\"ch0\", \"ch1\"),\n", + " \"i_unit\": \"A\",\n", + " \"v_unit\": \"V\",\n", + " \"t_unit\": \"s\",\n", + " },\n", + " }\n", + " )\n", + "\n", + "\n", + "targets_formated = []\n", + "for ecode in targets:\n", + " for amplitude in targets[ecode][\"amplitudes\"]:\n", + " for efeature in targets[ecode][\"efeatures\"]:\n", + " targets_formated.append(\n", + " {\n", + " \"efeature\": efeature,\n", + " \"protocol\": ecode,\n", + " \"amplitude\": amplitude,\n", + " \"tolerance\": tolerance,\n", + " }\n", + " )\n", + "\n", + "\n", + "configurator = TargetsConfigurator(pipeline.access_point)\n", + "configurator.new_configuration(files_metadata, targets_formated)\n", + "configurator.save_configuration()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can now proceed to extract the e-features" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pipeline.extract_efeatures()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The results of the feature extraction is stored in `./config/features/simplecell.json`. Let's take a look at the extracted features" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"efeatures\": [\n", + " {\n", + " \"efel_feature_name\": \"Spikecount\",\n", + " \"protocol_name\": \"IDrest_0.2\",\n", + " \"recording_name\": \"soma.v\",\n", + " \"threshold_efeature_std\": null,\n", + " \"default_std_value\": 0.001,\n", + " \"mean\": 5.0,\n", + " \"original_std\": 3.605551275463989,\n", + " \"sample_size\": 4,\n", + " \"efeature_name\": \"Spikecount\",\n", + " \"weight\": 1.0,\n", + " \"efel_settings\": {\n", + " \"interp_step\": 0.025,\n", + " \"strict_stiminterval\": true,\n", + " \"Threshold\": -48.99374771118164\n", + " }\n", + " },\n", + " {\n", + " \"efel_feature_name\": \"mean_frequency\",\n", + " \"protocol_name\": \"IDrest_0.2\",\n", + " \"recording_name\": \"soma.v\",\n", + " \"threshold_efeature_std\": null,\n", + " \"default_std_value\": 0.001,\n", + " \"mean\": 3.9105121810617045,\n", + " \"original_std\": 1.2910808300604681,\n", + " \"sample_size\": 3,\n", + " \"efeature_name\": \"mean_frequency\",\n", + " \"weight\": 1.0,\n", + " \"efel_settings\": {\n", + " \"interp_step\": 0.025,\n", + " \"strict_stiminterval\": true,\n", + " \"Threshold\": -48.99374771118164\n", + " }\n", + " },\n", + " {\n", + " \"efel_feature_name\": \"voltage_base\",\n", + " \"protocol_name\": \"IDrest_0.2\",\n", + " \"recording_name\": \"soma.v\",\n", + " \"threshold_efeature_std\": null,\n", + " \"default_std_value\": 0.001,\n", + " \"mean\": -84.83373198951986,\n", + " \"original_std\": 0.28733612701960526,\n", + " \"sample_size\": 4,\n", + " \"efeature_name\": \"voltage_base\",\n", + " \"weight\": 1.0,\n", + " \"efel_settings\": {\n", + " \"interp_step\": 0.025,\n", + " \"strict_stiminterval\": true,\n", + " \"Threshold\": -48.99374771118164\n", + " }\n", + " },\n", + " {\n", + " \"efel_feature_name\": \"Spikecount\",\n", + " \"protocol_name\": \"IDrest_0.4\",\n", + " \"recording_name\": \"soma.v\",\n", + " \"threshold_efeature_std\": null,\n", + " \"default_std_value\": 0.001,\n", + " \"mean\": 13.0,\n", + " \"original_std\": 0.001,\n", + " \"sample_size\": 1,\n", + " \"efeature_name\": \"Spikecount\",\n", + " \"weight\": 1.0,\n", + " \"efel_settings\": {\n", + " \"interp_step\": 0.025,\n", + " \"strict_stiminterval\": true,\n", + " \"Threshold\": -50.11250305175781\n", + " }\n", + " },\n", + " {\n", + " \"efel_feature_name\": \"mean_frequency\",\n", + " \"protocol_name\": \"IDrest_0.4\",\n", + " \"recording_name\": \"soma.v\",\n", + " \"threshold_efeature_std\": null,\n", + " \"default_std_value\": 0.001,\n", + " \"mean\": 6.888329580058474,\n", + " \"original_std\": 0.001,\n", + " \"sample_size\": 1,\n", + " \"efeature_name\": \"mean_frequency\",\n", + " \"weight\": 1.0,\n", + " \"efel_settings\": {\n", + " \"interp_step\": 0.025,\n", + " \"strict_stiminterval\": true,\n", + " \"Threshold\": -50.11250305175781\n", + " }\n", + " },\n", + " {\n", + " \"efel_feature_name\": \"voltage_base\",\n", + " \"protocol_name\": \"IDrest_0.4\",\n", + " \"recording_name\": \"soma.v\",\n", + " \"threshold_efeature_std\": null,\n", + " \"default_std_value\": 0.001,\n", + " \"mean\": -85.36351310593709,\n", + " \"original_std\": 0.001,\n", + " \"sample_size\": 1,\n", + " \"efeature_name\": \"voltage_base\",\n", + " \"weight\": 1.0,\n", + " \"efel_settings\": {\n", + " \"interp_step\": 0.025,\n", + " \"strict_stiminterval\": true,\n", + " \"Threshold\": -50.11250305175781\n", + " }\n", + " },\n", + " {\n", + " \"efel_feature_name\": \"voltage_base\",\n", + " \"protocol_name\": \"IV_-0.1\",\n", + " \"recording_name\": \"soma.v\",\n", + " \"threshold_efeature_std\": null,\n", + " \"default_std_value\": 0.001,\n", + " \"mean\": -84.4690685507692,\n", + " \"original_std\": 0.08042238367019017,\n", + " \"sample_size\": 4,\n", + " \"efeature_name\": \"voltage_base\",\n", + " \"weight\": 1.0,\n", + " \"efel_settings\": {\n", + " \"interp_step\": 0.025,\n", + " \"strict_stiminterval\": true,\n", + " \"Threshold\": -64.51874923706055\n", + " }\n", + " },\n", + " {\n", + " \"efel_feature_name\": \"ohmic_input_resistance_vb_ssse\",\n", + " \"protocol_name\": \"IV_-0.1\",\n", + " \"recording_name\": \"soma.v\",\n", + " \"threshold_efeature_std\": null,\n", + " \"default_std_value\": 0.001,\n", + " \"mean\": 48.64694052877976,\n", + " \"original_std\": 0.8664384286999164,\n", + " \"sample_size\": 4,\n", + " \"efeature_name\": \"ohmic_input_resistance_vb_ssse\",\n", + " \"weight\": 1.0,\n", + " \"efel_settings\": {\n", + " \"interp_step\": 0.025,\n", + " \"strict_stiminterval\": true,\n", + " \"Threshold\": -64.51874923706055\n", + " }\n", + " }\n", + " ],\n", + " \"protocols\": [\n", + " {\n", + " \"name\": \"IDrest_0.2\",\n", + " \"stimuli\": [\n", + " {\n", + " \"delay\": 700.0000000000001,\n", + " \"amp\": 0.23945312993600965,\n", + " \"thresh_perc\": null,\n", + " \"duration\": 2000.0,\n", + " \"totduration\": 3000.0,\n", + " \"holding_current\": -0.026718751061707735\n", + " }\n", + " ],\n", + " \"recordings_from_config\": [\n", + " {\n", + " \"type\": \"CompRecording\",\n", + " \"name\": \"IDrest_0.2.soma.v\",\n", + " \"location\": \"soma\",\n", + " \"variable\": \"v\"\n", + " }\n", + " ],\n", + " \"validation\": false,\n", + " \"protocol_type\": \"Protocol\",\n", + " \"stochasticity\": false\n", + " },\n", + " {\n", + " \"name\": \"IDrest_0.4\",\n", + " \"stimuli\": [\n", + " {\n", + " \"delay\": 700.0000000000001,\n", + " \"amp\": 0.3318749852478504,\n", + " \"thresh_perc\": null,\n", + " \"duration\": 2000.0,\n", + " \"totduration\": 3000.0,\n", + " \"holding_current\": -0.02656250074505806\n", + " }\n", + " ],\n", + " \"recordings_from_config\": [\n", + " {\n", + " \"type\": \"CompRecording\",\n", + " \"name\": \"IDrest_0.4.soma.v\",\n", + " \"location\": \"soma\",\n", + " \"variable\": \"v\"\n", + " }\n", + " ],\n", + " \"validation\": true,\n", + " \"protocol_type\": \"Protocol\",\n", + " \"stochasticity\": false\n", + " },\n", + " {\n", + " \"name\": \"IV_-0.1\",\n", + " \"stimuli\": [\n", + " {\n", + " \"delay\": 20.0,\n", + " \"amp\": -0.1265624992083758,\n", + " \"thresh_perc\": null,\n", + " \"duration\": 1000.0,\n", + " \"totduration\": 1320.0,\n", + " \"holding_current\": -0.02816406195051968\n", + " }\n", + " ],\n", + " \"recordings_from_config\": [\n", + " {\n", + " \"type\": \"CompRecording\",\n", + " \"name\": \"IV_-0.1.soma.v\",\n", + " \"location\": \"soma\",\n", + " \"variable\": \"v\"\n", + " }\n", + " ],\n", + " \"validation\": false,\n", + " \"protocol_type\": \"Protocol\",\n", + " \"stochasticity\": false\n", + " }\n", + " ]\n", + "}\n" + ] + } + ], + "source": [ + "fcc_path = \"./config/features/simplecell.json\"\n", + "with open(fcc_path, 'r') as file:\n", + " fcc = json.load(file)\n", + "\n", + "print(json.dumps(fcc, indent=4))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The \"validation\": true field indicates the IDrest_0.4 protocol will only be used for validation" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setting the parameter for the optimisation" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The parameters for the optimisation are defined in `./config/simple.json`. In this case, we are optimising two parameters: ``gnabar_hh`` and ``gkbar_hh``. These parameters determine the maximum conductances for sodium and potassium ion channels in a Hodgkin-Huxley neuron model, with optimisation ranges of 0.05–0.125 (S/cm2) and 0.01–0.075 (S/cm2), respectively." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"mechanisms\": {\n", + " \"somatic\": {\n", + " \"mech\": [\n", + " \"hh\"\n", + " ]\n", + " }\n", + " },\n", + " \"distributions\": {},\n", + " \"parameters\": {\n", + " \"__comment\": \"define constants as single values and params to optimise as tuples of bounds: [lower, upper]\",\n", + " \"global\": [\n", + " {\n", + " \"name\": \"v_init\",\n", + " \"val\": -80\n", + " },\n", + " {\n", + " \"name\": \"celsius\",\n", + " \"val\": 34\n", + " }\n", + " ],\n", + " \"somatic\": [\n", + " {\n", + " \"name\": \"Ra\",\n", + " \"val\": 100\n", + " },\n", + " {\n", + " \"name\": \"cm\",\n", + " \"val\": 1\n", + " },\n", + " {\n", + " \"name\": \"ena\",\n", + " \"val\": 50\n", + " },\n", + " {\n", + " \"name\": \"ek\",\n", + " \"val\": -90\n", + " },\n", + " {\n", + " \"name\": \"gnabar_hh\",\n", + " \"val\": [\n", + " 0.05,\n", + " 0.125\n", + " ]\n", + " },\n", + " {\n", + " \"name\": \"gkbar_hh\",\n", + " \"val\": [\n", + " 0.01,\n", + " 0.075\n", + " ]\n", + " }\n", + " ]\n", + " }\n", + "}\n" + ] + } + ], + "source": [ + "emc_path = \"./config/params/simple.json\"\n", + "with open(emc_path, 'r') as file:\n", + " emc = json.load(file)\n", + "\n", + "print(json.dumps(emc, indent=4))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Running the optimisation" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can now run the optimisation process using the `EModel_pipeline` class. The seed parameter is used to set the random number generator for reproducibility. The optimisation runs for 5 generations with a population size of 20. After each optimisation round is completed, the results are saved in a pickle file for each seed within the `./checkpoints` directory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pipeline.optimise(seed=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Validating the model" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This validation step is optional but essential for verifying that the optimised model reliably represents biological behavior. It involves executing the protocols defined under the `validation_protocols` key in `recipes.json` to ensure the model's e-feature scores are within acceptable limits." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pipeline.store_optimisation_results()\n", + "pipeline.validation()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `./final.json` contains the best model across all seeds. Since we're only running a single seed here, the result will simply be that model. Now, let's check the final results" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"simplecell__1\": {\n", + " \"emodel\": \"simplecell\",\n", + " \"etype\": \"cADpyr\",\n", + " \"ttype\": null,\n", + " \"mtype\": null,\n", + " \"species\": \"rat\",\n", + " \"brain_region\": \"SSCX\",\n", + " \"iteration\": null,\n", + " \"synapse_class\": null,\n", + " \"allen_notation\": null,\n", + " \"score\": 510.2861723258447,\n", + " \"parameters\": {\n", + " \"gnabar_hh.somatic\": 0.05000000003749999,\n", + " \"gkbar_hh.somatic\": 0.07499999996750001\n", + " },\n", + " \"fitness\": {\n", + " \"IDrest_0.2.soma.v.Spikecount\": 1.1094003924504583,\n", + " \"IDrest_0.2.soma.v.mean_frequency\": 250.0,\n", + " \"IDrest_0.2.soma.v.voltage_base\": 6.86265875272435,\n", + " \"IV_-0.1.soma.v.voltage_base\": 2.3141131806698945,\n", + " \"IV_-0.1.soma.v.ohmic_input_resistance_vb_ssse\": 250.0\n", + " },\n", + " \"features\": {\n", + " \"IDrest_0.2.soma.v.Spikecount\": 1.0,\n", + " \"IDrest_0.2.soma.v.mean_frequency\": 1739.1304358817733,\n", + " \"IDrest_0.2.soma.v.voltage_base\": -82.86184220245485,\n", + " \"IDrest_0.4.soma.v.Spikecount\": 1.0,\n", + " \"IDrest_0.4.soma.v.mean_frequency\": 2105.263159505021,\n", + " \"IDrest_0.4.soma.v.voltage_base\": -82.71053576131624,\n", + " \"IV_-0.1.soma.v.voltage_base\": -84.28296205269712,\n", + " \"IV_-0.1.soma.v.ohmic_input_resistance_vb_ssse\": 1060.243113329033\n", + " },\n", + " \"validation_fitness\": {\n", + " \"IDrest_0.4.soma.v.Spikecount\": 250.0,\n", + " \"IDrest_0.4.soma.v.mean_frequency\": 250.0,\n", + " \"IDrest_0.4.soma.v.voltage_base\": 250.0\n", + " },\n", + " \"validated\": false,\n", + " \"seed\": 1,\n", + " \"pdfs\": []\n", + " }\n", + "}\n" + ] + } + ], + "source": [ + "final_path = \"./final.json\"\n", + "with open(final_path, 'r') as file:\n", + " final = json.load(file)\n", + "\n", + "print(json.dumps(final, indent=4))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- **`score`**: The global z-score for the optimized model, summing z-scores of all e-features used in optimisation (e.g., `507.86`).\n", + "\n", + "- **`parameters`**: Best hall of fame parameters of the optimised e-model\n", + "\n", + "- **`fitness`**: Z-scores for each optimized e-feature, indicating how closely the model matches experimental data. High scores (e.g., `250.0`) suggest a poor fit.\n", + "\n", + "- **`features`**: The actual numerical values of each e-feature produced by the model.\n", + "\n", + "- **`validation_fitness`**: Z-scores for validation e-features; empty (`{}`) here, indicating no validation recorded.\n", + "\n", + "- **`validated`**: Shows validation status. Here, `false` means the model didn't pass, as fitness scores must be below 5 to pass.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plotting the results" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The plots are stored in the `./figures/` directory, organised into subfolders: `efeatures_extraction` for e-feature figures by protocol, distributions for parameter distributions, `optimisation` for optimisation curves and progress, `parameter_evolution` for parameter changes over generations, `scores` for z-scores of optimised e-features, `traces` for optimised e-model traces." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pipeline.plot()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv-bpem", + "language": "python", + "name": "venv-bpem" + }, + "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.8" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}