
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/read_proxy_with_lazy_load.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_examples_read_proxy_with_lazy_load.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_read_proxy_with_lazy_load.py:


Demonstration of lazy load and proxy objects
============================================

.. GENERATED FROM PYTHON SOURCE LINES 6-60

.. code-block:: default


    import urllib
    import neo
    import quantities as pq
    import numpy as np

    url_repo = 'https://web.gin.g-node.org/NeuralEnsemble/ephy_testing_data/raw/master/'

    # Get Plexon files
    distantfile = url_repo + 'micromed/File_micromed_1.TRC'
    localfile = './File_micromed_1.TRC'
    urllib.request.urlretrieve(distantfile, localfile)

    # create a reader
    reader = neo.MicromedIO(filename='File_micromed_1.TRC')
    reader.parse_header()


    lim0, lim1 = -20 * pq.ms, +20 * pq.ms


    def apply_my_fancy_average(sig_list):
        """basic average along triggers and then channels
        here we go back to numpy with magnitude
        to be able to use np.stack
        """
        sig_list = [s.magnitude for s in sig_list]
        sigs = np.stack(sig_list, axis=0)
        return np.mean(np.mean(sigs, axis=0), axis=1)


    seg = reader.read_segment(lazy=False)
    triggers = seg.events[0]
    anasig = seg.analogsignals[0]  # here anasig contain the whole recording in memory
    all_sig_chunks = []
    for t in triggers.times:
        t0, t1 = (t + lim0), (t + lim1)
        anasig_chunk = anasig.time_slice(t0, t1)
        all_sig_chunks.append(anasig_chunk)
    m1 = apply_my_fancy_average(all_sig_chunks)


    seg = reader.read_segment(lazy=True)
    triggers = seg.events[0].load(time_slice=None)  # this load all trigers in memory
    anasigproxy = seg.analogsignals[0]  # this is a proxy
    all_sig_chunks = []
    for t in triggers.times:
        t0, t1 = (t + lim0), (t + lim1)
        anasig_chunk = anasigproxy.load(time_slice=(t0, t1))  # here real data are loaded
        all_sig_chunks.append(anasig_chunk)
    m2 = apply_my_fancy_average(all_sig_chunks)

    print(m1)
    print(m2)


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.000 seconds)


.. _sphx_glr_download_examples_read_proxy_with_lazy_load.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example


    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: read_proxy_with_lazy_load.py <read_proxy_with_lazy_load.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: read_proxy_with_lazy_load.ipynb <read_proxy_with_lazy_load.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
