Full Code of braincorp/PVM for AI

master 3de2683634f3 cached
106 files
835.1 KB
201.1k tokens
709 symbols
1 requests
Download .txt
Showing preview only (873K chars total). Download the full file or copy to clipboard to get everything.
Repository: braincorp/PVM
Branch: master
Commit: 3de2683634f3
Files: 106
Total size: 835.1 KB

Directory structure:
gitextract__0_tzylg/

├── .gitmodules
├── LICENSE.TXT
├── PVM_framework/
│   ├── AbstractExecutionManager.py
│   ├── AbstractExecutionUnit.py
│   ├── Accelerated.cpp
│   ├── Accelerated.h
│   ├── CoreUtils.py
│   ├── LowLevelCPUControlx86.pyx
│   ├── MLP.py
│   ├── PVM_Create.py
│   ├── PVM_SignalProvider.py
│   ├── PVM_Storage.py
│   ├── PVM_datasets.py
│   ├── PVM_debug_console.py
│   ├── PVM_display_helper.py
│   ├── PVM_options.py
│   ├── PVM_tracker.json
│   ├── PVM_upgrade_dict.py
│   ├── SharedArray.py
│   ├── Sync.cpp
│   ├── Sync.h
│   ├── SyncUtils.pyx
│   ├── SyncUtils_python.py
│   ├── __init__.py
│   ├── debug_logger.py
│   └── fast_routines.pyx
├── PVM_models/
│   ├── PVM_Manager.py
│   ├── PVM_plot_error.py
│   ├── PVM_run.py
│   ├── PVM_tracker.py
│   ├── PVM_unit_2step_residual_v1.py
│   ├── PVM_unit_test.py
│   ├── PVM_unit_v1.py
│   ├── __init__.py
│   ├── demo00_run.py
│   ├── demo00_unit.py
│   ├── demo01_run.py
│   ├── demo01_unit.py
│   ├── demo02_run.py
│   ├── demo02_unit.py
│   ├── demo03_run.py
│   ├── demo03_unit.py
│   ├── demo04_run.py
│   ├── demo04_unit.py
│   ├── model_zoo/
│   │   ├── classic_medium.json
│   │   ├── experiment1_face.json
│   │   ├── experiment1_green_ball.json
│   │   ├── experiment1_stop_sign.json
│   │   ├── experiment2.json
│   │   └── very_deep.json
│   ├── process_dream_data.py
│   └── run_tracking_benchmark.py
├── PVM_tests/
│   ├── test_MLP.py
│   ├── test_SharedArray.py
│   ├── test_SyncUtils.py
│   ├── test_bounding_region.py
│   ├── test_create.py
│   ├── test_fast_routines.py
│   └── test_labeled_movie.py
├── PVM_tools/
│   ├── __init__.py
│   ├── abstract_bounding_boxer.py
│   ├── abstract_tracker.py
│   ├── benchmark.py
│   ├── bounding_region.py
│   └── labeled_movie.py
├── README.md
├── add_thirdparty_tracker_submodules.sh
├── docs/
│   ├── Makefile
│   ├── conf.py
│   ├── generate.sh
│   ├── index.rst
│   └── make.bat
├── download_data.sh
├── install_local.sh
├── install_ubuntu_dependencies.sh
├── other_trackers/
│   ├── ASMSearcher.hpp
│   ├── __init__.py
│   ├── backprojection.py
│   ├── bounding_boxer.py
│   ├── center_vision_tracker.py
│   ├── cmt_vision_tracker.py
│   ├── color_vision_tracker.py
│   ├── null_vision_tracker.py
│   ├── opentld_python.cpp
│   ├── setup_opentld.py
│   ├── setup_struck.py
│   ├── struck.cpp
│   ├── struck.h
│   ├── struck_bindings.pyx
│   ├── struck_tracker.py
│   ├── test_struck_bindings.py
│   ├── test_tld_basic.py
│   └── tld_vision_tracker.py
├── pytest.ini
├── requirements.txt
├── setup.py
├── test
└── tracker_tools/
    ├── __init__.py
    ├── export_to_zip.py
    ├── images_to_PVM_pickle.py
    ├── label_PVM_pickle.py
    ├── movie_to_PVM_pickle.py
    ├── play_PVM_pickle.py
    ├── raw_to_PVM_pickle.py
    ├── scale_PVM_pickle.py
    └── upgrade_cloud_lib.py

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitmodules
================================================
[submodule "other_trackers/original_struck"]
	path = other_trackers/original_struck
	url = https://github.com/gnebehay/STRUCK
[submodule "other_trackers/original_cmt"]
	path = other_trackers/original_cmt
	url = https://github.com/gnebehay/CMT
[submodule "other_trackers/original_opentld"]
	path = other_trackers/original_opentld
	url = https://github.com/gnebehay/OpenTLD


================================================
FILE: LICENSE.TXT
================================================

License

THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.

BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.

1. Definitions

    "Collective Work" means a work, such as a periodical issue, anthology or encyclopedia, in which the Work in its entirety in unmodified form, along with one or more other contributions, constituting separate and independent works in themselves, are assembled into a collective whole. A work that constitutes a Collective Work will not be considered a Derivative Work (as defined below) for the purposes of this License.
    "Derivative Work" means a work based upon the Work or upon the Work and other pre-existing works, such as a translation, musical arrangement, dramatization, fictionalization, motion picture version, sound recording, art reproduction, abridgment, condensation, or any other form in which the Work may be recast, transformed, or adapted, except that a work that constitutes a Collective Work will not be considered a Derivative Work for the purpose of this License. For the avoidance of doubt, where the Work is a musical composition or sound recording, the synchronization of the Work in timed-relation with a moving image ("synching") will be considered a Derivative Work for the purpose of this License.
    "Licensor" means the individual, individuals, entity or entities that offer(s) the Work under the terms of this License.
    "Original Author" means the individual, individuals, entity or entities who created the Work.
    "Work" means the copyrightable work of authorship offered under the terms of this License.
    "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this License despite a previous violation.
    "License Elements" means the following high-level license attributes as selected by Licensor and indicated in the title of this License: Attribution, Noncommercial, ShareAlike.

2. Fair Use Rights. Nothing in this license is intended to reduce, limit, or restrict any rights arising from fair use, first sale or other limitations on the exclusive rights of the copyright owner under copyright law or other applicable laws.

3. License Grant. Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated below:

    to reproduce the Work, to incorporate the Work into one or more Collective Works, and to reproduce the Work as incorporated in the Collective Works;
    to create and reproduce Derivative Works provided that any such Derivative Work, including any translation in any medium, takes reasonable steps to clearly label, demarcate or otherwise identify that changes were made to the original Work. For example, a translation could be marked "The original work was translated from English to Spanish," or a modification could indicate "The original work has been modified.";
    to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission the Work including as incorporated in Collective Works;
    to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission Derivative Works;

The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights include the right to make such modifications as are technically necessary to exercise the rights in other media and formats. All rights not expressly granted by Licensor are hereby reserved, including but not limited to the rights set forth in Sections 4(e) and 4(f).

4. Restrictions. The license granted in Section 3 above is expressly made subject to and limited by the following restrictions:

    You may distribute, publicly display, publicly perform, or publicly digitally perform the Work only under the terms of this License, and You must include a copy of, or the Uniform Resource Identifier for, this License with every copy or phonorecord of the Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Work that restrict the terms of this License or the ability of a recipient of the Work to exercise the rights granted to that recipient under the terms of the License. You may not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties. When You distribute, publicly display, publicly perform, or publicly digitally perform the Work, You may not impose any technological measures on the Work that restrict the ability of a recipient of the Work from You to exercise the rights granted to that recipient under the terms of the License. This Section 4(a) applies to the Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Work itself to be made subject to the terms of this License. If You create a Collective Work, upon notice from any Licensor You must, to the extent practicable, remove from the Collective Work any credit as required by Section 4(d), as requested. If You create a Derivative Work, upon notice from any Licensor You must, to the extent practicable, remove from the Derivative Work any credit as required by Section 4(d), as requested.
    You may distribute, publicly display, publicly perform, or publicly digitally perform a Derivative Work only under: (i) the terms of this License; (ii) a later version of this License with the same License Elements as this License; or, (iii) either the unported Creative Commons license or a Creative Commons license for another jurisdiction (either this or a later license version) that contains the same License Elements as this License (e.g. Attribution-NonCommercial-ShareAlike 3.0 (Unported)) ("the Applicable License"). You must include a copy of, or the Uniform Resource Identifier for, the Applicable License with every copy or phonorecord of each Derivative Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Derivative Works that restrict the terms of the Applicable License or the ability of a recipient of the Work to exercise the rights granted to that recipient under the terms of the Applicable License. You must keep intact all notices that refer to the Applicable License and to the disclaimer of warranties. When You distribute, publicly display, publicly perform, or publicly digitally perform the Derivative Work, You may not impose any technological measures on the Derivative Work that restrict the ability of a recipient of the Derivative Work from You to exercise the rights granted to that recipient under the terms of the Applicable License. This Section 4(b) applies to the Derivative Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Derivative Work itself to be made subject to the terms of the Applicable License.
    You may not exercise any of the rights granted to You in Section 3 above in any manner that is primarily intended for or directed toward commercial advantage or private monetary compensation. The exchange of the Work for other copyrighted works by means of digital file-sharing or otherwise shall not be considered to be intended for or directed toward commercial advantage or private monetary compensation, provided there is no payment of any monetary compensation in connection with the exchange of copyrighted works.
    If You distribute, publicly display, publicly perform, or publicly digitally perform the Work (as defined in Section 1 above) or any Derivative Works (as defined in Section 1 above) or Collective Works (as defined in Section 1 above), You must, unless a request has been made pursuant to Section 4(a), keep intact all copyright notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or pseudonym, if applicable) if supplied, and/or (ii) if the Original Author and/or Licensor designate another party or parties (e.g. a sponsor institute, publishing entity, journal) for attribution ("Attribution Parties") in Licensor's copyright notice, terms of service or by other reasonable means, the name of such party or parties; the title of the Work if supplied; to the extent reasonably practicable, the Uniform Resource Identifier, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to the copyright notice or licensing information for the Work; and, consistent with Section 3(b) in the case of a Derivative Work, a credit identifying the use of the Work in the Derivative Work (e.g., "French translation of the Work by Original Author," or "Screenplay based on original Work by Original Author"). The credit required by this Section 4(d) may be implemented in any reasonable manner; provided, however, that in the case of a Derivative Work or Collective Work, at a minimum such credit will appear, if a credit for all contributing authors of the Derivative Work or Collective Work appears, then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors. For the avoidance of doubt, You may only use the credit required by this Section for the purpose of attribution in the manner set out above and, by exercising Your rights under this License, You may not implicitly or explicitly assert or imply any connection with, sponsorship or endorsement by the Original Author, Licensor and/or Attribution Parties, as appropriate, of You or Your use of the Work, without the separate, express prior written permission of the Original Author, Licensor and/or Attribution Parties.

    For the avoidance of doubt, where the Work is a musical composition:
        Performance Royalties Under Blanket Licenses. Licensor reserves the exclusive right to collect whether individually or, in the event that Licensor is a member of a performance rights society (e.g. ASCAP, BMI, SESAC), via that society, royalties for the public performance or public digital performance (e.g. webcast) of the Work if that performance is primarily intended for or directed toward commercial advantage or private monetary compensation.
        Mechanical Rights and Statutory Royalties. Licensor reserves the exclusive right to collect, whether individually or via a music rights agency or designated agent (e.g. Harry Fox Agency), royalties for any phonorecord You create from the Work ("cover version") and distribute, subject to the compulsory license created by 17 USC Section 115 of the US Copyright Act (or the equivalent in other jurisdictions), if Your distribution of such cover version is primarily intended for or directed toward commercial advantage or private monetary compensation.
    Webcasting Rights and Statutory Royalties. For the avoidance of doubt, where the Work is a sound recording, Licensor reserves the exclusive right to collect, whether individually or via a performance-rights society (e.g. SoundExchange), royalties for the public digital performance (e.g. webcast) of the Work, subject to the compulsory license created by 17 USC Section 114 of the US Copyright Act (or the equivalent in other jurisdictions), if Your public digital performance is primarily intended for or directed toward commercial advantage or private monetary compensation.

5. Representations, Warranties and Disclaimer

UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND ONLY TO THE EXTENT OF ANY RIGHTS HELD IN THE LICENSED WORK BY THE LICENSOR. THE LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE WORK, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MARKETABILITY, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU.

6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

7. Termination

    This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License. Individuals or entities who have received Derivative Works (as defined in Section 1 above) or Collective Works (as defined in Section 1 above) from You under this License, however, will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License.
    Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time; provided, however that any such election will not serve to withdraw this License (or any other license that has been, or is required to be, granted under the terms of this License), and this License will continue in full force and effect unless terminated as stated above.

8. Miscellaneous

    Each time You distribute or publicly digitally perform the Work (as defined in Section 1 above) or a Collective Work (as defined in Section 1 above), the Licensor offers to the recipient a license to the Work on the same terms and conditions as the license granted to You under this License.
    Each time You distribute or publicly digitally perform a Derivative Work, Licensor offers to the recipient a license to the original Work on the same terms and conditions as the license granted to You under this License.
    If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this License, and without further action by the parties to this agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
    No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or consent shall be in writing and signed by the party to be charged with such waiver or consent.
    This License constitutes the entire agreement between the parties with respect to the Work licensed here. There are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall not be bound by any additional provisions that may appear in any communication from You. This License may not be modified without the mutual written agreement of the Licensor and You.



================================================
FILE: PVM_framework/AbstractExecutionManager.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================

import abc


class ExecutionManager(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def __init__(self):
        pass

    @abc.abstractmethod
    def start(self):
        """
        This will be called right before the simulation starts
        """
        pass

    @abc.abstractmethod
    def fast_action(self):
        """
        This is the time between steps of execution
        Data is consistent, but keep this piece absolutely minimal
        """
        pass

    @abc.abstractmethod
    def slow_action(self):
        """
        This is while the workers are running. You may do a lot of work here
        (preferably not more than the time of execution of workers).
        """
        pass

    @abc.abstractmethod
    def running(self):
        """
        While returning True the simulation will keep going.
        """

    @abc.abstractmethod
    def finish(self):
        """
        Called at the end of execution.
        """


class AbstractSignalProvider(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def __init__(self):
        pass

    @abc.abstractmethod
    def start(self):
        """
        This will be called right before the simulation starts
        """
        pass

    @abc.abstractmethod
    def get_signal(self, name, time):
        """
        Get the signal for the current time step if time is zero, or relative if time is non zero
        """
        pass

    @abc.abstractmethod
    def advance(self):
        """
        Move to the next timestep
        """
        pass

    @abc.abstractmethod
    def finish(self):
        """
        Called at the end of execution.
        """


================================================
FILE: PVM_framework/AbstractExecutionUnit.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================

import abc


class ExecutionUnit(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def execution_steps(cls):
        """
        The method needs to have sufficient number of execute methods
        Note, this needs to be extended as a class method
        """
        pass

    @abc.abstractmethod
    def __init__(self, parameters):
        pass

    @abc.abstractmethod
    def generate_missing_parameters(parameters, options):
        """
        This method can be called to generate all the missing dictionary parameters when all
        the other relevant viariables are known. Needs to be extended as a static method.
        :return:
        """
        pass

    @abc.abstractmethod
    def execute0(self):
        """
        This method will do the execution. It is nescessary, but you can implement more, like execute1 and so on.
        Their execution will be interleaved with a barrier.
        :return:
        """
        pass

    @abc.abstractmethod
    def cleanup(self):
        """
        WIll be called before the simulation is finished. Allows to copy back any remaining state into
        the share memory objects
        :return:
        """
        pass


================================================
FILE: PVM_framework/Accelerated.cpp
================================================
/*# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================*/

#include "Accelerated.h"

// Uncomment below for manual loop unrolling
#define UNROLL_LOOP

// Computes mult * vector . matrix.T -> result
void dot_transpose(double* mult, double* vector, int vect_shape_0, double* matrix, int mat_shape0, int mat_shape1, double* result)
{
   for(int i=0; i<mat_shape0; i++)
   {
      int j=0;
      double s0=0;
      int i_mat_shape = i * mat_shape1;
      #ifdef UNROLL_LOOP
      double s1=0;
      double s2=0;
      double s3=0;
      double s4=0;
      double s5=0;
      double s6=0;
      double s7=0;
      for(; j<vect_shape_0-8; j+=8)
      {
         int mat_idx = i_mat_shape+j;
         s0+=vector[j]*matrix[mat_idx];
         s1+=vector[j+1]*matrix[mat_idx+1];
         s2+=vector[j+2]*matrix[mat_idx+2];
         s3+=vector[j+3]*matrix[mat_idx+3];
         s4+=vector[j+4]*matrix[mat_idx+4];
         s5+=vector[j+5]*matrix[mat_idx+5];
         s6+=vector[j+6]*matrix[mat_idx+6];
         s7+=vector[j+7]*matrix[mat_idx+7];
      }
      #endif
      for(; j<vect_shape_0; j++)
      {
         int mat_idx = i_mat_shape+j;
         s0+=vector[j]*matrix[mat_idx];
      }
      #ifdef UNROLL_LOOP
      result[i]=mult[i]*(s0+s1+s2+s3+s4+s5+s6+s7);
      #else
      result[i]=mult[i]*s0;
      #endif
   }
}

// Computes vector . matrix.T -> result
void dot_transpose_simple(double* vector, int vect_shape_0, double* matrix, int mat_shape0, int mat_shape1, double* result)
{
   for(int i=0; i<mat_shape0; i++)
   {
      int j=0;
      double s0=0;
      int i_mat_shape = i * mat_shape1;
      #ifdef UNROLL_LOOP
      double s1=0;
      double s2=0;
      double s3=0;
      double s4=0;
      double s5=0;
      double s6=0;
      double s7=0;
      for(; j<vect_shape_0-8; j+=8)
      {
         int mat_idx = i_mat_shape+j;
         s0+=vector[j]*matrix[mat_idx];
         s1+=vector[j+1]*matrix[mat_idx+1];
         s2+=vector[j+2]*matrix[mat_idx+2];
         s3+=vector[j+3]*matrix[mat_idx+3];
         s4+=vector[j+4]*matrix[mat_idx+4];
         s5+=vector[j+5]*matrix[mat_idx+5];
         s6+=vector[j+6]*matrix[mat_idx+6];
         s7+=vector[j+7]*matrix[mat_idx+7];
      }
      #endif
      for(; j<vect_shape_0; j++)
      {
         int mat_idx = i_mat_shape+j;
         s0+=vector[j]*matrix[mat_idx];
      }
      #ifdef UNROLL_LOOP
      result[i]=(s0+s1+s2+s3+s4+s5+s6+s7);
      #else
      result[i]=s0;
      #endif
   }
}

// Computes vector * (1 - vector) * vector2 -> result
void derivative_dot(double* vector, double* vector2, int vect_shape_0, double* result)
{
   int j=0;
   for(; j<vect_shape_0; j++)
   {
      result[j] = vector[j] * (1 - vector[j]) * vector2[j];
   }
}


void derivative_dot_poly(double* vector, double* vector2, int vect_shape_0, double* result)
{
   int j=0;
   double x;
   for(; j<vect_shape_0; j++)
   {
      if (vector[j]>=0.5) {
         x=(2*vector[j]-1)/(1-(2*vector[j]-1));
      } else {
         x=(2*vector[j]-1)/(1+(2*vector[j]-1));
      }
      x=fabs(x)+1;
      result[j] =(1.0/(2*(x*x))) * vector2[j];
   }
}

// Computes alpha * vector1 x vector2 + beta*matrix -> result
void generalized_outer(double alpha, double * vector1, int vect1_shape, double * vector2, int vect2_shape, double beta, double* matrix, double* result)
{
   for(int i=0; i<vect1_shape; i++)
   {
      int j=0;
      int i_vect = i*vect2_shape;
      #ifdef UNROLL_LOOP
      for(; j<vect2_shape-8; j+=8)
      {
         int mat_ind = i_vect+j;
         result[mat_ind]=alpha*vector1[i]*vector2[j]+beta*matrix[mat_ind];
         result[mat_ind+1]=alpha*vector1[i]*vector2[j+1]+beta*matrix[mat_ind+1];
         result[mat_ind+2]=alpha*vector1[i]*vector2[j+2]+beta*matrix[mat_ind+2];
         result[mat_ind+3]=alpha*vector1[i]*vector2[j+3]+beta*matrix[mat_ind+3];
         result[mat_ind+4]=alpha*vector1[i]*vector2[j+4]+beta*matrix[mat_ind+4];
         result[mat_ind+5]=alpha*vector1[i]*vector2[j+5]+beta*matrix[mat_ind+5];
         result[mat_ind+6]=alpha*vector1[i]*vector2[j+6]+beta*matrix[mat_ind+6];
         result[mat_ind+7]=alpha*vector1[i]*vector2[j+7]+beta*matrix[mat_ind+7];
      }
      #endif
      for(; j<vect2_shape; j++)
      {
         int mat_ind = i_vect+j;
         result[mat_ind]=alpha*vector1[i]*vector2[j]+beta*matrix[mat_ind];
      }
   }
}

void dot_sigmoid(double* vector, double* matrix, int mat_shape0, int mat_shape1, double beta, double * result, int append_bias)
{
   for(int i=0; i<mat_shape1; i++)
   {
      int j=0;
      double s0=0.0;
      #ifdef UNROLL_LOOP
      double s1=0.0;
      double s2=0.0;
      double s3=0.0;
      double s4=0.0;
      double s5=0.0;
      double s6=0.0;
      double s7=0.0;
      for (; j<mat_shape0-8; j+=8)
      {
         int mat_ind = j*mat_shape1+i;
         s0+=vector[j]*matrix[mat_ind];
         s1+=vector[j+1]*matrix[mat_ind+1*mat_shape1];
         s2+=vector[j+2]*matrix[mat_ind+2*mat_shape1];
         s3+=vector[j+3]*matrix[mat_ind+3*mat_shape1];
         s4+=vector[j+4]*matrix[mat_ind+4*mat_shape1];
         s5+=vector[j+5]*matrix[mat_ind+5*mat_shape1];
         s6+=vector[j+6]*matrix[mat_ind+6*mat_shape1];
         s7+=vector[j+7]*matrix[mat_ind+7*mat_shape1];

      }
      #endif
      if (append_bias!=0)
      {
         for (; j<mat_shape0-1; j++)
         {
            int mat_ind = j*mat_shape1+i;
            s0+=vector[j]*matrix[mat_ind];
         }
         s0+=1.0*matrix[(mat_shape0-1)*mat_shape1+i];
      }
      else
      {
         for (; j<mat_shape0; j++)
         {
            int mat_ind = j*mat_shape1+i;
            s0+=vector[j]*matrix[mat_ind];
         }
      }
      #ifdef UNROLL_LOOP
      result[i]=1.0/(1.0+exp(-beta*(s0+s1+s2+s3+s4+s5+s6+s7)));
      #else
      result[i]=1.0/(1.0+exp(-beta*(s0)));
      #endif

   }
}

void dot_sigmoid_poly(double* vector, double* matrix, int mat_shape0, int mat_shape1, double beta, double * result, int append_bias)
{
   for(int i=0; i<mat_shape1; i++)
   {
      int j=0;
      double s0=0.0;
      #ifdef UNROLL_LOOP
      double s1=0.0;
      double s2=0.0;
      double s3=0.0;
      double s4=0.0;
      double s5=0.0;
      double s6=0.0;
      double s7=0.0;
      for (; j<mat_shape0-8; j+=8)
      {
         int mat_ind = j*mat_shape1+i;
         s0+=vector[j]*matrix[mat_ind];
         s1+=vector[j+1]*matrix[mat_ind+1*mat_shape1];
         s2+=vector[j+2]*matrix[mat_ind+2*mat_shape1];
         s3+=vector[j+3]*matrix[mat_ind+3*mat_shape1];
         s4+=vector[j+4]*matrix[mat_ind+4*mat_shape1];
         s5+=vector[j+5]*matrix[mat_ind+5*mat_shape1];
         s6+=vector[j+6]*matrix[mat_ind+6*mat_shape1];
         s7+=vector[j+7]*matrix[mat_ind+7*mat_shape1];

      }
      #endif
      if (append_bias!=0)
      {
         for (; j<mat_shape0-1; j++)
         {
            int mat_ind = j*mat_shape1+i;
            s0+=vector[j]*matrix[mat_ind];
         }
         s0+=1.0*matrix[(mat_shape0-1)*mat_shape1+i];
      }
      else
      {
         for (; j<mat_shape0; j++)
         {
            int mat_ind = j*mat_shape1+i;
            s0+=vector[j]*matrix[mat_ind];
         }
      }
      #ifdef UNROLL_LOOP
      s0=s0+s1+s2+s3+s4+s5+s6+s7;
      #else
      #endif
      result[i]=(s0/(2*(fabs(s0)+1)))+0.5;

   }
}

void dot_add(double* vector, double* matrix, int mat_shape0, int mat_shape1, double * result, int append_bias)
{
   for(int i=0; i<mat_shape1; i++)
   {
      int j=0;
      double s0=0.0;
      #ifdef UNROLL_LOOP
      double s1=0.0;
      double s2=0.0;
      double s3=0.0;
      double s4=0.0;
      double s5=0.0;
      double s6=0.0;
      double s7=0.0;
      for (; j<mat_shape0-8; j+=8)
      {
         int mat_ind = j*mat_shape1+i;
         s0+=vector[j]*matrix[mat_ind];
         s1+=vector[j+1]*matrix[mat_ind+1*mat_shape1];
         s2+=vector[j+2]*matrix[mat_ind+2*mat_shape1];
         s3+=vector[j+3]*matrix[mat_ind+3*mat_shape1];
         s4+=vector[j+4]*matrix[mat_ind+4*mat_shape1];
         s5+=vector[j+5]*matrix[mat_ind+5*mat_shape1];
         s6+=vector[j+6]*matrix[mat_ind+6*mat_shape1];
         s7+=vector[j+7]*matrix[mat_ind+7*mat_shape1];

      }
      #endif
      if (append_bias!=0)
      {
         for (; j<mat_shape0-1; j++)
         {
            int mat_ind = j*mat_shape1+i;
            s0+=vector[j]*matrix[mat_ind];
         }
         s0+=1.0*matrix[(mat_shape0-1)*mat_shape1+i];
      }
      else
      {
         for (; j<mat_shape0; j++)
         {
            int mat_ind = j*mat_shape1+i;
            s0+=vector[j]*matrix[mat_ind];
         }
      }
      #ifdef UNROLL_LOOP
      s0=s0+s1+s2+s3+s4+s5+s6+s7;
      #else
      #endif
      result[i]+=s0;
   }
}

void sigmoid_poly(double* result, int shape, double beta)
{
   for(int i=0; i<shape; i++)
   {
      double s0 = result[i];
      result[i]=(s0/(2*(fabs(s0)+1)))+0.5;
   }
}

void sigmoid(double* result, int shape, double beta)
{
   for(int i=0; i<shape; i++)
   {
      double s0 = result[i];
      result[i]=1.0/(1.0+exp(-beta*(s0)));
   }
}

================================================
FILE: PVM_framework/Accelerated.h
================================================
/*# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================
*/

#include <math.h>

void dot_transpose(double *mult, double* vector, int vect_shape_0, double* matrix, int mat_shape0, int mat_shape1, double* result);
void dot_transpose_simple(double* vector, int vect_shape_0, double* matrix, int mat_shape0, int mat_shape1, double* result);
void derivative_dot(double* vector, double* vector2, int vect_shape_0, double* result);
void derivative_dot_poly(double* vector, double* vector2, int vect_shape_0, double* result);
void generalized_outer(double alpha, double * vector1, int vect1_shape, double * vector2, int vect2_shape, double beta, double* matrix, double* result);
void dot_sigmoid(double* vector, double* matrix, int mat_shape0, int mat_shape1, double beta, double * result, int append_bias);
void dot_sigmoid_poly(double* vector, double* matrix, int mat_shape0, int mat_shape1, double beta, double * result, int append_bias);
void dot_add(double* vector, double* matrix, int mat_shape0, int mat_shape1,  double * result, int append_bias);
void sigmoid_poly(double* result, int shape, double beta);
void sigmoid(double* result, int shape, double beta);

================================================
FILE: PVM_framework/CoreUtils.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================

import cPickle
import gzip
import sys
import time
import multiprocessing as mp
import socket
import threading
import PVM_framework.PVM_debug_console as PVM_debug_console
import os
import debug_logger
import logging
import glob
import importlib
import random
import datetime
try:
    import PVM_framework.SyncUtils as SyncUtils
except:
    import PVM_framework.SyncUtils_python as SyncUtils
import pickle
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

# Legacy class/module renaming
renametable = {'future_encoder_framework.SharedArray': 'PVM_framework.SharedArray',
               'PVM_tools.labeled_movie': 'PVM_tools.labeled_movie',
               'PVM_tools.bounding_region': 'PVM_tools.bounding_region',
               }


def mapname(name):
    if name in renametable:
        return renametable[name]
    return name


def mapped_load_global(self):
    module = mapname(self.readline()[:-1])
    name = mapname(self.readline()[:-1])
    klass = self.find_class(module, name)
    self.append(klass)


def load_legacy_pickle(str):
    file = StringIO(str)
    unpickler = pickle.Unpickler(file)
    unpickler.dispatch[pickle.GLOBAL] = mapped_load_global
    return unpickler.load()


def load_legacy_pickle_file(file):
    unpickler = pickle.Unpickler(file)
    unpickler.dispatch[pickle.GLOBAL] = mapped_load_global
    return unpickler.load()


def save_model(pobject, filename, protocol=-1):
    """
    Save an object to a compressed disk file.

    :param pobject: serializable python object
    :type pobject: object
    :param filename: string representing a path to a file
    :type filename: str
    :param protocol: optional protocol parameter (defaults to -1 which is the latest available)
    :type protocol: int
    """
    logging.info("Saving a model to a file " + str(filename))
    pfile = gzip.GzipFile(filename, 'wb')
    cPickle.dump(pobject, pfile, protocol)
    pfile.close()
    logging.info("Saved a model to a file " + str(filename))
    logging.info("Warning: Saved to a python pickle. Note, if you ever use this code for production consider using other "
                 "serializing methods. Pickle is python specific and may pose a security threat")


def load_model(filename):
    """
    Loads a compressed object from disk

    :param filename: string representing a path to a file
    :type filename: str
    :return: python object loaded from file
    :rtype: object
    """
    logging.info("Loading a model from a file " + str(filename))
    pfile = gzip.GzipFile(filename, 'rb')
    prop_dict = load_legacy_pickle(pfile.read())
    pfile.close()
    logging.info("Loaded a model from a file " + str(filename))
    logging.info("Warning: Loaded from a python pickle. Note, if you ever use this code for production consider using other "
                 "serializing methods. Pickle is python specific and may pose a security threat")
    return prop_dict


def _worker_code(prop_dict, proc_id, barrier):
    """
    The code executed by a single worker process, instantiates and
    executes a subset of all the objects described in the dictionary,
    synchronizes with other workers and the supervisor thread.

    :param prop_dict: Dictionary with simulation data
    :param proc_id: process id provided by the parrent (not the unix pid)
    :param barrier: barrier object used for synchronization
    :return:
    """
    try:
        import PVM_framework.SyncUtils as SyncUtils
    except:
        import PVM_framework.SyncUtils_python as SyncUtils
    # Set the CPU flags
    try:
        import PVM_framework.LowLevelCPUControlx86 as LowLevelCPUControlx86
        LowLevelCPUControlx86.set_flush_denormals()
        # Flush denormals will collapse all floating point operation results
        # to zero if they are to small to fit in the normal float representation.
        # Although such action may slighly affect the precission of calculations
        # on some processors (e.g. x86) it substantially speeds up the execution
    except:
        # Unable to set the flags
        print "Setting flush denormals CPU flag not available"
    import numpy as np
    np.random.seed(proc_id)
    # Load the execution module
    while True:  # bacause of an aparent race condition, the first load may not be succesfull,
        try:
            unit = importlib.import_module(prop_dict['execution_unit_module'])
        except:
            continue
        break
    steps = int(unit.ExecutionUnit.execution_steps())
    if barrier.worker_barrier() is False:
        sys.exit(0)
    # Instantiate all the objects
    stages = prop_dict['stages']
    objects = {}  # Local objects of the worker
    calls = {}
    max_procs = prop_dict['num_proc']
    for stagenum in xrange(stages):
        stage = 'stage%d' % stagenum
        total_stage_elements = prop_dict['stage%d_size' % stagenum]
        my_stage_elements = range(proc_id, total_stage_elements, max_procs)
        objects[stage] = {}
        calls[stage] = {}
        objects[stage]['my_elements'] = my_stage_elements
        objects[stage]['units'] = []
        for i in my_stage_elements:
            Cunit = unit.ExecutionUnit(parameters=prop_dict[stage][i])
            objects[stage]['units'].append(Cunit)
        calls[stage] = []
        for j in range(steps):
            calls[stage].append([])
            for (i, _) in enumerate(objects[stage]['my_elements']):
                method = getattr(objects[stage]['units'][i], "execute%d" % j)
                calls[stage][j].append(method)

    # The main loop
    quit_now = False
    while True:
        for stagenum in range(stages):
            stage = 'stage%d' % stagenum
            for j in range(steps):
                while prop_dict['paused'][0] == 1:
                    time.sleep(1)
                for method in calls[stage][j]:
                    method()
                if barrier.worker_barrier() is False:
                    quit_now = True
                    break
            if quit_now:
                break
        if quit_now:
            for stagenum in range(stages):
                stage = 'stage%d' % stagenum
                for (i, _) in enumerate(objects[stage]['my_elements']):
                    objects[stage]['units'][i].cleanup()
            sys.exit(0)


def _supervisor_run(prop_dict, control_barrier):
    """
    This is the supervisor (parent class) code. For efficiency reasons the loop
    that synchronized the worker nodes is not run in the main thread. Main thread
    can perform actions like window display etc. in a loosely synchronized manner with the
    rest of the execution.

    [MAIN THREAD]       [ SUPERVISOR ]        [ WORKER 0]   ...
    runs manager          runs the  -------->  executes
    displays windows      main loop.           the unit
    adjust dict params.   Syncs to             code
    Syncs to the          workers  --------->
    supervisor     <----> and main thread

    :param prop_dict: dictionary with simulation memory and parameters
    :param control_barrier: barrier object used for synchronization with the main thread
    :return:
    """
    tryonce = True
    while True:
        try:
            unit = importlib.import_module(prop_dict['execution_unit_module'])
        except:
            if tryonce:
                logging.error("Could not load module %s, going insane in infinite loop. Kill me." % prop_dict['execution_unit_module'])
                tryonce = False
            continue
        break
    logging.info("Loaded the module %s, no reason to kill me yet." % prop_dict['execution_unit_module'])
    steps = unit.ExecutionUnit.execution_steps()
    barrier = SyncUtils.Barrier(prop_dict['num_proc'])
    procs = []
    for i in xrange(prop_dict['num_proc']):
        p = mp.Process(target=_worker_code, args=(prop_dict, i, barrier))
        p.start()
        procs.append(p)
    logging.info("Created workers, no one started yet")
    sys.stdout.flush()
    time.sleep(1)
    sys.stdout.flush()
    barrier.parent_barrier()
    N = prop_dict['N'][0]
    control_barrier.worker_barrier()
    logging.info("Starting workers")
    while barrier.workers_running() > 0:
        # Main execution loop

        for i in range(prop_dict['stages']):
            for j in range(steps):
                barrier.resume_workers()
                while prop_dict['paused'][0] == 1:
                    time.sleep(1)
                barrier.parent_barrier()
        N += 1
        prop_dict['N'][0] = N
        running = control_barrier.worker_barrier()
        if not running:
            prop_dict['paused'][0] = 0
            barrier.resume_workers()
            barrier.parent_barrier(quit_workers=True)
            logging.info("Quitting")
            sys.stdout.flush()
            break
    logging.info("All have finished")
    sys.stdout.flush()
    for p in procs:
        p.join()


def _run_debug_session(socket, prop_dict):
    """
    Starts a debugging session

    :param socket: Network socket on which the session is transmitted
    :param prop_dict: Simulation dictionary of the currently running simulation
    :return:
    """
    file = socket.makefile(mode="rw")
    print >>file, ""
    print >>file, "Predictive Vision Framework version 1.0"
    print >>file, "(C) 2016 Brain Corporation Technical Services"
    print >>file, ""
    print >>file, "You are connected to a debug shell"
    print >>file, "Type 'help' for available commands, 'quit' to exit debug shell"
    explorer = PVM_debug_console.InteractiveDictionaryExplorer(stdin=file, stdout=file, dict=prop_dict, infilename="engine"+str(os.getpid()))
    explorer.cmdloop()
    logging.info("Ended a remote debug session")
    socket.close()


def _monitor_debug_session(port, stop):
    """
    Separate thread code to monitor the debug session and end it if
    the stop() call is true. This is necessary because the debug server might be
    stuck on listen. When the simulation is finished (via any external event) the thread
    processing the server needs to be waken up from the listen mode. The function below accomplishes
    that by periodically trying to connect to it.

    :param port: Port on which the session is running
    :param stop: A function returning a bool value of whether the session should be stopped
    :return:
    """
    while 1:
        time.sleep(0.5)
        if stop():
            S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            S.connect(("localhost", port))
            S.close()
            logging.info("Closing the server socket")
            break


def _run_debug_server(prop_dict, port, stop):
    """
    Sets up a debug server at a given port

    :param prop_dict: Simulation dictionary of the currently running simulation
    :param port: Port on which to setup the debug server
    :param stop: A function returning a bool value of whether the session should be stopped
    :return:
    """
    serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    while True:
        try:
            serversock.bind(("", port))
            break
        except:
            logging.error("Port %d already taken, trying to bind to port %d" % (port, port +1))
            port += 1
            continue
    serversock.listen(5)
    logging.info("Listening on port " + str(port) + " for debug connections")
    clients = []
    monitor = threading.Thread(target=_monitor_debug_session, args=(port, stop))
    monitor.start()
    while 1:
        clientsock, addr = serversock.accept()
        if stop():
            logging.info("Exiting debug session")
            sys.stdout.flush()
            break
        logging.info("Accepted a debug connection from " + str(addr))
        clients.append(threading.Thread(target=_run_debug_session, args=(clientsock, prop_dict)))
        clients[-1].start()
    monitor.join()


class ModelExecution(object):
    """
    ModelExecution is a class allowing to control the execution of a model from the main thread.

    It takes a dictionary and a manager and allows for two modes of operation:

    1. blocking. The start call will block until the execution is complete (as decided by the manager)
    2. non blocking. Allows the main thread to control the execution step by step.

    :param prop_dict: Dictionary containing model data
    :type prop_dict: dict
    :param manager: Manager object controlling the aspects of execution
    :type AbstractExecutionManager: ExecutionManager
    """
    def __init__(self, prop_dict, manager, port=9000):
        self._prop_dict = prop_dict
        self._manager = manager
        self._port = port

    def start(self, blocking=True):
        """
        Begins the execution of the model. If blocking is True, the call will block the main thread
        until the model execution is complete. Otherwise the call will return and allow for stepped
        execution.

        .. note::

            If running in non blocking mode, the call will return, but all the necessary execution threads will
            be running. Since the system uses busy synchronization (Spin Locks) the CPU utilization after the call
            will be maximal. In order to maximize machine utilization, step() should be called as fast as possible.


        :Example:
        ::

            manager = Manager(dict, 1000)
            executor = CoreUtils.ModelExecution(prop_dict=dict, manager=manager)
            executor.start(blocking=True)
            CoreUtils.save_model(dict, filename)
            print("Saving and running again in non blocking mode")
            executor.start(blocking=False)
            while manager.running():
                executor.step()
            executor.finish()
            CoreUtils.save_model(dict, filename)


        :param blocking: Flag determining the execution mode, True by default
        :type blocking: bool
        """
        self._logger = debug_logger.DebugLogger(self._prop_dict)
        self._parent_control_barrier = SyncUtils.Barrier(1, timeout=0.0001, UseSpinLock=True)
        self._parent_proc = mp.Process(target=_supervisor_run, args=(self._prop_dict, self._parent_control_barrier))
        self._parent_proc.start()
        self._parent_control_barrier.parent_barrier()
        self._manager.start()
        self._finished=False
        self._stop_server = False
        self._t = threading.Thread(target=_run_debug_server, args=(self._prop_dict, self._port, lambda: self._stop_server))
        self._t.start()
        if blocking:
            while self._manager.running():
                self.step()
            self.finish()

    def step(self):
        """
        Executes a single step of simulation
        """
        # Quick turnover, data here is consistent
        self._logger.process_hookups()
        self._manager.fast_action()
        self._parent_control_barrier.resume_workers()
        # Workers execute
        # Longer turnover, data here might be inconsistent
        self._manager.slow_action()
        self._parent_control_barrier.parent_barrier(quit_workers=not self._manager.running())

    def finish(self):
        """
        After a session has been completed, finish removes all worker threads and closes debug server.
        """
        if not self._finished:
            logging.info("Attempting to stop threads")
            self._stop_server = True
            logging.info("Attempting to join parent")
            self._parent_proc.join()
            logging.info("attempting to join thread")
            self._t.join()
            self._manager.finish()
            self._finished=True


def run_model(prop_dict, manager, port=9000):
    """
    Simplified way of running a model.
    :param prop_dict: model dictionary
    :param manager: execution manager
    :param port: port for setting up a debugging server
    :return:
    """
    executor = ModelExecution(prop_dict=prop_dict, manager=manager, port=port)
    executor.start(blocking=True)
    executor.finish()


================================================
FILE: PVM_framework/LowLevelCPUControlx86.pyx
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================

cdef extern from "xmmintrin.h":
    void _mm_setcsr(unsigned int)
    unsigned int _mm_getcsr()


def set_flush_denormals():
    """
    This call will modify the Control Status Register (CSR) to instruct the CPU to flush denormals.
    A denormal representation of a float is such that is smaller then the smallest actual representation,
    with significand grater or equal to one say e.g.:

    1.0000000xE-50

    With the maximal negative exponent, this looks like the smallest representable number but it is not, since
    one can increase the precission by decreasing the value of significand:

    0.0000001xE-50

    is a few orders of magnitude smaller, but it is not a "normal" floating point represenation. Such denormal
    representations may appear form arithmetic operations, some architectures would flush such small numbers to zero.
    However x86 will by default keep them and perform further arithmetic operations, at the cost of very significant
    slowdown.

    Such slowdown may cause more harm (particularly in a parallel system) than the benefit of increased precision.

    This extension will only build on x86. Flush to zero is default on ARM architecture.
    """
    _mm_setcsr((_mm_getcsr() & ~0x0040) | (0x0040))


================================================
FILE: PVM_framework/MLP.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================

import numpy as np
import abc
import PVM_framework.fast_routines as fast_routines
import PVM_framework.SharedArray as SharedArray
import time

try:
    @profile
    def test():
        pass
except:
    def profile(fun):
        return fun


def random_ortho(n):
    """ Generates a random orthogonal matrix n x n

    :param n: dimesion
    :type n: int

    """
    A = np.mat(np.random.random((n, n)))
    Q, R = np.linalg.qr(A)
    return Q


def random_with_singular_values(m, n, singular_values):
    """
    Generates a random matrix with given singular values

    :param m: first dimension of the array
    :type m: int
    :param n: second dimension of the array
    :type n: int
    :param singular_values: value of the singuar values of the generated matrix
    :type singular_values: float
    :return: matrix
    :rtype: np.ndarray
    """
    Q = random_ortho(m)
    singular_values=np.array(singular_values)
    svs = singular_values.shape[0]
    if svs < max(n, m):
        singular_values = np.concatenate((singular_values, np.array([0] * (max(n, m)-svs))))
    D = np.diag(singular_values)
    V = random_ortho(n)
    M = Q*D[:m, :n]*V
    return np.array(M)


def optimal_weight_initialization(m, n, singular_value=1.1):
    """ Optimal weight matrix initialization """
    return random_with_singular_values(m, n, [singular_value] * min(m, n))


class MLPPrototype(object):
    """
    MLPPrototype is an abstract class to be extended by particular implementations of
    the multi layer perceptron.
    """
    def __init__(self, state):
        pass

    @abc.abstractmethod
    def train(self, inputs, targets):
        """
        Pass and train one vector consisting of:

          * inputs - 1 dimensional numpy array
          * targets - 1 dimensional numpy array

        returns the value of the perceptron as evaluated on inputs
        """

    @abc.abstractmethod
    def evaluate(self, inputs):
        """
        Returns the evaluation of a perceptron on inputs (1-d numpy array)
        """
    @abc.abstractmethod
    def copy_weights(self):
        """
        Assures that the weight matrix is stored back in the state dictionary

        """
    @abc.abstractmethod
    def get_hidden(self, layer):
        """
        Returns the activations of the hidden layer
        """

    @abc.abstractmethod
    def get_deltas(self, layer):
        """
        Returns the deltas computed by the backprop algorithm
        in the previous call of train.
        """

    @abc.abstractmethod
    def copy_state(self):
        """
        Copies all the state values into the state that appear
        in the paramaters dictionary. If the implementation already uses
        these arrays, the method can just pass
        """


def view_as_ndarray(object):
    """
    This function takes a nested collection of lists/dictionaries with leafs that are numpy array like objects
    (e.g. shared numpy array) and casts them to a numpy array object which may accelerate the access time.

    :param object: nested collection of dictionaries/lists with leafs that are numpy array like objects.

    .. note::
        If the leafs of the array are shared memory objects, running this method may render them non serializable,
        in other words, they will serialize as regular numpy arrays.
    """
    if type(object) is list:
        retval = []
        for element in object:
            retval.append(view_as_ndarray(element))
        return retval
    if type(object) is dict:
        retval = {}
        for element in object.keys():
            retval[element] = view_as_ndarray(object[element])
        return retval
    if object is None:
        return None
    try:
        retval = object.view(np.ndarray)
        return retval
    except:
        raise Exception("Object neither a dict, nor a list and not an ndarray like")


def get_layers(dims=None):
    """
    Instantiate MLP layers with given dimensions
    :param dims: list of ints
    :return: layer dictinary
    """
    if not dims:
        dims = []
    layers = []
    for d in dims:
        layers.append({'activation': SharedArray.SharedNumpyArray(d, np.float),
                       'error': SharedArray.SharedNumpyArray(d, np.float),
                       'delta': SharedArray.SharedNumpyArray(d, np.float)
                       })
    return layers


def get_weights(layers=None):
    """
    Instantiate MLP weights
    :param layers: layer dictionary
    :return: list of weight matrices
    """
    if not layers:
        layers = []
    mlp_weights = []
    for l in range(len(layers)-1):
        l0 = layers[l]['activation'].shape[0]
        l1 = layers[l+1]['activation'].shape[0] - 1
        mlp_weights.append(initialize_weights(SharedArray.SharedNumpyArray((l0, l1), np.float)))
    return mlp_weights


def initialize_weights(w, method=0):
    """
    Initialize a weight matrix. Two methods are supported:

    method 0 is the classical initialization with uniform random variable around zero with variance inversely
             proportional to the first matrix dimension
    method 1 creates a random matrix with all singular values equal to 1.1.

    :param w: array to be populated with values
    :type w: numpy.ndarray
    :param method: method to be used
    :type method: int
    :return: referece to the populated array
    :rtype: numpy.ndarray
    """
    if method == 1:
        w[:] = optimal_weight_initialization(w.shape[0], w.shape[1])
    elif method == 0:
        pass
        w[:] = (np.random.rand(w.shape[0], w.shape[1])-0.5)*2/np.sqrt(w.shape[0]-1)
    else:
        raise("Unknown initialization method!")
    return w


class MLP(MLPPrototype):
    """
    Multi Layer Perceptron with arbitrary number of layers.

    This sample code implements the MLPPrototype abstract class using pure Python
    and numpy.

    A distinctive feature of this code is that a lot of the data structures containing the state
    of the algorithm (e.g., weight matrices) are supplied externally to the constructor. The constructor then
    uses references to these arrays are internal state objects. This allows this code to be used in a parallel
    system, where state variables can be instantiated as shared memory arrays, and inspected, saved or modified
    externally.


    :param state: dictionary containing predefined fields. The fields describe the network
    structure and provide allocated arrays (numpy arrays) to store the calculated weight values and
    other data.
    :type state: dict

    This design allows the object to have external side effects, particularly side effects in a different process
    if supplied data strucutres are shared.

    The dictionary should contain:

    state['layers'] - list of layers each layer being a dictionary with three keys:

        * activation - a numpy array like object for storing activations (including the bias activation always
            equal to 1, being the last element)
        * error - a numpy array like object of the same shape as activation for storing the error
        * delta - a numpy array like object of the same shape as activation for storing the delta factors


    state['beta'] - numpy array like object containing a scaling factor for the sigmoid
    state['momentum'] - numpy array like object containing a  momentum term for learning
    state['learning_rate'] - numpy array like object containing the learning rate
    state['mse'] - a single element numpy array that will be populated with the calculated mean squared error (MSE)
    state['weights'] - a list of weight matrices with compatible shapes.

    The shapes of weights should be:

        * weights[0] - weights from layer 0 to layer 1. The shape should be A_l0 x (A_l1 - 1) where A_lX stands for
            the shape of activation array in layer X
        * weights[1] - weights from layer 1 to layer 2. The shape should be A_l1 x (A_l2 - 1)
        * weights[2] - weights from layer 2 to layer 3. The shape should be A_l2 x (A_l3 - 1)
        * and so on

    :Example:
    ::

        X = np.array([[0, 1], [1, 1], [1, 0], [0, 0]])
        Y = np.array([[1, 0], [0, 0], [0, 1], [1, 1]])
        state = {}
        state['layers'] = [
            {'activation': np.zeros((3,)), 'error': np.zeros((3,)), 'delta': np.zeros((3,))},
            {'activation': np.zeros((5,)), 'error': np.zeros((5,)), 'delta': np.zeros((5,))},
            {'activation': np.zeros((3,)), 'error': np.zeros((3,)), 'delta': np.zeros((3,))},
        ]
        state['weights'] = [
            MLP2.initialize_weights(np.zeros((3, 4)), False),
            MLP2.initialize_weights(np.zeros((5, 2)), False)
        ]
        state['beta'] = np.array([1.0])
        state['learning_rate'] = np.array([0.1])
        state['momentum'] = np.array([0.5])
        state['mse'] = np.array([0.0])
        M = MLP2(state)
        for n in xrange(20000):
            i = np.random.randint(low=0, high=4)
            M.train(X[i], Y[i])
        for i in range(4):
            O = M.evaluate(X[i])
            assert(np.allclose(Y[i], O, atol=0.05))

    """

    def copy_weights(self):
        pass

    def get_hidden(self, layer):
        pass

    def __init__(self, state):
        # Set up network size
        self.layers = view_as_ndarray(state['layers'])
        for layer in self.layers:
            assert 'activation' in layer.keys()
            assert 'error' in layer.keys()
            assert 'delta' in layer.keys()

        self.n_layers = len(self.layers)
        self.beta = state['beta']
        self.momentum = state['momentum'].view(np.ndarray)
        self.learning_rate = state['learning_rate'].view(np.ndarray)
        # Initialize network
        self.weights = view_as_ndarray(state['weights'])
        self.delta_w = []
        self.__prev_update = []
        for i in range(len(self.weights)):
            assert(self.layers[i]['activation'].shape[0] == self.weights[i].shape[0])
            if i > 0:
                assert(self.layers[i]['activation'].shape[0] >= self.weights[i-1].shape[1]+1)
            self.delta_w.append(np.zeros_like(self.weights[i]))
            self.__prev_update.append(np.zeros_like(self.weights[i]))
        self.mse = state['mse'].view(np.ndarray)
        for layer in self.layers:
            if "no_bias" in state.keys():
                layer['activation'][-1] = 0
            else:
                layer['activation'][-1] = 1
        self.polynomial = False
        if "polynomial" in state:
            self.polynomial = True

    def train(self, inputs, targets, eta=None):
        """
        Train one sample of data

        :param inputs: one dimensional numpy array containing the input vector
        :type inputs: np.ndarray
        :param targets: one dimesinonal numpy array containing the desired output
        :type targets: np.ndarray
        :param eta : override the learning rate
        :type eta: float

        :return: actual activation of the output layer
        :rtype: np.ndarray
        """
        if eta is None:
            eta = self.learning_rate[0]
        self.mlp_forward(inputs)
        outputs = self.layers[-1]['activation'][:-1]
        self.layers[-1]['error'][:-1] = targets - outputs
        if not self.polynomial:
            fast_routines.derivative_dot_c(outputs, self.layers[-1]['error'], self.layers[-1]['delta'])
        else:
            fast_routines.derivative_dot_c_poly(outputs, self.layers[-1]['error'], self.layers[-1]['delta'])
        self.mlp_backpropagate()
        self.calculate_weight_update()
        self.update_weights()
        return outputs.copy()

    def evaluate(self, inputs):
        """
        Evaluate the perceptron on a sample without learning

        :param inputs: one dimensional numpy array of inputs
        :type inputs: numpy.ndarray

        :return: activation of the output layer
        :rtype: np.ndarray
        """
        self.mlp_forward(inputs)
        return self.layers[-1]['activation'][:-1].copy()

    def train2(self, error, eta=None):
        """
        Similar to train only here the user supplies the error which allows for a little bit more
        flexibility (e.g. part of the error be a direct function of the results)

        :param error: one dimensional numpy array of error
        :type error: numpy.ndarray
        :param eta: override the learning rate
        :type eta: float
        :return: numpy.ndarray
        :rtype: numpy.ndarray
        """
        if eta is None:
            eta = self.learning_rate[0]
        self.layers[-1]['error'][:] = 0
        self.layers[-1]['error'][:error.shape[0]] = error
        if not self.polynomial:
            fast_routines.derivative_dot_c(self.layers[-1]['activation'], self.layers[-1]['error'], self.layers[-1]['delta'])
        else:
            fast_routines.derivative_dot_c_poly(self.layers[-1]['activation'], self.layers[-1]['error'], self.layers[-1]['delta'])
        self.mlp_backpropagate()
        self.calculate_weight_update()
        self.update_weights()

    def mlp_forward(self, inputs=None):
        """
        Run the network forward. This method should not be used externally.

        :param inputs: one dimensional numpy array of inputs
        :type inputs: numpy.ndarray
        """
        if inputs is not None:
            self.layers[0]['activation'][:-1] = inputs
        for layer in xrange(self.n_layers-1):
            self.mlp_forward_layer(layer)

    def mlp_forward_layer(self, layer):
        """
        Propagate activations forward from layer to layer + 1

        :param layer: index of the layer to propagate from
        :type layer: int
        :return:
        """
        input = self.layers[layer]['activation']
        weights = self.weights[layer]
        beta = self.beta[0]
        result = self.layers[layer+1]['activation']
        if not self.polynomial:
            fast_routines.dot_sigmoid_c(input, weights, beta, result, 0)
        else:
            fast_routines.dot_sigmoid_c_poly(input, weights, beta, result, 0)

    def mlp_backpropagate_layer(self, layer):
        """
        Propagate error backwards from layer + 1 to layer

        :param layer: index of the layer to propagate the error to
        :type layer: int
        :return:
        """
        deltas = self.layers[layer]['delta']
        upper_deltas = self.layers[layer+1]['delta']
        activation = self.layers[layer]['activation']
        weights = self.weights[layer]
        error = self.layers[layer]['error']
        fast_routines.dot_transpose_c(upper_deltas[:weights.shape[1]], weights, error)
        if not self.polynomial:
            fast_routines.derivative_dot_c(activation, error, deltas)
        else:
            fast_routines.derivative_dot_c_poly(activation, error, deltas)

    def mlp_backpropagate(self):
        """
        Propagate the error backwards from the output to the input layer

        :return:
        """
        for layer in xrange(self.n_layers-2, -1, -1):
            self.mlp_backpropagate_layer(layer)

    def calculate_weight_update_layer(self, layer, eta=None):
        """
        Given the backpropagated error and delta factors, calculate the weight update

        :param layer:
        :return:
        """
        if eta is None:
            eta = self.learning_rate[0]
        inputs = self.layers[layer]['activation']
        deltas = self.layers[layer+1]['delta']
        fast_routines.generalized_outer_c(eta, inputs, deltas[:self.weights[layer].shape[1]], self.momentum[0], self.__prev_update[layer], self.delta_w[layer])

    def calculate_weight_update(self, eta=None):
        """
        Calculate weight updates for all the layers.

        :return
        """
        for layer in xrange(len(self.weights)):
            self.calculate_weight_update_layer(layer, eta=eta)

    def update_weights_layer(self, layer):
        """
        Perform the actual weight update for a layer of weights. Note layer here is the index of the
        input layer for the weight matrix that will be modified.

        :param layer: layer index
        :type layer: int
        :return:
        """
        self.weights[layer] += self.delta_w[layer]
        self.__prev_update[layer][:] = self.delta_w[layer][:]

    def update_weights(self):
        """
        Update weights in all layers.

        :return:
        """
        for layer in xrange(len(self.weights)):
            self.update_weights_layer(layer)

    def get_activation(self, layer=0):
        """
        Get the activation of the layers (updated when self.train or self.evaluate was called)

        :param layer: index of the layer (0 - input, -1 output)
        :type layer: int
        :return: activation of the hidden layer
        :rtype: numpy.ndarray
        """
        return self.layers[layer]['activation'][:-1]

    def get_deltas(self, layer=0):
        """
        Get the deltas calculated by backprop on hidden layers (updated when self.train was called)

        :param layer : index of the  layer
        :type layer: int
        :return: backpropagated error for every unit in the hidden layer
        :rtype: numpy.ndarray
        """
        return self.layers[layer]['delta']

    def copy_state(self):
        """
        An empty method, since all of the state is actually kept in externally supplied data
        structures.

        :return: No return value
        """
        pass


if __name__ == "__main__":
    # Timing with and without flushing denormals
    X = np.random.rand(100, 100)
    Y = np.random.rand(100, 100)
    state = {}
    state['layers'] = [
        {'activation': np.zeros((101,)), 'error': np.zeros((101,)), 'delta': np.zeros((101,))},
        {'activation': np.zeros((51,)), 'error': np.zeros((51,)), 'delta': np.zeros((51,))},
        {'activation': np.zeros((101,)), 'error': np.zeros((101,)), 'delta': np.zeros((101,))},
    ]
    state['weights'] = [
        MLP.initialize_weights(np.zeros((101, 50)), False),
        MLP.initialize_weights(np.zeros((51, 100)), False)
    ]
    state['beta'] = np.array([1.0])
    state['learning_rate'] = np.array([0.1])
    state['momentum'] = np.array([1.0])
    state['mse'] = np.array([0.0])
    M = MLP(state)
    t_start = time.clock()
    try:
        import LowLevelCPUControlx86
        LowLevelCPUControlx86.set_flush_denormals()
    except:
        # Unable to set the flags
        print "Setting flush denormals CPU flag not available"
    for n in xrange(100000):
        i = n % 100
        M.train(X[i], Y[i])
    t_exec = time.clock()-t_start
    print "Execution time %f" % t_exec


================================================
FILE: PVM_framework/PVM_Create.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================

import multiprocessing as mp
import numpy as np
import PVM_framework.SharedArray as SharedArray
import logging
import importlib
import random
import datetime
import glob

PVM_FLAG_CTRL_DREAM = 0
PVM_FLAG_CTRL_DREAM_EXP = 1
PVM_FLAG_CTRL_LEARNING = 2

PVM_FLAG_VAL_TRIGGER = 1
PVM_FLAG_VAL_CANCEL = 2
PVM_FLAG_VAL_RESET = 0
PVM_FLAG_TRIGGER_DISPLAY = 15
PVM_FLAG_TRIGGER_RECORD = 16
PVM_MAX_LAYERS = 20  # Arbitrarily set to 20, could be anything really
PVM_LOG_ERROR_EVERY = 1000  # Interval for error logging

PVM_FLAG_VAL_DREAM = 1
PVM_FLAG_VAL_BLINDSPOT = 2
PVM_FLAG_VAL_GRAY = 3
PVM_FLAG_VAL_NOISE = 4
PVM_FLAG_VAL_NOISE_SPOT = 5
PVM_FLAG_VAL_INV_BLINDSPOT = 6
PVM_FLAG_VAL_INV_NOISE_SPOT = 10
PVM_FLAG_VAL_DREAM_SPOT = 11
PVM_FLAG_VAL_INV_DREAM_SPOT = 7
PVM_FLAG_VAL_DEEP_DREAM = 12
PVM_FLAG_VAL_BLINKS = 8
PVM_FLAG_VAL_NOISY_SIGNAL = 9

PVM_LEARNING_FLAG = 2
PVM_LEARNING_RESET = 0
PVM_LEARNING_FREEZE = 1
PVM_LEARNING_UNFREEZE = 2

PVM_PAUSE = 1
PVM_RESUME = 0
PVM_DUMP = 2


def create_blank_dictionary(name="default", description="default", save_sources=False):
    """
    Creates a minimal model dictionary.

    :return: model dict
    :rtype: dict
    """
    import PVM_framework.SharedArray as SharedArray
    import numpy as np
    simulation_dict = {}
    simulation_dict['N'] = SharedArray.SharedNumpyArray((1,), np.int64)
    simulation_dict['N'][0] = 0
    simulation_dict['record_filename'] = SharedArray.SharedNumpyArray((256,), np.uint8)
    simulation_dict['paused'] = SharedArray.SharedNumpyArray((1,), np.int)
    simulation_dict['paused'][0] = 0
    simulation_dict['finished'] = SharedArray.SharedNumpyArray((1,), np.int)
    simulation_dict['finished'][0] = 0
    simulation_dict['flags'] = SharedArray.SharedNumpyArray((16,), np.uint8)
    for i in range(16):
        simulation_dict['flags'][i] = 0
    simulation_dict['num_proc'] = mp.cpu_count()/2
    simulation_dict['debug_infrastructure'] = {}
    simulation_dict['debug_infrastructure']['enabled_hookups'] = {}
    simulation_dict['debug_infrastructure']['disabled_hookups'] = {}
    logging.info("Created a blank dictionary")
    simulation_dict['description'] = description
    simulation_dict['name'] = name
    simulation_dict['sources'] = {}
    if save_sources:
        for filename in glob.glob("*.py"):
            f = open(filename, "r")
            simulation_dict['sources'][filename] = f.read()
            f.close()
            logging.info("Saved source file %s into the dictionary" % filename)
        for filename in glob.glob("*.pyx"):
            f = open(filename, "r")
            simulation_dict['sources'][filename] = f.read()
            f.close()
            logging.info("Saved source file %s into the dictionary" % filename)
    simulation_dict['hash'] = "%08x" % random.getrandbits(32)
    simulation_dict['timestamp'] = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
    logging.info("Assigned hash %s to this simulation instance" % simulation_dict['hash'])
    logging.info("Assigned timestamp %s to this simulation instance" % simulation_dict['timestamp'])
    return simulation_dict


def get_surround(xy, dim_x=10, dim_y=10, radius=1, exclude_self=True):
    """
    Returns the indices of elements on the grid that are within square radius
    of the given xy

      radius = 1:

        0 1 0
        1 1 1
        0 1 0

      radius = 1.5:

        1 1 1
        1 1 1
        1 1 1

      radius = 2

        0 0 1 0 0
        0 1 1 1 0
        1 1 1 1 1
        0 1 1 1 0
        0 0 1 0 0

    Setting exclude_self to True removes the center unit
    """
    laterals = []
    for dx in range(-int(radius), int(radius)+1, 1):
        for dy in range(-int(radius), int(radius)+1, 1):
            if dx**2 + dy**2 > radius**2:
                continue
            if (xy[0]+dx >= 0) and (xy[0]+dx < dim_x) and (xy[1]+dy >= 0) and (xy[1]+dy < dim_y):
                if not (exclude_self and dx == 0 and dy == 0):
                    laterals.append((xy[0]+dx, xy[1]+dy))
    return laterals


def get_fan_in(xy=(0, 0), dim_x_l=10, dim_y_l=10, dim_x_u=9, dim_y_u=9, block_x=2, block_y=2, radius=2):
    """
    Selects a block_x x block_y subsquare in the underlying layers lying directly below the unit in the
    upper layer. Selects units within radius in that block.

      e.g. block_x=2, block_y=2 radius=2

        1 1
        1 1

      e.g. block_x=3, block_y=3 radius=2

        1 1 1
        1 1 1
        1 1 1

      e.g. block_x=3, block_y=3 radius=1

        0 1 0
        1 1 1
        0 1 0

    """
    x = xy[0]
    y = xy[1]
    if dim_x_u > 1:
        factor_x = ((dim_x_l-1)-(block_x-1))/(1.0*(dim_x_u-1))
    else:
        factor_x = ((dim_x_l-1)-(block_x))/2.0
    if dim_y_u > 1:
        factor_y = ((dim_y_l-1)-(block_y-1))/(1.0*(dim_y_u-1))
    else:
        factor_y = ((dim_y_l-1)-(block_y))/2.0
    results = []
    if dim_x_u > 1 and dim_y_u > 1:
        for xx in range(block_x):
            for yy in range(block_y):
                if (xx-(block_x-1)*0.5)**2 + (yy-(block_y-1)*0.5)**2 > radius**2:
                    continue
                results.append((int((factor_x*(x))+xx), int((factor_y*(y))+yy)))
        return results
    elif dim_x_u == 1 and dim_y_u > 1:
        for xx in range(block_x):
            for yy in range(block_y):
                if (xx-(block_x-1)*0.5)**2 + (yy-(block_y-1)*0.5)**2 > radius**2:
                    continue
                results.append((int((dim_x_l-block_x)/2.0+xx), int((factor_y*(y)+yy))))
        return results
    elif dim_x_u > 1 and dim_y_u == 1:
        for xx in range(block_x):
            for yy in range(block_y):
                if (xx-(block_x-1)*0.5)**2 + (yy-(block_y-1)*0.5)**2 > radius**2:
                    continue
                results.append((int((factor_x*(x)+xx)), int((dim_y_l-block_y)/2.0+yy)))
        return results
    elif dim_x_u == 1 and dim_y_u == 1:
        for xx in range(block_x):
            for yy in range(block_y):
                if (xx-(block_x-1)*0.5)**2 + (yy-(block_y-1)*0.5)**2 > radius**2:
                    continue
                results.append((int((dim_x_l-block_x)/2.0+xx), int((dim_y_l-block_y)/2.0+yy)))
        return results


def connect_forward_and_back(simulation_dict, (index0, blocks_per_dim0, predicted_array), (index1, blocks_per_dim1), square_size, radius, context_factor):
    """
    Connect two layers with a given fan-in as defined by the square_size and radius.
    The forward connections are accompanied by a feedback context connections back to
    the originating source unit.

    :param simulation_dict:
    :param square_size:
    :param radius:
    :param context_factor:
    :return:
    """
    hidden_size = simulation_dict['hidden_size']
    dx = hidden_size
    dy = hidden_size
    logging.info("Connecting from index %d to index %d" % (index0, index1))
    logging.info("Input layer size is %d, receiving layer size is %d" % (blocks_per_dim0, blocks_per_dim1))
    logging.info("Radius of connectivity %d" % radius)
    for x in range(blocks_per_dim1):
        for y in range(blocks_per_dim1):
            surround = get_fan_in((x, y),
                                  dim_x_l=blocks_per_dim0,
                                  dim_y_l=blocks_per_dim0,
                                  dim_x_u=blocks_per_dim1,
                                  dim_y_u=blocks_per_dim1,
                                  block_x=square_size,
                                  block_y=square_size,
                                  radius=radius)
            dest = index1 + x * (blocks_per_dim1) + y  # destination unit
            for xy in surround:
                source = index0 + xy[0] * blocks_per_dim0 + xy[1]  # source unit
                # Prepare the input and corresponding delta block at source
                input_block = simulation_dict['stage0'][source]['output_block']
                delta_block = SharedArray.SharedNumpyArray_like(input_block)
                simulation_dict['stage0'][source]['delta_blocks'].append(delta_block)
                # Prepare the context and corresonding delta block at destination
                context_block = simulation_dict['stage0'][dest]['output_block']
                delta_block2 = SharedArray.SharedNumpyArray_like(context_block)
                simulation_dict['stage0'][dest]['delta_blocks'].append(delta_block2)
                # Connect the context block to the source
                simulation_dict['stage0'][source]['context_blocks'].append((context_block, delta_block2, context_factor))
                # Prepare the predicted blocks
                xx = xy[0]*hidden_size
                yy = xy[1]*hidden_size
                assert(predicted_array[xx:xx+dx, yy:yy+dy].shape == context_block.shape)
                predicted_block = SharedArray.DynamicView(predicted_array)[xx:xx+dx, yy:yy+dy]
                if not (predicted_block.shape == (dx, dy)):
                    print predicted_block.shape
                    raise
                # Connect the input to the destination together with its predicted blocks and so on.
                past_block = SharedArray.SharedNumpyArray_like(input_block)
                derivative_block = SharedArray.SharedNumpyArray_like(input_block)
                integral_block = SharedArray.SharedNumpyArray_like(input_block)
                pred_block_local = SharedArray.SharedNumpyArray_like(input_block)
                simulation_dict['stage0'][dest]['signal_blocks'].append((input_block, delta_block, predicted_block, past_block, derivative_block, integral_block, pred_block_local))


def connect_forward_and_back_v1(simulation_dict, (index0, blocks_per_dim0, predicted_array, predicted_array_t2), (index1, blocks_per_dim1), square_size, radius, context_factor):
    """
    Connect two layers with a given fan in as defined by the square_size and radius.
    The forward connections are accompanied by feedback context connections back to
    the originating source unit.

    :param simulation_dict:
    :param square_size:
    :param radius:
    :param context_factor:
    :return:
    """
    hidden_size = simulation_dict['hidden_size']
    dx = hidden_size
    dy = hidden_size
    logging.info("Connecting from index %d to index %d" % (index0, index1))
    logging.info("Input layer size is %d, receiving layer size is %d" % (blocks_per_dim0, blocks_per_dim1))
    logging.info("Radius of connectivity %d" % radius)
    for x in range(blocks_per_dim1):
        for y in range(blocks_per_dim1):
            surround = get_fan_in((x, y),
                                  dim_x_l=blocks_per_dim0,
                                  dim_y_l=blocks_per_dim0,
                                  dim_x_u=blocks_per_dim1,
                                  dim_y_u=blocks_per_dim1,
                                  block_x=square_size,
                                  block_y=square_size,
                                  radius=radius)
            dest = index1 + x * (blocks_per_dim1) + y  # destination unit
            for xy in surround:
                source = index0 + xy[0] * blocks_per_dim0 + xy[1]  # source unit
                # Prepare the input and corresponding delta block at source
                input_block = simulation_dict['stage0'][source]['output_block']
                delta_block = SharedArray.SharedNumpyArray_like(input_block)
                simulation_dict['stage0'][source]['delta_blocks'].append(delta_block)
                # Prepare the context and corresonding delta block at destination
                context_block = simulation_dict['stage0'][dest]['output_block']
                delta_block2 = SharedArray.SharedNumpyArray_like(context_block)
                simulation_dict['stage0'][dest]['delta_blocks'].append(delta_block2)
                # Connect the context block to the source
                simulation_dict['stage0'][source]['context_blocks'].append((context_block, delta_block2, context_factor))
                # Prepare the predicted blocks
                xx = xy[0]*hidden_size
                yy = xy[1]*hidden_size
                assert(predicted_array[xx:xx+dx, yy:yy+dy].shape == context_block.shape)
                predicted_block = SharedArray.DynamicView(predicted_array)[xx:xx+dx, yy:yy+dy]
                predicted_block2 = SharedArray.DynamicView(predicted_array_t2)[xx:xx+dx, yy:yy+dy]
                if not (predicted_block.shape == (dx, dy)):
                    print predicted_block.shape
                    raise
                # Connect the input to the destination together with its predicted blocks and so on.
                simulation_dict['stage0'][dest]['signal_blocks'].append((input_block, delta_block, predicted_block, predicted_block2))


def connect_back(simulation_dict, (index_from, blocks_per_dim_from), (index_to, blocks_per_dim_to), square_size, radius, context_factor):
    """
    Connect feedback only. This function is used to connect two layers that are not directly connected (in which
    case the feedback connection would have been established along with the feedforward connection), e.g. in cases
    when feedback is sent from some layer way above to some lower layer.

    :param simulation_dict:
    :param square_size:
    :param radius:
    :param context_factor:
    :return:
    """
    logging.info("Connecting back additional context from index %d to index %d" % (index_from, index_to))
    logging.info("Connecting back additional context from layer size is %d, receiving layer size is %d" % (blocks_per_dim_from, blocks_per_dim_to))
    logging.info("Radius of connectivity %d" % radius)
    for x in range(blocks_per_dim_from):
        for y in range(blocks_per_dim_from):
            surround = get_fan_in((x, y),
                                  dim_x_l=blocks_per_dim_to,
                                  dim_y_l=blocks_per_dim_to,
                                  dim_x_u=blocks_per_dim_from,
                                  dim_y_u=blocks_per_dim_from,
                                  block_x=square_size,
                                  block_y=square_size,
                                  radius=radius)
            source = index_from + x * (blocks_per_dim_from) + y  # unit in the higher layer
            for xy in surround:
                dest = index_to + xy[0] * blocks_per_dim_to + xy[1]  # unit in the lower layer
                context_block = simulation_dict['stage0'][source]['output_block']
                delta_block2 = SharedArray.SharedNumpyArray_like(context_block)
                simulation_dict['stage0'][source]['delta_blocks'].append(delta_block2)
                # Connect the context block to the source
                simulation_dict['stage0'][dest]['context_blocks'].append((context_block, delta_block2, context_factor))


def gather_surround(simulation_dict, (index0, blocks_per_dim0), radius, context_factor, exclude_self=True):
    for x in range(blocks_per_dim0):
        for y in range(blocks_per_dim0):
            surround = get_surround((x, y), dim_x=blocks_per_dim0, dim_y=blocks_per_dim0, radius=radius, exclude_self=exclude_self)
            dest = index0 + x * blocks_per_dim0 + y  # destination unit
            for xy in surround:
                source = xy[0] * blocks_per_dim0 + xy[1]  # source unit
                context_block = simulation_dict['stage0'][source]['output_block']
                delta_block = SharedArray.SharedNumpyArray_like(context_block)
                simulation_dict['stage0'][source]['delta_blocks'].append(delta_block)
                simulation_dict['stage0'][dest]['context_blocks'].append((context_block, delta_block, context_factor))


def create_basic_unit_v1(learning_rate, momentum, tau, readout_learning_rate):
    unit_parameters = dict()
    unit_parameters['tau'] = tau
    unit_parameters['primary_learning_rate'] = learning_rate
    unit_parameters['readout_learning_rate'] = readout_learning_rate
    unit_parameters['momentum'] = momentum
    unit_parameters['signal_blocks'] = []
    unit_parameters['readout_blocks'] = []
    unit_parameters['predicted_blocks'] = []
    unit_parameters['delta_blocks'] = []
    unit_parameters['context_blocks'] = []
    return unit_parameters


def generate_dict_options(name, description, options):
    if options['version_major'] == "1" and options["version_minor"] == "0":
        dic_ = generate_v1(name=name,
                           description=description,
                           options=options
                           )
        return dic_


def generate_v1(name, description, options):
    input_block_size = int(options["input_block_size"])
    hidden_size = int(options["hidden_block_size"])
    layer_shape = map(lambda x: int(x), options["layer_shapes"])
    readout_block_size = map(lambda x: int(x), options["readout_block_size"])
    readout_layer = map(lambda x: x == "1", options["enable_readout"])
    lateral_radius = float(options["lateral_radius"])
    fan_in_square_size = int(options["fan_in_square_size"])
    fan_in_radius = int(options["fan_in_radius"])
    readout_depth = int(options["readout_depth"])
    ex_module = options["ex_module"]
    exclude_self = (options["context_exclude_self"] == '1')
    last_layer_context_to_all = (options["last_layer_context_to_all"] == '1')
    send_context_two_layers_back = (options["send_context_two_layers_back"] == '1')
    simulation_dict = create_blank_dictionary(name=name, description=description, save_sources=(options["save_source_files"] == '1'))
    simulation_dict['stages'] = 1
    simulation_dict['num_proc'] = 2*mp.cpu_count()/3
    simulation_dict['stage0'] = []
    simulation_dict['execution_unit_module'] = ex_module
    simulation_dict['version_major'] = 1
    simulation_dict['version_minor'] = 0
    unit = importlib.import_module(simulation_dict['execution_unit_module'])
    blocks_per_dim = layer_shape
    layers = len(blocks_per_dim)
    error_log = SharedArray.SharedNumpyArray((layers+1, 1000000), np.float)
    simulation_dict['error_log'] = error_log

    simulation_dict['input_block_size'] = input_block_size
    simulation_dict['hidden_size'] = hidden_size
    simulation_dict['learning_rates'] = []
    simulation_dict['momenta'] = []
    simulation_dict['taus'] = []
    simulation_dict['predicted_arrays'] = []
    simulation_dict['predicted_arrays_t2'] = []
    simulation_dict['predicted_readout_arrays'] = []
    simulation_dict['readout_arrays'] = []
    simulation_dict['state_arrays'] = []
    simulation_dict['delta_arrays'] = []

    input_array = SharedArray.SharedNumpyArray((input_block_size*blocks_per_dim[0], input_block_size*blocks_per_dim[0], 3), np.uint8)
    simulation_dict['input_array'] = input_array
    input_array_float = SharedArray.SharedNumpyArray((input_block_size*blocks_per_dim[0], input_block_size * blocks_per_dim[0], 3), np.float)
    simulation_dict['input_array_float'] = input_array_float

    for (i, bpd) in enumerate(blocks_per_dim):
        if readout_layer[i]:
            readout_array_float00 = SharedArray.SharedNumpyArray((bpd*readout_block_size[i], bpd*readout_block_size[i], readout_depth), np.float)
            simulation_dict['readout_arrays'].append(readout_array_float00)
            predicted_readout_array_float00 = SharedArray.SharedNumpyArray((bpd*readout_block_size[i], bpd*readout_block_size[i], readout_depth), np.float)
            simulation_dict['predicted_readout_arrays'].append(predicted_readout_array_float00)

    # input array 0 is a special case, 3 dimensions because of color input
    predicted_array0 = SharedArray.SharedNumpyArray((input_block_size*blocks_per_dim[0], input_block_size*blocks_per_dim[0], 3), np.float)
    simulation_dict['predicted_arrays'].append(predicted_array0)
    predicted_array2 = SharedArray.SharedNumpyArray((input_block_size*blocks_per_dim[0], input_block_size*blocks_per_dim[0], 3), np.float)
    simulation_dict['predicted_arrays_t2'].append(predicted_array2)
    delta_array0 = SharedArray.SharedNumpyArray((input_block_size*blocks_per_dim[0], input_block_size*blocks_per_dim[0], 3), np.float)
    simulation_dict['delta_arrays'].append(delta_array0)
    # All the rest is generic
    for (i, bpd) in enumerate(blocks_per_dim[:-1]):
        predicted_array1 = SharedArray.SharedNumpyArray((hidden_size*bpd, hidden_size*bpd), np.float)
        simulation_dict['predicted_arrays'].append(predicted_array1)
        predicted_array2 = SharedArray.SharedNumpyArray((hidden_size*bpd, hidden_size*bpd), np.float)
        simulation_dict['predicted_arrays_t2'].append(predicted_array2)
        delta_array1 = SharedArray.SharedNumpyArray((hidden_size*bpd, hidden_size*bpd), np.float)
        simulation_dict['delta_arrays'].append(delta_array1)
    for (i, bpd) in enumerate(blocks_per_dim):
        state_array0 = SharedArray.SharedNumpyArray((hidden_size*bpd, hidden_size*bpd), np.float)
        simulation_dict['state_arrays'].append(state_array0)

    # Base learning rate
    for (i, bpd) in enumerate(blocks_per_dim):
        learning_rate = SharedArray.SharedNumpyArray((1, ), np.float)
        learning_rate[0] = 0.0
        simulation_dict['learning_rates'].append(learning_rate)
    additional_learning_rate = SharedArray.SharedNumpyArray((1, ), np.float)
    additional_learning_rate[0] = 0.0
    simulation_dict['readout_learning_rate'] = additional_learning_rate
    # Momentum is the same everywhere.
    momentum = SharedArray.SharedNumpyArray((1, ), np.float)
    momentum[0] = float(options["momentum"])
    simulation_dict['momentum'] = momentum
    # Tau is the integration constant for the signal integral
    tau = SharedArray.SharedNumpyArray((1, ), np.float)
    tau[0] = float(options['tau'])
    simulation_dict['tau'] = tau
    context_factor_lateral = SharedArray.SharedNumpyArray((1, ), np.float)
    context_factor_lateral[0] = 0.0
    simulation_dict['context_factor_lateral'] = context_factor_lateral
    context_factor_feedback = SharedArray.SharedNumpyArray((1, ), np.float)
    context_factor_feedback[0] = 0.0
    simulation_dict['context_factor_feedback'] = context_factor_feedback
    base_index = [0]
    for bpd in blocks_per_dim:
        base_index.append(base_index[-1] + bpd*bpd)

    # Layer 0 is specific and has to be constructed separately
    for i in xrange(blocks_per_dim[0] * blocks_per_dim[0]):
        unit_parameters = create_basic_unit_v1(simulation_dict['learning_rates'][0], momentum, tau, additional_learning_rate)
        x = (i / blocks_per_dim[0])*input_block_size
        y = (i % blocks_per_dim[0])*input_block_size
        dx = input_block_size
        dy = input_block_size
        input_block = SharedArray.DynamicView(input_array_float)[x:x+dx, y:y+dy]
        predicted_block = SharedArray.DynamicView(simulation_dict['predicted_arrays'][0])[x:x+dx, y:y+dy]
        predicted_block_2 = SharedArray.DynamicView(simulation_dict['predicted_arrays_t2'][0])[x:x+dx, y:y+dy]
        delta_block = SharedArray.DynamicView(simulation_dict['delta_arrays'][0])[x:x+dx, y:y+dy]
        if not (predicted_block.shape == (dx, dy, 3)):
            print predicted_block.shape
            raise Exception("Block sizes don't agree")
        k = (i / blocks_per_dim[0])*hidden_size
        l = (i % blocks_per_dim[0])*hidden_size
        output_block = SharedArray.DynamicView(simulation_dict['state_arrays'][0])[k:k+hidden_size, l:l+hidden_size]
        unit_parameters['signal_blocks'].append((input_block,
                                                 delta_block,
                                                 predicted_block,
                                                 predicted_block_2
                                                 ))
        unit_parameters['output_block'] = output_block
        if readout_layer[0]:
            # Motor heatmap prediction
            layer = 0
            bpd = blocks_per_dim[layer]
            readout_teaching_block = SharedArray.DynamicView(simulation_dict['readout_arrays'][layer])[(i / bpd)*readout_block_size[0]:(i / bpd+1)*readout_block_size[0], (i % bpd)*readout_block_size[0]:(i % bpd+1)*readout_block_size[0]]
            readout_delta_block = SharedArray.SharedNumpyArray_like(readout_teaching_block)
            predicted_readout_block = SharedArray.DynamicView(simulation_dict['predicted_readout_arrays'][layer])[(i / bpd)*readout_block_size[0]:(i / bpd+1)*readout_block_size[0], (i % bpd)*readout_block_size[0]:(i % bpd+1)*readout_block_size[0]]
            unit_parameters['readout_blocks'] = [(readout_teaching_block, readout_delta_block, predicted_readout_block)]
            unit_parameters["layer"] = 0
            # End motor heatmap prediction
        simulation_dict['stage0'].append(unit_parameters)
    # Layer 0 surround
    gather_surround(simulation_dict, (base_index[0], blocks_per_dim[0]), radius=lateral_radius, context_factor=context_factor_lateral, exclude_self=exclude_self)

    # The following layers are more generic
    for layer in range(1, layers):
        for i in xrange(blocks_per_dim[layer] * blocks_per_dim[layer]):
            unit_parameters = create_basic_unit_v1(simulation_dict['learning_rates'][layer], momentum, tau, additional_learning_rate)
            k = (i / blocks_per_dim[layer])*hidden_size
            l = (i % blocks_per_dim[layer])*hidden_size
            output_block = SharedArray.DynamicView(simulation_dict['state_arrays'][layer])[k:k+hidden_size, l:l+hidden_size]
            unit_parameters['output_block'] = output_block
            if readout_layer[layer]:
                # Motor heatmap prediction
                bpd = blocks_per_dim[layer]
                readout_teaching_block = SharedArray.DynamicView(simulation_dict['readout_arrays'][layer])[(i / bpd)*readout_block_size[layer]:(i / bpd+1)*readout_block_size[layer], (i % bpd)*readout_block_size[layer]:(i % bpd+1)*readout_block_size[layer]]
                readout_delta_block = SharedArray.SharedNumpyArray_like(readout_teaching_block)
                predicted_readout_block = SharedArray.DynamicView(simulation_dict['predicted_readout_arrays'][layer])[(i / bpd)*readout_block_size[layer]:(i / bpd+1)*readout_block_size[layer], (i % bpd)*readout_block_size[layer]:(i % bpd+1)*readout_block_size[layer]]
                unit_parameters['readout_blocks'] = [(readout_teaching_block, readout_delta_block, predicted_readout_block)]
                unit_parameters["layer"] = layer
                # End motor heatmap prediction
            simulation_dict['stage0'].append(unit_parameters)
        # Connect to the previous layer
        connect_forward_and_back_v1(simulation_dict, (base_index[layer-1], blocks_per_dim[layer-1], simulation_dict['predicted_arrays'][layer], simulation_dict['predicted_arrays_t2'][layer]), (base_index[layer], blocks_per_dim[layer]), square_size=fan_in_square_size, radius=fan_in_radius, context_factor=context_factor_feedback)
        # Layer surround
        gather_surround(simulation_dict, (base_index[layer], blocks_per_dim[layer]), radius=lateral_radius, context_factor=context_factor_lateral, exclude_self=exclude_self)
        if send_context_two_layers_back and layer > 1:
            connect_back(simulation_dict, (base_index[layer], blocks_per_dim[layer]), (base_index[layer-2], blocks_per_dim[layer-2]), square_size=2*fan_in_square_size, radius=2*fan_in_radius, context_factor=context_factor_feedback)

    # Add the global feedback from the top layer
    if last_layer_context_to_all:
        logging.info("Connecting last layer back to everyone")
        for to_idx in xrange(base_index[layers-1]):
            for from_idx in range(base_index[layers-1], len(simulation_dict["stage0"])):
                context_block = simulation_dict['stage0'][from_idx]['output_block']
                delta_block2 = SharedArray.SharedNumpyArray_like(context_block)
                simulation_dict['stage0'][from_idx]['delta_blocks'].append(delta_block2)
                # Connect the context block to the source
                simulation_dict['stage0'][to_idx]['context_blocks'].append((context_block, delta_block2, context_factor_feedback))

    simulation_dict['stage0_size'] = len(simulation_dict['stage0'])
    for i in range(simulation_dict['stage0_size']):
            simulation_dict['stage0'][i]['flags'] = simulation_dict['flags']
            unit.ExecutionUnit.generate_missing_parameters(simulation_dict['stage0'][i], options=options)
    return simulation_dict


def upgrade_readout(simulation_dict):
    """
    Upgrade the dictionary with a parameters for a three layer perceptron for readout
    :param simulation_dict:
    :return:
    """
    logging.info("Upgrading readout network to a full perceptron")
    import PVM_framework.SharedArray as SharedArray
    import PVM_framework.MLP as MLP
    simulation_dict['stage0_size'] = len(simulation_dict['stage0'])
    needed = True
    for i in range(simulation_dict['stage0_size']):
        if len(simulation_dict['stage0'][i]["MLP_parameters_additional"]['layers']) == 2 and len(simulation_dict['stage0'][i]["MLP_parameters_additional"]['weights']) == 1:
            nhidden = simulation_dict['stage0'][i]["MLP_parameters_additional"]['layers'][0]['activation'].shape[0]-1
            nadditional = simulation_dict['stage0'][i]["MLP_parameters_additional"]['layers'][-1]['activation'].shape[0]-1
            layer = {'activation': SharedArray.SharedNumpyArray((nhidden+1), np.float),
                     'error': SharedArray.SharedNumpyArray((nhidden+1), np.float),
                     'delta': SharedArray.SharedNumpyArray((nhidden+1), np.float)
                     }

            simulation_dict['stage0'][i]["MLP_parameters_additional"]['layers'].insert(1, layer)
            simulation_dict['stage0'][i]["MLP_parameters_additional"]['weights'] = \
                [MLP.initialize_weights(SharedArray.SharedNumpyArray((nhidden+1, nhidden), np.float)),
                 MLP.initialize_weights(SharedArray.SharedNumpyArray((nhidden+1, nadditional), np.float)),
                 ]
        else:
            needed = False
    if needed:
        logging.info("Upgrade complete")
    else:
        logging.info("Upgrade was not nescessary")


def upgrade(simulation_dict):
    old_log = simulation_dict['error_log']
    if old_log.shape[1] < 1000000:
        new_log = SharedArray.SharedNumpyArray((old_log.shape[0], 1000000), np.float)
        new_log[:, 0:old_log.shape[1]] = old_log
        simulation_dict['error_log'] = new_log
    if "flags" not in simulation_dict.keys():
        simulation_dict['flags'] = SharedArray.SharedNumpyArray((16,), np.uint8)
        for i in range(16):
            simulation_dict['flags'][i] = 0
    if "record_filename" not in simulation_dict.keys():
        simulation_dict['record_filename'] = SharedArray.SharedNumpyArray((256,), np.uint8)


def apply_options(simulation_dict, options):
    if "bias_free" in options and options["bias_free"] == "1":
        for i in range(simulation_dict['stage0_size']):
            simulation_dict['stage0'][i]["Primary_Predictor_params"]['no_bias'] = True
            if "Residual_Predictor_params" in simulation_dict['stage0'][i].keys():
                simulation_dict['stage0'][i]["Residual_Predictor_params"]['no_bias'] = True
    if "bias_free_additional" in options and options["bias_free_additional"] == "1":
        for i in range(simulation_dict['stage0_size']):
            simulation_dict['stage0'][i]["Readout_Predictor_params"]['no_bias'] = True
    simulation_dict['options'] = options


def upgrade_dictionary_to_ver1_0(simulation_dict):
    upgrade(simulation_dict)
    if "version_major" not in simulation_dict.keys() or simulation_dict["version_major"] < 1:
        logging.info("Simulation dictionary of the old type, automatically upgrading to ver 1.0")
        if 'learning_rates' not in simulation_dict.keys():
            simulation_dict['learning_rates'] = []
        if 'momenta' not in simulation_dict.keys():
            simulation_dict['momenta'] = []
        if 'taus' not in simulation_dict.keys():
            simulation_dict['taus'] = []
        if 'predicted_arrays' not in simulation_dict.keys():
            simulation_dict['predicted_arrays'] = []
        if 'predicted_arrays_t2' not in simulation_dict.keys():
            simulation_dict['predicted_arrays_t2'] = []
        if 'predicted_readout_arrays' not in simulation_dict.keys():
            simulation_dict['predicted_readout_arrays'] = []
        if 'readout_arrays' not in simulation_dict.keys():
            simulation_dict['readout_arrays'] = []
        if 'predicted_arrays' not in simulation_dict.keys():
            simulation_dict['predicted_arrays'] = []
        if 'state_arrays' not in simulation_dict.keys():
            simulation_dict['state_arrays'] = []
        if 'delta_arrays' not in simulation_dict.keys():
            simulation_dict['delta_arrays'] = []
        for i in range(PVM_MAX_LAYERS):  # max number of layers
            if "delta_array%02d" % i in simulation_dict.keys():
                simulation_dict['delta_arrays'].append(simulation_dict['delta_array%02d' % i])
                del simulation_dict['delta_array%02d' % i]
            if "learning_rate%02d" % i in simulation_dict.keys():
                simulation_dict['learning_rates'].append(simulation_dict['learning_rate%02d' % i])
                del simulation_dict['learning_rate%02d' % i]
            if "state_array%02d" % i in simulation_dict.keys():
                simulation_dict['state_arrays'].append(simulation_dict['state_array%02d' % i])
                del simulation_dict['state_array%02d' % i]
            if "predicted_readout_array_float%02d" % i in simulation_dict.keys():
                simulation_dict['predicted_readout_arrays'].append(simulation_dict['predicted_readout_array_float%02d' % i])
                del simulation_dict['predicted_readout_array_float%02d' % i]
            if "readout_array_float%02d" % i in simulation_dict.keys():
                simulation_dict['readout_arrays'].append(simulation_dict['readout_array_float%02d' % i])
                del simulation_dict['readout_array_float%02d' % i]
            if "predicted_array%02d" % i in simulation_dict.keys():
                simulation_dict['predicted_arrays'].append(simulation_dict['predicted_array%02d' % i])
                simulation_dict['predicted_arrays_t2'].append(SharedArray.SharedNumpyArray_like(simulation_dict['predicted_array%02d' % i]))
                del simulation_dict['predicted_array%02d' % i]

        if "motor_delta_array_float" in simulation_dict.keys():
            del simulation_dict["motor_delta_array_float"]
        if "readout_array_float" in simulation_dict.keys():
            del simulation_dict["readout_array_float"]
        if "readout_array_float" in simulation_dict.keys():
            del simulation_dict["readout_array_float"]
        if "predicted_motor_derivative_array_float" in simulation_dict.keys():
            del simulation_dict["predicted_motor_derivative_array_float"]
        if "predicted_readout_array_float" in simulation_dict.keys():
            del simulation_dict["predicted_readout_array_float"]
        if "additional_learning_rate" in simulation_dict.keys():
            simulation_dict["readout_learning_rate"] = simulation_dict["additional_learning_rate"]
            del simulation_dict["additional_learning_rate"]
        elif "readout_learning_rate" not in simulation_dict.keys():
            simulation_dict["readout_learning_rate"] = SharedArray.SharedNumpyArray((1, ), np.float)
            simulation_dict["readout_learning_rate"][:] = 0.00001

        simulation_dict['execution_unit_module'] += "_v1"
        if not simulation_dict['execution_unit_module'].startswith("PVM_models"):
            simulation_dict['execution_unit_module'] = "PVM_models."+simulation_dict['execution_unit_module']
        ex_unit = importlib.import_module(simulation_dict['execution_unit_module'])
        for s in range(simulation_dict['stages']):
            stage = simulation_dict['stage%d' % s]
            for unit in stage:
                signal_blocks = unit['signal_blocks']
                unit['signal_blocks'] = []
                for block in signal_blocks:
                    # Each block: signal_block, delta_block, prediction_t+1, prediction_t+2
                    unit['signal_blocks'].append([block[0], block[1], block[2], SharedArray.SharedNumpyArray_like(block[2])])
                context_blocks = unit['context_blocks']
                unit['context_blocks'] = []
                for block in context_blocks:
                    # Each block: context_block, delta_block, switching_factor
                    unit['context_blocks'].append([block[0], block[1], block[2]])
                readout_blocks = unit['predicted_blocks']
                del unit['predicted_blocks']
                unit['readout_blocks'] = []
                for block in readout_blocks:
                    # Each block: teaching_signal, delta_block, readout_block
                    unit['readout_blocks'].append([block[0], block[1], block[2]])
                # Delta blocks can remain unchanged
                if "learning_rate" in unit.keys():
                    unit['primary_learning_rate'] = unit.pop("learning_rate")
                if "momentum" in unit.keys():
                    unit['primary_momentum'] = unit.pop("momentum")
                    unit['readout_momentum'] = unit['primary_momentum']
                if "additional_learning_rate" in unit.keys():
                    unit['readout_learning_rate'] = unit.pop("additional_learning_rate")
                else:
                    unit['readout_learning_rate'] = simulation_dict["readout_learning_rate"]
                # Output block may remain unchanged
                if "MLP_parameters" in unit.keys():
                    unit['Primary_Predictor_params'] = unit.pop("MLP_parameters")
                if "MLP_parameters_res" in unit.keys():
                    unit['Residual_Predictor_params'] = unit.pop("MLP_parameters_res")
                if "MLP_parameters_additional" in unit.keys():
                    unit['Readout_Predictor_params'] = unit.pop("MLP_parameters_additional")
                unit['flags'] = simulation_dict['flags']
                ex_unit.ExecutionUnit.upgrade_to_ver_1(unit)

        simulation_dict['version_major'] = 1
        simulation_dict['version_minor'] = 0
        # Remove all the old source files
        simulation_dict['sources'] = {}
        logging.info("Upgrade succesfull")
    else:
        for s in range(simulation_dict['stages']):
            stage = simulation_dict['stage%d' % s]
            for unit in stage:
                unit['flags'] = simulation_dict['flags']
        logging.info("Dictionary already ver 1.0 or above, no need to upgrade")


if __name__ == "__main__":
    pass


================================================
FILE: PVM_framework/PVM_SignalProvider.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================
import PVM_framework.AbstractExecutionManager as AbstractExecutionManager
import logging
from PVM_tools.labeled_movie import FrameCollection
import cv2
import os
import numpy as np


class SimpleSignalProvider(AbstractExecutionManager.AbstractSignalProvider):
    def __init__(self, frame_resolution, heatmap_resolution, files, storage, channel="default", remove_files=True, reverse=False):
        """
        Handy object to provide input frames and supervising heatmaps efficiently

        :param frame_resolution:
        :param heatmap_resolution:
        :param files:
        :param channel:
        """
        self.files = files
        self.frame_resolution = frame_resolution
        self.heatmap_resolution = heatmap_resolution
        self.channel = channel
        self.frames = []
        self.nframes = 0
        self.masks = []
        self.index = 0
        self.remove_files = remove_files
        self.reverse = reverse
        self.storage=storage

    def start(self):
        """
        Collect and preprocess all the data
        :return:
        """
        for (labeled_file, channel) in self.files:
            logging.info("Incorporating file %s" % (labeled_file))
            local_labeled_file = self.storage.get(labeled_file)
            if local_labeled_file is not None:
                self.fc = FrameCollection()
                self.fc.load_from_file(local_labeled_file)
                self.nframes += len(self.fc)
                for i in xrange(len(self.fc)):
                    img1 = self.fc.Frame(i).get_image(channel=channel)
                    img = cv2.resize(img1, dsize=self.frame_resolution, interpolation=cv2.INTER_CUBIC)
                    label = self.fc.Frame(i).get_label(channel=channel)
                    if label is not None:
                        label.set_image_shape(shape=img1.shape)
                        mask = self.fc.Frame(i).get_label(channel=channel).get_mask()
                        mask = cv2.resize(mask, dsize=self.heatmap_resolution, interpolation=cv2.INTER_CUBIC)
                    else:
                        mask = np.zeros(self.heatmap_resolution, dtype=np.uint8)
                    self.masks.append(mask)
                    self.frames.append(img)
                if self.remove_files:
                    os.remove(local_labeled_file)

    def get_signal(self, name, time):
        """
        Return the requested signal. If time is zero, return the current signal, otherwise
        return past of future signals as indicated by the time parameter

        :param name:
        :param time:
        :return:
        """
        if name == "frame":
            return self.frames[(self.index+time) % self.nframes]
        elif name == "mask":
            return self.masks[(self.index+time) % self.nframes]
        else:
            raise Exception("Unknown signal type")

    def get_length(self):
        return self.nframes

    def advance(self):
        """
        Move current index by one step forward
        :return:
        """
        if self.reverse:
            self.index = (self.index - 1) % self.nframes
        else:
            self.index = (self.index + 1) % self.nframes

    def finish(self):
        """
        Cleanup
        :return:
        """
        pass

    def reset(self):
        self.index = 0

    def get_index(self):
        """
        Return the index of the current frame in the sequence
        :return:
        """
        return self.index


class StereoSignalProvider(AbstractExecutionManager.AbstractSignalProvider):
    def __init__(self, frame_resolution, heatmap_resolution, files, storage, channel="default", remove_files=True, only_one_channel=""):
        """
        Handy object to provide input frames and supervising heatmaps efficiently

        :param frame_resolution:
        :param heatmap_resolution:
        :param files:
        :param channel:
        :return:
        """
        self.files = files
        self.frame_resolution = frame_resolution
        self.frame_resolution_half = (frame_resolution[0], frame_resolution[1]/2)

        self.heatmap_resolution = heatmap_resolution
        self.channel = channel
        self.frames = []
        self.nframes = 0
        self.masks = []
        self.index = 0
        self.remove_files = remove_files
        self.only_one_channel = only_one_channel
        self.storage=storage

    def start(self):
        """
        Collect and preprocess all the data
        :return:
        """
        for (labeled_file, channel) in self.files:
            logging.info("Incorporating file %s" % (labeled_file))
            local_labeled_file = self.storage.get(labeled_file)
            if local_labeled_file is not None:
                self.fc = FrameCollection()
                self.fc.load_from_file(local_labeled_file)
                self.nframes += len(self.fc)
                for i in xrange(len(self.fc)):
                    img1 = self.fc.Frame(i).get_image(channel="left")
                    imgl = cv2.resize(img1, dsize=self.frame_resolution_half, interpolation=cv2.INTER_CUBIC)
                    img1 = self.fc.Frame(i).get_image(channel="right")
                    imgr = cv2.resize(img1, dsize=self.frame_resolution_half, interpolation=cv2.INTER_CUBIC)
                    if self.only_one_channel == "left":
                        imgr *= 0
                    if self.only_one_channel == "right":
                        imgl *= 0
                    img = self.interlace_two_images(imgl, imgr)
                    label = self.fc.Frame(i).get_label(channel="left")
                    if label is not None:
                        label.set_image_shape(shape=img1.shape)
                        mask = label.get_mask()
                        mask = cv2.resize(mask, dsize=self.heatmap_resolution, interpolation=cv2.INTER_CUBIC)
                    else:
                        mask = np.zeros(self.heatmap_resolution, dtype=np.uint8)
                    self.masks.append(mask)
                    self.frames.append(img)
                if self.remove_files:
                    os.remove(local_labeled_file)

    def interlace_two_images(self, img1, img2):
        interlacedimg = np.zeros((img1.shape[0]+img2.shape[0],
                                  img1.shape[1], img1.shape[2]), dtype=np.uint8)
        interlacedimg[0::2, :, :] = img1
        interlacedimg[1::2, :, :] = img2
        return interlacedimg

    def get_signal(self, name, time):
        """
        Return the requested signal. If time is zero, return the current signal, otherwise
        return past of future signals as indicated by the time parameter

        :param name:
        :param time:
        :return:
        """
        if name == "frame":
            return self.frames[(self.index+time) % self.nframes]
        elif name == "mask":
            return self.masks[(self.index+time) % self.nframes]
        else:
            raise Exception("Unknown signal type")

    def get_length(self):
        return self.nframes

    def advance(self):
        """
        Move current index by one step forward
        :return:
        """
        self.index = (self.index + 1) % self.nframes

    def finish(self):
        """
        Cleanup
        :return:
        """
        pass

    def reset(self):
        self.index = 0

    def get_index(self):
        """
        Return the index of the current frame in the sequence
        :return:
        """
        return self.index


class TripleSignalProvider(AbstractExecutionManager.AbstractSignalProvider):
    def __init__(self, frame_resolution, heatmap_resolution, files1, files2, files3, storage, channel="default", remove_files=True):
        """
        Handy object to provide input frames and supervising heatmaps efficiently

        :param frame_resolution:
        :param heatmap_resolution:
        :param files:
        :param channel:
        :return:
        """
        self.files1 = files1
        self.files2 = files2
        self.files3 = files3
        self.frame_resolution = frame_resolution
        self.heatmap_resolution = heatmap_resolution
        self.channel = channel
        self.frames = []
        self.nframes = 0
        self.masks = []
        self.index = 0
        self.remove_files = remove_files
        self.storage = storage

    def buffer_files(self, idx, channel, files):
        for (labeled_file, channel) in files:
            logging.info("Incorporating file %s" % (labeled_file))
            local_labeled_file = self.storage.get(labeled_file)
            if local_labeled_file is not None:
                self.fc = FrameCollection()
                self.fc.load_from_file(local_labeled_file)
                self.nframes += len(self.fc)
                for i in xrange(len(self.fc)):
                    img1 = self.fc.Frame(i).get_image(channel=channel)
                    img = cv2.resize(img1, dsize=self.frame_resolution, interpolation=cv2.INTER_CUBIC)
                    label = self.fc.Frame(i).get_label(channel=channel)
                    label.set_image_shape(shape=img1.shape)
                    mask = self.fc.Frame(i).get_label(channel=channel).get_mask()
                    mask = cv2.resize(mask, dsize=self.heatmap_resolution, interpolation=cv2.INTER_CUBIC)
                    mask3d = np.zeros((mask.shape[0], mask.shape[1], 3), dtype=mask.dtype)
                    mask3d[:, :, idx] = mask
                    self.masks.append(mask3d)
                    self.frames.append(img)
                if self.remove_files:
                    os.remove(local_labeled_file)

    def start(self):
        """
        Collect and preprocess all the data
        :return:
        """
        self.buffer_files(0, channel=self.channel, files=self.files1)
        self.buffer_files(1, channel=self.channel, files=self.files2)
        self.buffer_files(2, channel=self.channel, files=self.files3)

    def get_signal(self, name, time):
        """
        Return the requested signal. If time is zero, return the current signal, otherwise
        return past of future signals as indicated by the time parameter

        :param name:
        :param time:
        :return:
        """
        if name == "frame":
            return self.frames[(self.index+time) % self.nframes]
        elif name == "mask":
            return self.masks[(self.index+time) % self.nframes]
        else:
            raise Exception("Unknown signal type")

    def get_length(self):
        return self.nframes

    def advance(self):
        """
        Move current index by one step forward
        :return:
        """
        self.index = (self.index + 1) % self.nframes

    def finish(self):
        """
        Cleanup
        :return:
        """
        pass

    def reset(self):
        self.index = 0

    def get_index(self):
        """
        Return the index of the current frame in the sequence
        :return:
        """
        return self.index


================================================
FILE: PVM_framework/PVM_Storage.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================
import ssl
import socket
import sys
import traceback
import os
import logging
import re
import random
import hashlib
import errno
import shutil


def get_S3_credentials():
    """
    The function will attempt to load credentials from ~/.aws/credentials
    (as well as environment variables, boto config files and other places)

    If succesfull will return a credentials object.

    :return:
    """
    from boto.provider import Provider
    from boto.sts.credentials import Credentials
    provider = Provider('aws')
    if provider.access_key and provider.secret_key:
        cred = Credentials()
        cred.access_key = provider.access_key
        cred.secret_key = provider.secret_key
        return cred
    else:
        logging.getLogger(__name__).info("Could not load permanent credentials")
        return None


class Storage(object):

    _pattern = re.compile("[a-zA-Z0-9._\\-/]*$")

    def __init__(self, local_storage_folder="~/PVM_data", S3_bucket="technical.services.braincorporation.net"):
        """
        Initialize storage object. All the elements will by stored in the local_storage_folder.

        If in addition an S3 bucket and credentials are given, the local storage will act as local cache
        for S3.
        :param local_storage_folder: A folder where the files are stored/mirrored
        :param S3_bucket: Optional S3 bucket where the data will be stored
        :param S3_credentials: Credentials to access the bucket
        :return:
        """
        if S3_bucket is not None:
            S3_credentials = get_S3_credentials()
            if S3_credentials is not None and S3_credentials.access_key and S3_credentials.secret_key:
                import boto
                logging.getLogger('boto').setLevel(logging.CRITICAL)
                self.connector = boto.connect_s3(S3_credentials.access_key,
                                                 S3_credentials.secret_key,
                                                 security_token=S3_credentials.session_token,
                                                 calling_format='boto.s3.connection.OrdinaryCallingFormat')
                # getting bucket with disabled validation to avoid connection error:'S3ResponseError:403 URLBlocked'
                self.bucket = self.connector.get_bucket(S3_bucket, validate=False)
            else:
                self.bucket = None
        else:
            self.bucket = None
        self.local_storage = os.path.expanduser(local_storage_folder)
        if not os.path.exists(self.local_storage):
            os.mkdir(self.local_storage)

    def is_valid_key(self, key):
        return self._pattern.match(key) is not None

    def exists(self, key):
        """
        Do not check existence of a folder because there are no folders in S3
        """
        assert self.is_valid_key(key)
        from boto.exception import S3ResponseError
        try:
            k = self.bucket.get_key(key)
            return k is not None
        except S3ResponseError:
            # When using a guest account, checking whether an object exists
            # will result in a 503 Forbidden exception if it does not exist
            return False

    def _ensure_containing_folder_exists(self, filepath):
        """
        Make sure that the folder containing filepath exists already to avoid
        errors when creating the file.
        """
        directory = os.path.dirname(filepath)
        try:
            os.makedirs(directory)
        except OSError as err:
            if err.errno != errno.EEXIST:
                raise

    def get(self, path, to_path=None, force=False):
        """
        Get a file from local/remote storage. The file will get downloaded from S3 if bucket is in use, to a local storage
        directory. Path to the file in local storage directory will be returned.
        :param path:
        :param to_path:
        :param force:
        :return:
        """
        logging.info("Storage received a request to get file %s" % path)
        if self.bucket is None:
            file_p = os.path.join(self.local_storage, path)
            logging.info("Loading file %s" % file_p)
            if not os.path.exists(file_p):
                # maybe then such file actually exist in the file system?
                if not os.path.exists(path):
                    logging.error("File %s does not exist!" % path)
                    raise Exception("Requested file %s does not exist" % path)
                else:
                    return path
            else:
                return file_p
        else:
            if to_path is None:
                dest_path = os.path.join(self.local_storage, path)
            else:
                dest_path = to_path
            if os.path.isfile(dest_path):
                if self.exists(path) and force:
                    os.remove(dest_path)
                elif force:
                    raise Exception("Forcing download but the original file does not exist on S3")
                else:
                    return dest_path
            try:
                if self.exists(path) and not path.endswith("/"):
                    while True:
                        try:
                            self.download(to_file=dest_path, key=path, progress_callback=self._progress_download)
                            break
                        except ssl.SSLError, socket.error:
                            logging.error("Download timeout, trying again! file %s" % path)
                            continue
                    logging.info("Downloaded file %s to %s" % (path, dest_path))
                    return dest_path
                else:
                    if not os.path.exists(path):
                        logging.error("File %s does not exist!" % path)
                        raise Exception("Requested file %s does not exist" % path)
                    else:
                        logging.info("Using file found in the local file system. %s" % path)
                        return path
            except:
                traceback.print_exc(file=sys.stdout)
                return None

    def put(self, path=None, from_path=None, to_folder=None, overwrite=False):
        """
        Put a file to local/remot storage.
        If using an S3 bucket the file will be uploaded. If not it will be copied to local storage

        :param path: path (key) within the local cache structure (same are remote S3 path)
        :type path: str
        :param from_path: (optional) path to the file that should be uploaded
        :type from path: str
        :param to_folder: (optional) remote directory to which the file needs to be uploaded
        :type to_folder: str
        :param overwrite: if True a file will be overwritten if it already exists remotely
        :type overwrite: bool
        :return: True if successful, None if error
        """
        if path == "/":
            path = ""
        # Just copy to the right directory
        if path is None:
            dst = os.path.join(self.local_storage, to_folder)
            dst = os.path.join(dst, os.path.basename(from_path))
        else:
            dst = os.path.join(self.local_storage, path)
        if os.path.isfile(dst) and not overwrite:
            logging.error("File %s already exists, not overwriting" % dst)
            return None
        self._ensure_containing_folder_exists(dst)
        if not (os.path.isfile(dst) and os.path.samefile(from_path, dst)):
            shutil.copy2(from_path, dst)
        if self.bucket is not None:
            if from_path is None:
                source_path = os.path.join(self.local_storage, path)
            else:
                source_path = from_path
            if to_folder is None:
                to_folder = os.path.dirname(path)
            if not os.path.isfile(source_path):
                logging.error("File %s does not exists" % source_path)
                return None
            while True:
                try:
                    self.upload(from_file=source_path,
                                to_folder=to_folder,
                                progress_callback=self._progress_upload,
                                overwrite=overwrite)
                    break
                except ssl.SSLError, socket.error:
                    logging.error("Upload timeout, trying again!, file %s" % source_path)
                    continue
            logging.info("Uploaded file %s to folder %s" % (source_path, to_folder))
            return True

    def download(self, to_file, key, progress_callback=None):
        """
        Download a file from S3 to a local storage

        :param to_file: filename in which to store the downloaded file (str)
        :param key: the key to download from the bucket (str)
        :param progress_callback: callback function printing progress
        :return:
        """
        from boto.s3.key import Key
        k = Key(self.bucket, key)
        self._ensure_containing_folder_exists(to_file)
        tmpfile = to_file + '.tmp.%s' % hashlib.sha1(str(random.getrandbits(256))).hexdigest()
        try:
            k.get_contents_to_filename(filename=tmpfile, cb=progress_callback, num_cb=100)
        except:
            if os.path.exists(tmpfile):
                os.remove(tmpfile)
            raise
        # download is successful, move the temporary file to desired location
        os.rename(tmpfile, to_file)

    def upload(self, from_file, to_folder, overwrite=False, progress_callback=None):
        """
        Upload a file to S3 bucket.
        S3 doen't have concept of folder so /path/name.txt is just a long filename.
        It means that 'folders' will be created automatically if they do not exists

        :param from_file: path to the file being uploaded
        :param to_folder: S3 "folder" to which the file should go.
        :param overwrite: overwrite if True
        :param progress_callback: callback function printing progress
        :return:
        """
        from boto.s3.key import Key
        custom_key_name = os.path.basename(from_file)
        keyname = os.path.join(to_folder, custom_key_name)
        assert self.is_valid_key(keyname)
        # check that file exists
        if not overwrite and self.exists(keyname):
            raise Exception('Can not upload, file %s already exists on amazon' %
                            keyname)

        logging.getLogger(__name__).info('UI:Uploading to "%s"...' % keyname)
        k = Key(self.bucket, keyname)
        k.set_contents_from_filename(from_file, cb=progress_callback, num_cb=100)
        return True

    def _progress_download(self, elapsed, total):
        """
        Prints the download progress
        :param elapsed:
        :param total:
        :return:
        """
        print "Downloading %d %% \r" % (elapsed*100/max(total, 1)),
        sys.stdout.flush()

    def _progress_upload(self, elapsed, total):
        """
        Prints the upload progress
        :param elapsed:
        :param total:
        :return:
        """
        print "Uploading %d %% \r" % (elapsed*100/max(total, 1)),
        sys.stdout.flush()

    def list_path(self, path):
        """
        List all the keys in the remote path

        :param path: path (key) in the local or remote storage
        :type path: str
        :return: list of keys
        :rtype: list
        """
        if path == "/":
            path = ""
        result = []
        if self.bucket is None:
            for root, dirs, files in os.walk(self.local_storage, topdown=False):
                for name in files:
                    result.append(os.path.join(root, name))
        else:
            assert self.is_valid_key(path)
            keys_list = self.bucket.list(prefix=path)
            result = [key.name for key in keys_list if os.path.normpath(key.name) != os.path.normpath(path)]
        return result

    def remove(self, key, check_existence=True):
        """
        Remove a file from local/remote storage

        :param key:
        :param check_existence:
        """
        if self.bucket is None:
            if check_existence and not os.path.exists(os.path.join(self.local_storage, key)):
                raise Exception('Can not remove non-existent key %s' % key)
            os.remove(os.path.join(self.local_storage, key))
        else:
            from boto.s3.key import Key
            if check_existence and not self.exists(key):
                raise Exception('Can not remove non-existent key %s' % key)
            k = Key(self.bucket)
            k.key = key
            self.bucket.delete_key(k)


def test_storage_local():
    """
    """
    S = Storage(local_storage_folder="~/PVM_test")
    contents = S.list_path("")
    assert contents == []
    S.put(path="", from_path=os.path.abspath(__file__))
    contents = S.list_path("")
    assert len(contents) == 1
    S.remove(__file__)
    contents = S.list_path("")
    assert len(contents) == 0


def test_storage_remote():
    """
    """
    S = Storage(local_storage_folder="~/PVM_test_local_remote", S3_bucket="technical.services.braincorporation.net", S3_credentials=get_S3_credentials())
    contents = S.list_path("")
    N = len(contents)
    S.put(path="", from_path=os.path.abspath(__file__), overwrite=True)
    contents = S.list_path("")
    assert len(contents) == (N + 1)
    S.remove(__file__)
    contents = S.list_path("")
    assert len(contents) == N


if __name__ == "__main__":
    test_storage_local()
    test_storage_remote()


================================================
FILE: PVM_framework/PVM_datasets.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================


import PVM_framework.PVM_Storage as PVM_Storage

import logging

sets = {
    "face_training": [["PVM_data/face01.pkl", "default"],
                      ["PVM_data/face03.pkl", "default"],
                      ["PVM_data/no_target.pkl", "default"],
                      ["PVM_data/face16.pkl", "default"],
                      ["PVM_data/face17.pkl", "default"],
                      ["PVM_data/face18.pkl", "default"],
                      ["PVM_data/face19.pkl", "default"],
                      ["PVM_data/no_target_01.pkl", "default"],
                      ["PVM_data/face20.pkl", "default"],
                      ["PVM_data/face21.pkl", "default"],
                      ],
    "face_testing": [["PVM_data/face02.pkl", "default"],
                     ["PVM_data/face04.pkl", "default"],
                     ["PVM_data/face05.pkl", "default"],
                     ["PVM_data/face06.pkl", "default"],
                     ["PVM_data/face07.pkl", "default"],
                     ["PVM_data/face08.pkl", "default"],
                     ["PVM_data/face09.pkl", "default"],
                     ["PVM_data/face10.pkl", "default"],
                     ["PVM_data/face11.pkl", "default"],
                     ["PVM_data/face12.pkl", "default"],
                     ["PVM_data/face13.pkl", "default"],
                     ["PVM_data/face14.pkl", "default"],
                     ["PVM_data/face15.pkl", "default"],
                     ["PVM_data/face22.pkl", "default"],
                     ["PVM_data/face23.pkl", "default"],
                     ["PVM_data/face24.pkl", "default"],
                     ],
    "face_ex_testing": [["PVM_data/face25.pkl", "default"],
                        ["PVM_data/face26.pkl", "default"],
                        ["PVM_data/face27.pkl", "default"],
                        ["PVM_data/face28.pkl", "default"],
                        ["PVM_data/face29.pkl", "default"],
                        ["PVM_data/face30.pkl", "default"],
                        ["PVM_data/face31.pkl", "default"],
                        ["PVM_data/face32.pkl", "default"],
                        ["PVM_data/face33.pkl", "default"],
                        ],
    "face_additional": [],
    "green_ball_training": [["PVM_data/green_ball_long.pkl", "default"],
                            ["PVM_data/green_ball_on_grass.pkl", "default"],
                            ["PVM_data/green_ball_test_14.pkl", "default"],
                            ["PVM_data/green_ball_test_15.pkl", "default"],
                            ["PVM_data/green_ball_test_16.pkl", "default"],
                            ],
    "green_ball_testing": [["PVM_data/green_ball_test.pkl", "default"],
                           ["PVM_data/green_ball_test_01.pkl", "default"],
                           ["PVM_data/green_ball_test_02.pkl", "default"],
                           ["PVM_data/green_ball_test_03.pkl", "default"],
                           ["PVM_data/green_ball_test_04.pkl", "default"],
                           ["PVM_data/green_ball_test_05.pkl", "default"],
                           ["PVM_data/green_ball_test_06.pkl", "default"],
                           ["PVM_data/green_ball_test_07.pkl", "default"],
                           ["PVM_data/green_ball_test_08.pkl", "default"],
                           ["PVM_data/green_ball_test_09.pkl", "default"],
                           ["PVM_data/green_ball_test_10.pkl", "default"],
                           ["PVM_data/green_ball_test_11.pkl", "default"],
                           ["PVM_data/green_ball_test_12.pkl", "default"],
                           ["PVM_data/green_ball_test_13.pkl", "default"],
                           ["PVM_data/green_ball_01_small.pkl", "default"],
                           ["PVM_data/green_ball_bc_office.pkl", "default"],
                           ],
    "green_ball_ex_testing": [["PVM_data/green_ball_test_17.pkl", "default"],
                              ["PVM_data/green_ball_test_18.pkl", "default"],
                              ["PVM_data/green_ball_test_19.pkl", "default"],
                              ["PVM_data/green_ball_test_20.pkl", "default"],
                              ["PVM_data/green_ball_test_21.pkl", "default"],
                              ["PVM_data/green_ball_test_22.pkl", "default"],
                              ["PVM_data/green_ball_test_23.pkl", "default"],
                              ["PVM_data/green_ball_test_24.pkl", "default"],
                              ["PVM_data/green_ball_test_25.pkl", "default"],
                              ["PVM_data/green_ball_test_26.pkl", "default"],
                              ["PVM_data/green_ball_test_27.pkl", "default"],
                              ["PVM_data/green_ball_test_28.pkl", "default"],
                              ["PVM_data/green_ball_test_29.pkl", "default"],
                              ],
    "green_ball_additional": [["PVM_data/blue_ball_on_grass_daytime.pkl", "default"],
                              ["PVM_data/blue_ball_at_home_02.pkl", "default"],
                              ["PVM_data/blue_ball_at_home_01.pkl", "default"],
                              ],
    "stop_sign_training": [["PVM_data/stop01.pkl", "default"],
                           ["PVM_data/stop03.pkl", "default"],
                           ["PVM_data/stop05.pkl", "default"],
                           ["PVM_data/stop07.pkl", "default"],
                           ["PVM_data/stop09.pkl", "default"],
                           ["PVM_data/stop11.pkl", "default"],
                           ["PVM_data/stop13.pkl", "default"],
                           ["PVM_data/stop15.pkl", "default"],
                           ["PVM_data/stop17.pkl", "default"],
                           ["PVM_data/stop19.pkl", "default"],
                           ["PVM_data/stop21.pkl", "default"],
                           ["PVM_data/stop23.pkl", "default"],
                           ["PVM_data/stop25.pkl", "default"],
                           ["PVM_data/stop27.pkl", "default"],
                           ["PVM_data/stop29.pkl", "default"],
                           ["PVM_data/stop32.pkl", "default"],
                           ["PVM_data/stop34.pkl", "default"],
                           ["PVM_data/stop36.pkl", "default"],
                           ["PVM_data/stop38.pkl", "default"],
                           ["PVM_data/stop40.pkl", "default"],
                           ],
    "stop_sign_testing": [["PVM_data/stop02.pkl", "default"],
                          ["PVM_data/stop04.pkl", "default"],
                          ["PVM_data/stop06.pkl", "default"],
                          ["PVM_data/stop08.pkl", "default"],
                          ["PVM_data/stop10.pkl", "default"],
                          ["PVM_data/stop12.pkl", "default"],
                          ["PVM_data/stop14.pkl", "default"],
                          ["PVM_data/stop16.pkl", "default"],
                          ["PVM_data/stop18.pkl", "default"],
                          ["PVM_data/stop20.pkl", "default"],
                          ["PVM_data/stop22.pkl", "default"],
                          ["PVM_data/stop24.pkl", "default"],
                          ["PVM_data/stop26.pkl", "default"],
                          ["PVM_data/stop28.pkl", "default"],
                          ["PVM_data/stop30.pkl", "default"],
                          ["PVM_data/stop33.pkl", "default"],
                          ["PVM_data/stop35.pkl", "default"],
                          ["PVM_data/stop37.pkl", "default"],
                          ["PVM_data/stop39.pkl", "default"],
                          ],
    "stop_sign_additional": [],
    "stop_sign_ex_testing": [["PVM_data/stop41.pkl", "default"],
                             ["PVM_data/stop42.pkl", "default"],
                             ["PVM_data/stop43.pkl", "default"],
                             ["PVM_data/stop44.pkl", "default"],
                             ["PVM_data/stop45.pkl", "default"],
                             ["PVM_data/stop46.pkl", "default"],
                             ["PVM_data/stop47.pkl", "default"],
                             ["PVM_data/stop48.pkl", "default"],
                             ["PVM_data/stop49.pkl", "default"],
                             ["PVM_data/stop50.pkl", "default"],
                             ["PVM_data/stop51.pkl", "default"],
                             ],
    "short_training": [["PVM_data/stop40.pkl", "default"]],
    "short_testing": [["PVM_data/stop41.pkl", "default"]],
    "short_additional": [],

    "non_spec_training": [["PVM_data/no_target.pkl", "default"],
                          ["PVM_data/no_target_01.pkl", "default"],
                          ["PVM_data/green_ball_long.pkl", "default"],
                          ["PVM_data/stop01.pkl", "default"],
                          ["PVM_data/green_ball_on_grass.pkl", "default"],
                          ["PVM_data/stop03.pkl", "default"],
                          ["PVM_data/face01.pkl", "default"],
                          ["PVM_data/stop05.pkl", "default"],
                          ["PVM_data/face03.pkl", "default"],
                          ["PVM_data/stop07.pkl", "default"],
                          ["PVM_data/face16.pkl", "default"],
                          ["PVM_data/stop09.pkl", "default"],
                          ["PVM_data/face17.pkl", "default"],
                          ["PVM_data/stop11.pkl", "default"],
                          ["PVM_data/face18.pkl", "default"],
                          ["PVM_data/stop13.pkl", "default"],
                          ["PVM_data/green_ball_test_14.pkl", "default"],
                          ["PVM_data/stop15.pkl", "default"],
                          ["PVM_data/green_ball_test_15.pkl", "default"],
                          ["PVM_data/stop17.pkl", "default"],
                          ["PVM_data/green_ball_test_16.pkl", "default"],
                          ["PVM_data/stop19.pkl", "default"],
                          ["PVM_data/stop21.pkl", "default"],
                          ["PVM_data/stop23.pkl", "default"],
                          ],
    "non_spec_testing": [["PVM_data/stop41.pkl", "default"]],
    "non_spec_additional": [],
}


sets["stop_sign_full_testing"] = sets["stop_sign_testing"] + sets["stop_sign_ex_testing"]
sets["green_ball_full_testing"] = sets["green_ball_testing"] + sets["green_ball_ex_testing"]
sets["face_full_testing"] = sets["face_testing"] + sets["face_ex_testing"]

sets["stop_sign_fast_testing"] = [sets["stop_sign_testing"][0]]
sets["green_ball_fast_testing"] = [sets["green_ball_testing"][0]]
sets["face_fast_testing"] = [sets["face_testing"][0]]


class PVMDataset(object):
    def __init__(self, name, storage=None):
        logging.info("Parsing dataset info on %s" % name)
        self.name = name
        self.all = []
        if name+"_training" in sets.keys():  # ok so we have a good dataset name
            self.training = sets[name+"_training"]
            self.testing = sets[name+"_testing"]
            self.additional = sets[name+"_additional"]
            logging.info("Dataset appears to be defined withing datasets")
        elif name == "all":
            self.training = []
            self.testing = []
            self.additional = []
            for ds in ("stop_sign", "green_ball", "face"):
                self.training.extend(sets["%s_training" % ds])
                self.testing.extend(sets["%s_testing" % ds])
                self.testing.extend(sets["%s_ex_testing" % ds])
                self.additional.extend(sets["%s_additional" % ds])
            logging.info("Dataset is a special keyword")
        elif name == "all_short":
            self.training = []
            self.testing = []
            self.additional = []
            for ds in ("stop_sign", "green_ball", "face"):
                self.training.append(sets["%s_training" % ds][0])
                self.testing.append(sets["%s_testing" % ds][0])
                self.additinal.append(sets["%s_testing" % ds][0])
            logging.info("Dataset is a special keyword")
        else:  # it does not match anything but maybe it is just a filename directly
            logging.info("Dataset is unknown, trying a file")
            try:
                logging.info("Trying to download the file %s" % name)
                filename = storage.get(name)
                logging.info("got file %s" % filename)
            except:
                logging.error("Invalid dataset")
                raise(Exception("Invalid dataset"))
            if filename is not None:
                # the file is valid
                logging.info("Got the file")
                self.training = [[name, "default"]]
                self.testing = [[name, "default"]]
                self.additional = [[name, "default"]]
            else:
                logging.error("Invalid dataset")
                raise(Exception("Invalid dataset"))
        self.all.extend(self.training)
        self.all.extend(self.testing)
        self.all.extend(self.additional)


if __name__ == "__main__":
    import PVM_tools.labeled_movie as lm
    TS = PVM_Storage.Storage()
    for set in sets.keys():
        print "Set :" + str(set)
        set_l = 0
        for element in sets[set]:
            local_path = TS.get(element[0])
            fc = lm.FrameCollection()
            fc.load_from_file(local_path)
            fc.write_to_file(local_path)
            print "File %s, %d frames %f minutes" % (local_path, len(fc), len(fc)/(25.0*60))
            set_l += len(fc)
        print "Set %s, %f frames, %f minutes" % (set, set_l, set_l/(25.0*60))


================================================
FILE: PVM_framework/PVM_debug_console.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================

import numpy as np
import PVM_framework.SharedArray as SharedArray
import PVM_framework.PVM_Create as PVM_Create
import os
import sys
import argparse
import pprint
import cmd
import traceback
import time
import logging


class InteractiveDictionaryExplorer(cmd.Cmd):
    use_rawinput = False

    def __init__(self,
                 stdin=None,
                 stdout=None,
                 infilename=None,
                 dict=None,
                 pprint=False,
                 sprint=False,
                 nans=None,
                 gt=None,
                 lt=None,
                 abss=None,
                 filter_name=None,
                 upgrade=False):
        """
        Console object used to traverse the simulation dictionary. Can be used standalone on a saved distinary or
        live on a running simulation by logging into the debug port.

        :param stdin:
        :param stdout:
        :param infilename:
        :param dict:
        :param pprint:
        :param sprint:
        :param nans:
        :param gt:
        :param lt:
        :param abss:
        :param filter_name:
        :return:
        """

        if stdin is not None:
            self.stdin = stdin
        else:
            self.stdin=sys.stdin
            self.use_rawinput=True
        if stdout is not None:
            self.stdout = stdout
        else:
            self.stdout=sys.stdout
        cmd.Cmd.__init__(self, stdin=self.stdin, stdout=self.stdout)
        if dict is None and infilename is None:
            sys.stderr.write("dict and infilename cannot be empty at the same time")
        if dict is not None:
            self.dict = dict
            self.filename = str(infilename)
        elif os.path.exists(infilename) and os.path.isfile(infilename):
            # This import below needs to go here as otherwise it would
            # lead to a circular import and failure.
            import PVM_framework.CoreUtils as CoreUtils
            self.dict = CoreUtils.load_model(infilename)
            if upgrade:
                import PVM_framework.PVM_Create as PVM_Create
                PVM_Create.upgrade_dictionary_to_ver1_0(self.dict)
        else:
            sys.stderr.write("Input file not found\n")
        self.pprint = pprint
        self.sprint = sprint
        self.filename = str(infilename)
        self.gt = None
        self.lt = None
        self.nans = None
        self.filter_name = None
        self.abss=None
        if gt is not None:
            self.gt = float(gt)
        if lt is not None:
            self.lt = float(lt)
        if nans is not None:
            self.nans = True
        if filter_name is not None:
            self.filter_name = filter_name
        if abss is not None:
            self.abss = float(abss)
        self.text_bold = '\033[1m'
        self.text_end = '\033[0m'
        self.prompt = self.text_bold + str(self.dict['name'])+"/$ " + self.text_end
        self.current_element = self.dict
        self.pwd = "/"
        logging.info("Created an interactive dictionary explorer session")

    def getsizeof(self, element):
        size=0
        try:
            size = element.nbytes
        except:
            size = sys.getsizeof(element)
        return size

    def getshapeof(self, element):
        shape = "---"
        try:
            shape = str(element.shape)
        except:
            shape = "---"
        return shape

    def gettypename(self, element):
        name = ""
        try:
            name = element.__class__.__name__
        except:
            name = type(element).__name__
        return name

    def emptyline(self):
        pass

    def do_ls(self, line, quiet=False):
        """
        List the contents of the current element in the dictionary
        """
        if line.startswith("/"):
            listed_element = self.getsubelement(self.dict, line)
        else:
            listed_element = self.getsubelement(self.current_element, line)
        if not quiet:
            print >>self.stdout, "%-*s " % (30, "Element name"),
            print >>self.stdout, "%*s " % (20, "type"),
            print >>self.stdout, "%*s " % (12, "shape"),
            print >>self.stdout, "%*s " % (12, "size (bytes)"),
            print >>self.stdout, "%*s " % (15, "id")
            print >>self.stdout, "%-*s " % (30, "."),
            print >>self.stdout, "%*s " % (20, self.gettypename(listed_element)),
            print >>self.stdout, "%*s " % (12, self.getshapeof(listed_element)),
            print >>self.stdout, "%*s " % (12, self.getsizeof(listed_element)),
            print >>self.stdout, "%*s " % (15, id(listed_element))
        if isinstance(listed_element, dict):
            for key in listed_element.keys():
                element = listed_element[key]
                print >>self.stdout, "%-*s " % (30, key),
                print >>self.stdout, "%*s " % (20, self.gettypename(element)),
                print >>self.stdout, "%*s " % (12, self.getshapeof(element)),
                print >>self.stdout, "%*s " % (12, self.getsizeof(element)),
                print >>self.stdout, "%*s " % (15, id(element))
        elif isinstance(listed_element, list):
            for i, element in enumerate(listed_element):
                print >>self.stdout, "%-*s " % (30, str(i)),
                print >>self.stdout, "%*s " % (20, self.gettypename(element)),
                print >>self.stdout, "%*s " % (12, self.getsizeof(element)),
                print >>self.stdout, "%*s " % (12, self.getshapeof(element)),
                print >>self.stdout, "%*s " % (15, id(element))
        elif isinstance(listed_element, tuple):
            for i, element in enumerate(listed_element):
                print >>self.stdout, "%-*s " % (30, str(i)),
                print >>self.stdout, "%*s " % (20, self.gettypename(element)),
                print >>self.stdout, "%*s " % (12, self.getsizeof(element)),
                print >>self.stdout, "%*s " % (12, self.getshapeof(element)),
                print >>self.stdout, "%*s " % (15, id(element))
        else:
            pprint.pprint(listed_element, stream=self.stdout)

    def do_cd(self, path):
        """
        Change the current element in the dictionary
        """
        wd = ""
        current_dir = self.dict
        if path.endswith("/") and len(path)>1:
            path = path[:-1]
        if path.startswith("/"):
            wd = path
        elif path == ".." or path == "../":
            wd = "/"+"/".join(self.pwd.split("/")[:-1])
        elif path == "../.." or path == "../../":
            wd = "/"+"/".join(self.pwd.split("/")[:-2])
        else:
            wd = self.pwd + "/" + path
        subdirs = wd.split("/")
        try:
            for subdir in subdirs:
                if subdir.isdigit():
                    i = int(subdir)
                    current_dir = current_dir[i]
                elif subdir != '':
                    current_dir = current_dir[subdir]
            self.current_element = current_dir
            self.pwd = wd.replace("//", "/")
            self.prompt = self.text_bold + self.dict['name'] + self.pwd + "$ " + self.text_end
        except:
            print >>self.stdout, "Invalid path"

    def getsubelement(self, element, path):
        current_dir = element
        subdirs = path.split("/")
        try:
            for subdir in subdirs:
                if subdir.isdigit():
                    i = int(subdir)
                    current_dir = current_dir[i]
                elif subdir != '':
                    current_dir = current_dir[subdir]
            return current_dir
        except:
            print >>self.stdout, "Invalid path"

    def getsubelements(self, element):
        if isinstance(element, dict):
            return element.keys()
        if isinstance(element, list):
            return map(lambda x: str(x), range(len(element)))
        return []

    def generic_complete(self, text, line, beginidx, endidx):
        if "/" in line:
            (cmd, arg) = line.split(" ")
            if not arg.endswith("/"):
                arg = "/".join(arg.split("/")[:-1])
            element = self.getsubelement(self.current_element, arg)
            prefix = "/".join(text.split("/")[:-1])
        else:
            element = self.current_element
            prefix = ""
        all_completions = map(lambda x: prefix + x, self.getsubelements(element))
        if not text:
            return all_completions
        else:
            return [c for c in all_completions if c.startswith(text)]

    def complete_cd(self, text, line, beginidx, endidx):
        return self.generic_complete(text, line, beginidx, endidx)

    def complete_ls(self, text, line, beginidx, endidx):
        return self.generic_complete(text, line, beginidx, endidx)

    def do_cat(self, line):
        try:
            print >>self.stdout, self.current_element[line]
        except:
            print >>self.stdout, "Cound not access the element"

    def do_debug_create(self, line):
        """
        Creates a debug hookup. Called in a given context needs:
        Nescessary arguments:

                filename='...'
                object=name of the object in the given context
                interval=number of steps between successive dumps

        Optional:

                phase=phase shift of the dump (default 0)

        E.g.:
                debug_create filename='my_debug.p' object=weights0 interval=10 phase=5
        """
        try:
            params = line.split(" ")
            args = {}
            for param in params:
                (L, R) = param.split('=')
                args[L]=R
            if "filename" not in args.keys():
                raise
            if "object" not in args.keys():
                raise
            if "interval" not in args.keys():
                raise
            if 'phase' not in args.keys():
                args['phase'] = 0
        except:
            print >>self.stdout, "Error processing arguments"
            logging.debug("Exception in debug hookup creation, error processing arguments")
        try:
            hookup = {}
            hookup['filename'] = args['filename']
            # Will raise an exception unless the call below succedes
            self.current_element[args['object']].view(np.ndarray)
            hookup['object'] = self.current_element[args['object']]
            hookup['interval'] = int(args['interval'])
            hookup['phase'] = int(args['phase'])
            hookup['path'] = self.pwd + '/' + args['object']
            self.dict['debug_infrastructure']['disabled_hookups'][args['filename']] = hookup
        except:
            print >>self.stdout, "Error making a debug hookup"
            traceback.print_exc(file=self.stdout)
            logging.debug("Exception in debug hookup creation")
        logging.info("Created a debug hookup with parameters " + line)

    def do_debug_disable(self, line):
        """
        Disable a currently enabled debug hookup
        """
        try:
            hookup = self.dict['debug_infrastructure']['enabled_hookups'][line]
            self.dict['debug_infrastructure']['disabled_hookups'][line]=hookup
            del self.dict['debug_infrastructure']['enabled_hookups'][line]
            print >>self.stdout, "Disabled "+line
        except:
            print >>self.stdout, "Error disabling a debug hookup"
            traceback.print_exc(file=self.stdout)
            logging.debug("Exception while disabling a debug hookup " + line)
        logging.info("Disabled a debug hookup " + line)

    def do_debug_enable(self, line):
        """
        Enable a currently disabled debug hookup
        """
        try:
            hookup = self.dict['debug_infrastructure']['disabled_hookups'][line]
            self.dict['debug_infrastructure']['enabled_hookups'][line]=hookup
            del self.dict['debug_infrastructure']['disabled_hookups'][line]
            print >>self.stdout, "Enabled "+line
        except:
            print >>self.stdout, "Error enabling a debug hookup"
            traceback.print_exc(file=self.stdout)
            logging.debug("Exception while enabling a debug hookup " + line)
        logging.info("Enabled a debug hookup " + line)

    def do_debug_delete(self, line):
        """
        Delete an existing enabled or disabled debug hookup
        """
        try:
            del self.dict['debug_infrastructure']['disabled_hookups'][line]
            print >>self.stdout, "Deleted the debug hookup from disabled"
            logging.info("Deleted the debug hookup from disabled")
        except:
            pass
        try:
            del self.dict['debug_infrastructure']['enabled_hookups'][line]
            print >>self.stdout, "Deleted the debug hookup from enabled"
            logging.info("Deleted the debug hookup from enabled")
        except:
            pass

    def do_debug_list_enabled(self, line):
        """
        List existing enabled debug hookups
        """
        self.do_ls("/debug_infrastructure/enabled_hookups", quiet=True)

    def do_debug_list_disabled(self, line):
        """
        List existing disabled debug hookups
        """
        self.do_ls("/debug_infrastructure/disabled_hookups", quiet=True)

    def do_debug_list(self, line):
        """
        List existing debug hookups
        """
        print >>self.stdout, "--Enabled-------------------------------------------------------------------"
        self.do_debug_list_enabled(line)
        print >>self.stdout, "--Disabled------------------------------------------------------------------"
        self.do_debug_list_disabled(line)

    def do_quit(self, line):
        """
        Exit program
        """
        return True

    def list_to_dict(self, something):
        if isinstance(something, dict):
            return something.copy()
        if isinstance(something, list):
            d = {}
            for i in xrange(len(something)):
                d["EL_"+str(i)] = self.list_to_dict(something[i])
            return d

    def do_python(self, line):
        """
        Execute a python command in the current context. Objects appearing in the current context
        will be accesible by their name. One can put import statements etc.
        :param line:
        :return:
        """
        oldout = sys.stdout
        olderr = sys.stderr
        env = self.list_to_dict(self.current_element)
        sys.stdout=self.stdout
        sys.stderr=self.stdout
        try:
            exec(line, env, env)
        except:
            traceback.print_exc(file=self.stdout)
        sys.stdout = oldout
        sys.stderr = olderr

    def help_quit(self):
        print >>self.stdout, "Exit the interactive shell"

    do_EOF = do_quit
    help_EOF = help_quit

    def search_recursive(self, dict_object, path, name, leaf_method):
        if (isinstance(dict_object, np.ndarray) or
           isinstance(dict_object, SharedArray.SharedNumpyArray) or
           isinstance(dict_object, SharedArray.DoubleBufferedSharedNumpyArray)):
                if self.filter_name is not None:
                    if name == self.filter_name:
                        leaf_method(path, name, dict_object)
                else:
                    leaf_method(path, name, dict_object)
                return
        if isinstance(dict_object, dict):
            for key in dict_object.keys():
                self.search_recursive(dict_object[key], path + "/" + str(key), key, leaf_method)
        if isinstance(dict_object, list):
            for i, element in enumerate(dict_object):
                self.search_recursive(element, path + "[" +str(i) + "]", "", leaf_method)

    def print_path(self, path, name, element):
        print >>self.stdout, path

    def check_gt(self, path, name, element):
        if np.max(element) > self.gt:
            print >>self.stdout, path + " has elements greater than " + str(self.gt) + " (" + str(np.max(element)) +")"
            print >>self.stdout, element

    def check_abs(self, path, name, element):
        if np.max(np.fabs(element)) > self.abss:
            print >>self.stdout, path + " has elements with absolute value greater than " + str(self.abss) + " (" + str(np.max(np.fabs(element))) +")"
            print >>self.stdout, element

    def check_lt(self, path, name, element):
        if np.min(element) < self.lt:
            print >>self.stdout, path + " has elements less than " + str(self.lt) + " (" + str(np.min(element)) +")"
            print >>self.stdout, element

    def check_nans(self, path, name, element):
        if np.isnan(element).any():
            print >>self.stdout, path + " contains NANs "
            print >>self.stdout, element

    def check_id(self, path, name, element):
        if int(id(element)) == self.search_identity:
            print >>self.stdout, path

    def run_noninteractive(self):
        if self.pprint:
            pprint.pprint(self.dict, stream=self.stdout)
        if self.sprint:
            self.search_recursive(self.dict, self.filename, "", self.print_path)
        if self.gt is not None:
            self.search_recursive(self.dict, self.filename, "", self.check_gt)
        if self.lt is not None:
            self.search_recursive(self.dict, self.filename, "", self.check_lt)
        if self.nans is not None:
            self.search_recursive(self.dict, self.filename, "", self.check_nans)
        if self.abss is not None:
            self.search_recursive(self.dict, self.filename, "", self.check_abs)

    def do_nanscheck(self, line):
        """
        Find all elements in the current subtree that contain NANs
        """
        self.search_recursive(self.current_element, self.pwd, "", self.check_nans)

    def do_gtcheck(self, line):
        """
        Find all elements in the current subtree that contain values greater than a given value
        Usage:

        gtcheck value

        where value is a numeric value
        """
        try:
            self.gt = float(line)
            self.search_recursive(self.current_element, self.pwd, "", self.check_gt)
        except:
            print >>self.stdout, "Error executing a command"
            self.do_help("gtcheck")

    def do_ltcheck(self, line):
        """
        Find all elements in the current subtree that contain values less than a given value
        Usage:

          ltcheck value

        where value is a numeric value
        """
        try:
            self.lt = float(line)
            self.search_recursive(self.current_element, self.pwd, "", self.check_lt)
        except:
            print >>self.stdout, "Error executing a command"
            self.do_help("ltcheck")

    def do_abscheck(self, line):
        """
        Find all elements in the current subtree that contain values greater in absolute value than a given value.
        Usage:

          abscheck value

        where value is a numeric value
        """
        try:
            self.abss = float(line)
            self.search_recursive(self.current_element, self.pwd, "", self.check_abs)
        except:
            print >>self.stdout, "Error executing a command"
            self.do_help("abscheck")

    def do_findid(self, line):
        """
        Find all the occurrences of an object with a given id.
        Usage:

          findid value

        where value is the numeric id
        """
        try:
            self.search_identity = int(line)
            self.search_recursive(self.dict, self.filename, "", self.check_id)
        except:
            print >>self.stdout, "Error executing a command"
            self.do_help("findid")

    def do_findrefs(self, line):
        """
        Finds all the references to a given object inside of the whole structure.
        Usage:

          findrefs object

        where object is in the current scope.
        """
        try:
            if line.isdigit():
                element = self.current_element[int(line)]
            else:
                element = self.current_element[line]
            self.search_identity = int(id(element))
            self.search_recursive(self.dict, self.filename, "", self.check_id)
        except:
            print >>self.stdout, "Error executing a command"
            self.do_help("findrefs")

    def do_pause(self, line):
        """
        Set the paused flag of a simulation (works when connected to a running simulation)
        """
        self.dict['paused'][0] = PVM_Create.PVM_PAUSE

    def do_step(self, line):
        """
        Stepwise execution. Be careful, works when connected to a runing simulation, otherwise will hang.
        """
        N = self.dict['N'][0]
        self.dict['paused'][0] = PVM_Create.PVM_RESUME
        while self.dict['N'][0] <= (N+1):
            pass
        self.dict['paused'][0] = PVM_Create.PVM_PAUSE

    def do_finish(self, line):
        """
        Set the paused flag of a simulation (works when connected to a running simulation)
        """
        self.dict['finished'][0] = PVM_Create.PVM_FLAG_VAL_TRIGGER
        return True

    def do_toggle_display(self, line):
        """
        Changes the logic for displaying a window (works when connected to a running simulation)
        """
        self.dict['finished'][0] = PVM_Create.PVM_FLAG_TRIGGER_DISPLAY

    def do_record(self, line):
        """
        Start recording to a file given after space, e.g.:

          record myfilename.avi

        """
        if line == "":
            print >>self.stdout, "No file name given"
        else:
            name = np.fromstring(line, dtype=np.uint8)
            self.dict["record_filename"][:] = 0
            self.dict["record_filename"][:name.shape[0]] = name
            self.dict['finished'][0] = PVM_Create.PVM_FLAG_TRIGGER_RECORD
            print >>self.stdout, "Started recording"

    def do_stop_record(self, line):
        """
        Stop recording to a file
        """
        self.dict['finished'][0] = PVM_Create.PVM_FLAG_TRIGGER_RECORD
        print >>self.stdout, "Stopped recording"

    def do_toggle_dream(self, line):
        """
        Sets the flags to inform the manager to run the dream mode in which predicted input is fed back as new input
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_DREAM
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_blindspot(self, line):
        """
        Sets the flags to inform the manager to show a gray spot in the center of the image
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_BLINDSPOT
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_gray(self, line):
        """
        Set neutral gray as input
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_GRAY
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_noise(self, line):
        """
        Set white noise as input
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_NOISE
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_noise_spot(self, line):
        """
        Set white noise spot in the center of the image
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_NOISE_SPOT
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_inverse_spot(self, line):
        """
        Set inverse blind spot (gray out periphery)
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_INV_BLINDSPOT
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_inverse_spot_noise(self, line):
        """
        Set inverse blind spot noise (noise out periphery)
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_INV_NOISE_SPOT
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_partial_dream(self, line):
        """
        Set dream mode in the periphery of the visual field
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_INV_DREAM_SPOT
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_dream_spot(self, line):
        """
        Set dream mode in the periphery of the visual field
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_DREAM_SPOT
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_deep_dream(self, line):
        """
        Make all the units in the hierachy dream
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_DEEP_DREAM
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_blinks(self, line):
        """
        Set dream mode in the periphery of the visual field
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_BLINKS
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_toggle_noisy_signal(self, line):
        """
        Set dream mode in the periphery of the visual field
        """
        if self.dict['flags'][0] == PVM_Create.PVM_FLAG_VAL_RESET:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_NOISY_SIGNAL
        else:
            self.dict['flags'][0] = PVM_Create.PVM_FLAG_VAL_RESET

    def do_disable_lateral(self, line):
        """
        Remove the lateral communication between the units
        """
        self.dict['context_factor_lateral'][0] = 0

    def do_disable_feedback(self, line):
        """
        Remove the feedback communication between the units
        """
        self.dict['context_factor_feedback'][0] = 0

    def do_enable_lateral(self, line):
        """
        Remove the lateral communication between the units
        """
        self.dict['context_factor_lateral'][0] = 1.0

    def do_enable_feedback(self, line):
        """
        Remove the feedback communication between the units
        """
        self.dict['context_factor_feedback'][0] = 1.0

    def do_freeze_learning(self, line):
        """
        Sets the flags to inform the manager to run the dream mode in which predicted input is fed back as new input
        """
        self.dict['flags'][PVM_Create.PVM_LEARNING_FLAG] = PVM_Create.PVM_LEARNING_FREEZE

    def do_unfreeze_learning(self, line):
        """
        Sets the flags to inform the manager to run the dream mode in which predicted input is fed back as new input
        """
        self.dict['flags'][PVM_Create.PVM_LEARNING_FLAG] = PVM_Create.PVM_LEARNING_UNFREEZE

    def do_resume(self, line):
        """
        Set the paused flag of a simulation (works when connected to a running simulation)
        """
        self.dict['paused'][0] = PVM_Create.PVM_RESUME

    def do_dump(self, line):
        """
        Dump the state of the current simulation to a given file. Warning, this method will attempt to pause
        a simulation and wait 1s, but is not guaranteed to save a consistent state. Use only in an emergency.
        """
        self.dict['paused'][0] = PVM_Create.PVM_PAUSE
        time.sleep(1)
        import PVM_framework.CoreUtils as CoreUtils
        CoreUtils.save_model(self.dict, line)
        self.dict['paused'][0] = PVM_Create.PVM_RESUME


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('input_file',
                        default='demo.p.gz',
                        nargs=1,
                        help='Input file dictionary')
    parser.add_argument("-p", "--pprint", help="Print the contents of the entire dictionary", action="store_true")
    parser.add_argument("-s", "--sprint", help="Print all the paths in the dictionary", action="store_true")
    parser.add_argument("-n", "--nans", help="Search for NANs", action="store_true")
    parser.add_argument("-u", "--upgrade", help="Upgrade to ver 1", action="store_true")
    parser.add_argument("-g", "--gt", type=str, help="Search for values greater than ")
    parser.add_argument("-l", "--lt", type=str, help="Search for values less than ")
    parser.add_argument("-a", "--abs", type=str, help="Search for values with absol;ute value greater than ")
    parser.add_argument("-f", "--filter", type=str, help="Display only filtered the elements")
    parser.add_argument("-i", "--interactive", help="Start a shell like command line interface to explore the dictionary", action="store_true")
    args = parser.parse_args()
    if not args.input_file:
        parser.print_help()
    else:
        app = InteractiveDictionaryExplorer(infilename=args.input_file[0],
                                            pprint=args.pprint,
                                            sprint=args.sprint,
                                            gt=args.gt,
                                            lt=args.lt,
                                            nans=args.nans,
                                            abss=args.abs,
                                            filter_name=args.filter,
                                            upgrade=args.upgrade)
        if args.interactive:
            app.cmdloop()
        else:
            app.run_noninteractive()


================================================
FILE: PVM_framework/PVM_display_helper.py
================================================
# ==================================================================================
# Copyright (c) 2016, Brain Corporation
#
# This software is released under Creative Commons
# Attribution-NonCommercial-ShareAlike 3.0 (BY-NC-SA) license.
# Full text available here in LICENSE.TXT file as well as:
# https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
#
# In summary - you are free to:
#
#    Share - copy and redistribute the material in any medium or format
#    Adapt - remix, transform, and build upon the material
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Under the following terms:
#    * Attribution - You must give appropriate credit, provide a link to the
#                    license, and indicate if changes were made. You may do so
#                    in any reasonable manner, but not in any way that suggests
#                    the licensor endorses you or your use.
#    * NonCommercial - You may not use the material for commercial purposes.
#    * ShareAlike - If you remix, transform, or build upon the material, you
#                   must distribute your contributions under the same license
#                   as the original.
#    * No additional restrictions - You may not apply legal terms or technological
#                                   measures that legally restrict others from
#                                   doing anything the license permits.
# ==================================================================================
import numpy as np
import cv2
import logging
import os


class VideoRecorder(object):
    def __init__(self, rec_filename):
        """
        :param rec_filename:
        :return:

        Handy object to carry out video recording
        """
        if rec_filename[-4:] == ".avi":
            rec_filename = rec_filename[:-4]
        rec_filename = rec_filename + "_%02d.avi"
        self.rec_filename = rec_filename
        self._video = None
        self.index = 0

    def set_filename(self, filename):
        self.override_name = filename

    def _get_filename(self):
        if self.override_name is None:
            return self.rec_filename % self.index
        else:
            return self.override_name

    def record(self, image):
        """
        :param image:
        :return:

        Takes and image and records it into a file
        """
        if self._video is None:
                self._video = cv2.VideoWriter()
                fps = 20
                retval = self._video.open(os.path.expanduser(self._get_filename()),
                                          cv2.cv.CV_FOURCC('M', 'J', 'P', 'G'),
                                          fps, (image.shape[1], image.shape[0]))
                assert(retval)
                logging.info("Creating an avi file %s" % os.path.expanduser(self._get_filename()))
        self._video.write(image)

    def finish(self):
        """
        When done releases the cv video writer
        :return:
        """
        if self._video is not None:
            self._video.release()
            self._video = None
            self.index += 1
            logging.info("Finished recording file %s" % os.path.expanduser(self._get_filename()))


class DisplayHelperObject(object):
    """
    An object that allows to easily assemble display frame
    """
    def __init__(self, width=1920, height=1080, margin=8, grid=(4, 5)):
        self.frame = np.zeros((height, width, 3), dtype=np.uint8)
        self.margin = margin
        self.im_size = min((width-2*self.margin)/grid[0], (height-2*self.margin)/grid[1])-2*self.margin
        self.grid = grid
        self.clear_frame()
        self.logo = cv2.imread(os.path.join(os.path.dirname(os.path.abspath(__file__)), "bc_logo_gray_sm.png"))
        self.no_refresh = {}

    def grid_to_pix(self, x, y):
        if x < 0 or x > self.grid[0] or y < 0 or y > self.grid[1]:
            raise Exception("Out of grid!")
        px = x*(self.im_size+2*self.margin)+2*self.margin
        py = y*(self.im_size+2*self.margin)+2*self.margin
        return py, px
    
    def place_gray_image(self, grid_x, grid_y, image, text=""):
        (px, py) = self.grid_to_pix(grid_x, grid_y)
        self.place_label(grid_x=grid_x, grid_y=grid_y, text=text)
        resized = cv2.resize(image, dsize=(self.im_size, self.im_size), interpolation=cv2.INTER_NEAREST)
        self.frame[px:px+self.im_size, py:py+self.im_size, 0] = resized
        self.frame[px:px+self.im_size, py:py+self.im_size, 1] = resized
        self.frame[px:px+self.im_size, py:py+self.im_size, 2] = resized
        cv2.rectangle(self.frame, (py-2, px-2), (py+self.im_size+1, px+self.im_size+1), color=(140, 100, 100))

    def place_gray_float_image(self, grid_x, grid_y, image, text=""):
        (px, py) = self.grid_to_pix(grid_x, grid_y)
        self.place_label(grid_x=grid_x, grid_y=grid_y, text=text)
        resized = cv2.resize(image, dsize=(self.im_size, self.im_size), interpolation=cv2.INTER_NEAREST)
        resized *= 255
        resized = resized.astype(np.uint8)
        self.frame[px:px+self.im_size, py:py+self.im_size, 0] = resized
        self.frame[px:px+self.im_size, py:py+self.im_size, 1] = resized
        self.frame[px:px+self.im_size, py:py+self.im_size, 2] = resized
        cv2.rectangle(self.frame, (py-2, px-2), (py+self.im_size+1, px+self.im_size+1), color=(140, 100, 100))

    def place_color_image(self, grid_x, grid_y, image, text=""):
        (px, py) = self.grid_to_pix(grid_x, grid_y)
        self.place_label(grid_x=grid_x, grid_y=grid_y, text=text)
        cv2.rectangle(self.frame, (py-2, px-2), (py+self.im_size+1, px+self.im_size+1), color=(140, 100, 100))
        self.frame[px:px+self.im_size, py:py+self.im_size, :] = cv2.resize(image, dsize=(self.im_size, self.im_size), interpolation=cv2.INTER_NEAREST)

    def place_color_float_image(self, grid_x, grid_y, image, text=""):
        (px, py) = self.grid_to_pix(grid_x, grid_y)
        self.place_label(grid_x=grid_x, grid_y=grid_y, text=text)
        cv2.rectangle(self.frame, (py-2, px-2), (py+self.im_size+1, px+self.im_size+1), color=(140, 100, 100))
        resized = cv2.resize(image, dsize=(self.im_size, self.im_size), interpolation=cv2.INTER_NEAREST)
        resized *= 255
        resized = resized.astype(np.uint8)
        self.frame[px:px+self.im_size, py:py+self.im_size, :] = resized

    def place_image(self, grid_x, grid_y, image, text=""):
        if image.dtype == np.float:
            if len(image.shape) == 3 and image.shape[2] == 3:
                self.place_color_float_image(grid_x, grid_y, image, text)
            else:
                self.place_gray_float_image(grid_x, grid_y, image, text)
        else:
            if len(image.shape) == 3 and image.shape[2] == 3:
                self.place_color_image(grid_x, grid_y, image, text)
            else:
                self.place_gray_image(grid_x, grid_y, image, text)

    def place_color_logo(self, grid_x, grid_y, image=None, text=""):
        if image is None:
            image = self.logo
        (px, py) = self.grid_to_pix(grid_x, grid_y)
        self.frame[px:px+image.shape[0], py:py+image.shape[1], :] = image

    def place_label(self,
Download .txt
gitextract__0_tzylg/

├── .gitmodules
├── LICENSE.TXT
├── PVM_framework/
│   ├── AbstractExecutionManager.py
│   ├── AbstractExecutionUnit.py
│   ├── Accelerated.cpp
│   ├── Accelerated.h
│   ├── CoreUtils.py
│   ├── LowLevelCPUControlx86.pyx
│   ├── MLP.py
│   ├── PVM_Create.py
│   ├── PVM_SignalProvider.py
│   ├── PVM_Storage.py
│   ├── PVM_datasets.py
│   ├── PVM_debug_console.py
│   ├── PVM_display_helper.py
│   ├── PVM_options.py
│   ├── PVM_tracker.json
│   ├── PVM_upgrade_dict.py
│   ├── SharedArray.py
│   ├── Sync.cpp
│   ├── Sync.h
│   ├── SyncUtils.pyx
│   ├── SyncUtils_python.py
│   ├── __init__.py
│   ├── debug_logger.py
│   └── fast_routines.pyx
├── PVM_models/
│   ├── PVM_Manager.py
│   ├── PVM_plot_error.py
│   ├── PVM_run.py
│   ├── PVM_tracker.py
│   ├── PVM_unit_2step_residual_v1.py
│   ├── PVM_unit_test.py
│   ├── PVM_unit_v1.py
│   ├── __init__.py
│   ├── demo00_run.py
│   ├── demo00_unit.py
│   ├── demo01_run.py
│   ├── demo01_unit.py
│   ├── demo02_run.py
│   ├── demo02_unit.py
│   ├── demo03_run.py
│   ├── demo03_unit.py
│   ├── demo04_run.py
│   ├── demo04_unit.py
│   ├── model_zoo/
│   │   ├── classic_medium.json
│   │   ├── experiment1_face.json
│   │   ├── experiment1_green_ball.json
│   │   ├── experiment1_stop_sign.json
│   │   ├── experiment2.json
│   │   └── very_deep.json
│   ├── process_dream_data.py
│   └── run_tracking_benchmark.py
├── PVM_tests/
│   ├── test_MLP.py
│   ├── test_SharedArray.py
│   ├── test_SyncUtils.py
│   ├── test_bounding_region.py
│   ├── test_create.py
│   ├── test_fast_routines.py
│   └── test_labeled_movie.py
├── PVM_tools/
│   ├── __init__.py
│   ├── abstract_bounding_boxer.py
│   ├── abstract_tracker.py
│   ├── benchmark.py
│   ├── bounding_region.py
│   └── labeled_movie.py
├── README.md
├── add_thirdparty_tracker_submodules.sh
├── docs/
│   ├── Makefile
│   ├── conf.py
│   ├── generate.sh
│   ├── index.rst
│   └── make.bat
├── download_data.sh
├── install_local.sh
├── install_ubuntu_dependencies.sh
├── other_trackers/
│   ├── ASMSearcher.hpp
│   ├── __init__.py
│   ├── backprojection.py
│   ├── bounding_boxer.py
│   ├── center_vision_tracker.py
│   ├── cmt_vision_tracker.py
│   ├── color_vision_tracker.py
│   ├── null_vision_tracker.py
│   ├── opentld_python.cpp
│   ├── setup_opentld.py
│   ├── setup_struck.py
│   ├── struck.cpp
│   ├── struck.h
│   ├── struck_bindings.pyx
│   ├── struck_tracker.py
│   ├── test_struck_bindings.py
│   ├── test_tld_basic.py
│   └── tld_vision_tracker.py
├── pytest.ini
├── requirements.txt
├── setup.py
├── test
└── tracker_tools/
    ├── __init__.py
    ├── export_to_zip.py
    ├── images_to_PVM_pickle.py
    ├── label_PVM_pickle.py
    ├── movie_to_PVM_pickle.py
    ├── play_PVM_pickle.py
    ├── raw_to_PVM_pickle.py
    ├── scale_PVM_pickle.py
    └── upgrade_cloud_lib.py
Download .txt
SYMBOL INDEX (709 symbols across 66 files)

FILE: PVM_framework/AbstractExecutionManager.py
  class ExecutionManager (line 33) | class ExecutionManager(object):
    method __init__ (line 37) | def __init__(self):
    method start (line 41) | def start(self):
    method fast_action (line 48) | def fast_action(self):
    method slow_action (line 56) | def slow_action(self):
    method running (line 64) | def running(self):
    method finish (line 70) | def finish(self):
  class AbstractSignalProvider (line 76) | class AbstractSignalProvider(object):
    method __init__ (line 80) | def __init__(self):
    method start (line 84) | def start(self):
    method get_signal (line 91) | def get_signal(self, name, time):
    method advance (line 98) | def advance(self):
    method finish (line 105) | def finish(self):

FILE: PVM_framework/AbstractExecutionUnit.py
  class ExecutionUnit (line 33) | class ExecutionUnit(object):
    method execution_steps (line 37) | def execution_steps(cls):
    method __init__ (line 45) | def __init__(self, parameters):
    method generate_missing_parameters (line 49) | def generate_missing_parameters(parameters, options):
    method execute0 (line 58) | def execute0(self):
    method cleanup (line 67) | def cleanup(self):

FILE: PVM_framework/Accelerated.cpp
  function dot_transpose (line 36) | void dot_transpose(double* mult, double* vector, int vect_shape_0, doubl...
  function dot_transpose_simple (line 78) | void dot_transpose_simple(double* vector, int vect_shape_0, double* matr...
  function derivative_dot (line 120) | void derivative_dot(double* vector, double* vector2, int vect_shape_0, d...
  function derivative_dot_poly (line 130) | void derivative_dot_poly(double* vector, double* vector2, int vect_shape...
  function generalized_outer (line 147) | void generalized_outer(double alpha, double * vector1, int vect1_shape, ...
  function dot_sigmoid (line 175) | void dot_sigmoid(double* vector, double* matrix, int mat_shape0, int mat...
  function dot_sigmoid_poly (line 229) | void dot_sigmoid_poly(double* vector, double* matrix, int mat_shape0, in...
  function dot_add (line 283) | void dot_add(double* vector, double* matrix, int mat_shape0, int mat_sha...
  function sigmoid_poly (line 336) | void sigmoid_poly(double* result, int shape, double beta)
  function sigmoid (line 345) | void sigmoid(double* result, int shape, double beta)

FILE: PVM_framework/CoreUtils.py
  function mapname (line 62) | def mapname(name):
  function mapped_load_global (line 68) | def mapped_load_global(self):
  function load_legacy_pickle (line 75) | def load_legacy_pickle(str):
  function load_legacy_pickle_file (line 82) | def load_legacy_pickle_file(file):
  function save_model (line 88) | def save_model(pobject, filename, protocol=-1):
  function load_model (line 108) | def load_model(filename):
  function _worker_code (line 127) | def _worker_code(prop_dict, proc_id, barrier):
  function _supervisor_run (line 211) | def _supervisor_run(prop_dict, control_barrier):
  function _run_debug_session (line 280) | def _run_debug_session(socket, prop_dict):
  function _monitor_debug_session (line 301) | def _monitor_debug_session(port, stop):
  function _run_debug_server (line 323) | def _run_debug_server(prop_dict, port, stop):
  class ModelExecution (line 359) | class ModelExecution(object):
    method __init__ (line 373) | def __init__(self, prop_dict, manager, port=9000):
    method start (line 378) | def start(self, blocking=True):
    method step (line 424) | def step(self):
    method finish (line 437) | def finish(self):
  function run_model (line 452) | def run_model(prop_dict, manager, port=9000):

FILE: PVM_framework/MLP.py
  function test (line 38) | def test():
  function profile (line 41) | def profile(fun):
  function random_ortho (line 45) | def random_ortho(n):
  function random_with_singular_values (line 57) | def random_with_singular_values(m, n, singular_values):
  function optimal_weight_initialization (line 81) | def optimal_weight_initialization(m, n, singular_value=1.1):
  class MLPPrototype (line 86) | class MLPPrototype(object):
    method __init__ (line 91) | def __init__(self, state):
    method train (line 95) | def train(self, inputs, targets):
    method evaluate (line 106) | def evaluate(self, inputs):
    method copy_weights (line 111) | def copy_weights(self):
    method get_hidden (line 117) | def get_hidden(self, layer):
    method get_deltas (line 123) | def get_deltas(self, layer):
    method copy_state (line 130) | def copy_state(self):
  function view_as_ndarray (line 138) | def view_as_ndarray(object):
  function get_layers (line 168) | def get_layers(dims=None):
  function get_weights (line 185) | def get_weights(layers=None):
  function initialize_weights (line 201) | def initialize_weights(w, method=0):
  class MLP (line 226) | class MLP(MLPPrototype):
    method copy_weights (line 301) | def copy_weights(self):
    method get_hidden (line 304) | def get_hidden(self, layer):
    method __init__ (line 307) | def __init__(self, state):
    method train (line 339) | def train(self, inputs, targets, eta=None):
    method evaluate (line 367) | def evaluate(self, inputs):
    method train2 (line 380) | def train2(self, error, eta=None):
    method mlp_forward (line 404) | def mlp_forward(self, inputs=None):
    method mlp_forward_layer (line 416) | def mlp_forward_layer(self, layer):
    method mlp_backpropagate_layer (line 433) | def mlp_backpropagate_layer(self, layer):
    method mlp_backpropagate (line 452) | def mlp_backpropagate(self):
    method calculate_weight_update_layer (line 461) | def calculate_weight_update_layer(self, layer, eta=None):
    method calculate_weight_update (line 474) | def calculate_weight_update(self, eta=None):
    method update_weights_layer (line 483) | def update_weights_layer(self, layer):
    method update_weights (line 495) | def update_weights(self):
    method get_activation (line 504) | def get_activation(self, layer=0):
    method get_deltas (line 515) | def get_deltas(self, layer=0):
    method copy_state (line 526) | def copy_state(self):

FILE: PVM_framework/PVM_Create.py
  function create_blank_dictionary (line 74) | def create_blank_dictionary(name="default", description="default", save_...
  function get_surround (line 120) | def get_surround(xy, dim_x=10, dim_y=10, radius=1, exclude_self=True):
  function get_fan_in (line 158) | def get_fan_in(xy=(0, 0), dim_x_l=10, dim_y_l=10, dim_x_u=9, dim_y_u=9, ...
  function connect_forward_and_back (line 222) | def connect_forward_and_back(simulation_dict, (index0, blocks_per_dim0, ...
  function connect_forward_and_back_v1 (line 279) | def connect_forward_and_back_v1(simulation_dict, (index0, blocks_per_dim...
  function connect_back (line 333) | def connect_back(simulation_dict, (index_from, blocks_per_dim_from), (in...
  function gather_surround (line 368) | def gather_surround(simulation_dict, (index0, blocks_per_dim0), radius, ...
  function create_basic_unit_v1 (line 381) | def create_basic_unit_v1(learning_rate, momentum, tau, readout_learning_...
  function generate_dict_options (line 395) | def generate_dict_options(name, description, options):
  function generate_v1 (line 404) | def generate_v1(name, description, options):
  function upgrade_readout (line 580) | def upgrade_readout(simulation_dict):
  function upgrade (line 613) | def upgrade(simulation_dict):
  function apply_options (line 627) | def apply_options(simulation_dict, options):
  function upgrade_dictionary_to_ver1_0 (line 639) | def upgrade_dictionary_to_ver1_0(simulation_dict):

FILE: PVM_framework/PVM_SignalProvider.py
  class SimpleSignalProvider (line 37) | class SimpleSignalProvider(AbstractExecutionManager.AbstractSignalProvid...
    method __init__ (line 38) | def __init__(self, frame_resolution, heatmap_resolution, files, storag...
    method start (line 59) | def start(self):
    method get_signal (line 86) | def get_signal(self, name, time):
    method get_length (line 102) | def get_length(self):
    method advance (line 105) | def advance(self):
    method finish (line 115) | def finish(self):
    method reset (line 122) | def reset(self):
    method get_index (line 125) | def get_index(self):
  class StereoSignalProvider (line 133) | class StereoSignalProvider(AbstractExecutionManager.AbstractSignalProvid...
    method __init__ (line 134) | def __init__(self, frame_resolution, heatmap_resolution, files, storag...
    method start (line 158) | def start(self):
    method interlace_two_images (line 192) | def interlace_two_images(self, img1, img2):
    method get_signal (line 199) | def get_signal(self, name, time):
    method get_length (line 215) | def get_length(self):
    method advance (line 218) | def advance(self):
    method finish (line 225) | def finish(self):
    method reset (line 232) | def reset(self):
    method get_index (line 235) | def get_index(self):
  class TripleSignalProvider (line 243) | class TripleSignalProvider(AbstractExecutionManager.AbstractSignalProvid...
    method __init__ (line 244) | def __init__(self, frame_resolution, heatmap_resolution, files1, files...
    method buffer_files (line 267) | def buffer_files(self, idx, channel, files):
    method start (line 289) | def start(self):
    method get_signal (line 298) | def get_signal(self, name, time):
    method get_length (line 314) | def get_length(self):
    method advance (line 317) | def advance(self):
    method finish (line 324) | def finish(self):
    method reset (line 331) | def reset(self):
    method get_index (line 334) | def get_index(self):

FILE: PVM_framework/PVM_Storage.py
  function get_S3_credentials (line 42) | def get_S3_credentials():
  class Storage (line 64) | class Storage(object):
    method __init__ (line 68) | def __init__(self, local_storage_folder="~/PVM_data", S3_bucket="techn...
    method is_valid_key (line 98) | def is_valid_key(self, key):
    method exists (line 101) | def exists(self, key):
    method _ensure_containing_folder_exists (line 115) | def _ensure_containing_folder_exists(self, filepath):
    method get (line 127) | def get(self, path, to_path=None, force=False):
    method put (line 183) | def put(self, path=None, from_path=None, to_folder=None, overwrite=Fal...
    method download (line 235) | def download(self, to_file, key, progress_callback=None):
    method upload (line 257) | def upload(self, from_file, to_folder, overwrite=False, progress_callb...
    method _progress_download (line 283) | def _progress_download(self, elapsed, total):
    method _progress_upload (line 293) | def _progress_upload(self, elapsed, total):
    method list_path (line 303) | def list_path(self, path):
    method remove (line 325) | def remove(self, key, check_existence=True):
  function test_storage_local (line 345) | def test_storage_local():
  function test_storage_remote (line 359) | def test_storage_remote():

FILE: PVM_framework/PVM_datasets.py
  class PVMDataset (line 213) | class PVMDataset(object):
    method __init__ (line 214) | def __init__(self, name, storage=None):

FILE: PVM_framework/PVM_debug_console.py
  class InteractiveDictionaryExplorer (line 43) | class InteractiveDictionaryExplorer(cmd.Cmd):
    method __init__ (line 46) | def __init__(self,
    method getsizeof (line 127) | def getsizeof(self, element):
    method getshapeof (line 135) | def getshapeof(self, element):
    method gettypename (line 143) | def gettypename(self, element):
    method emptyline (line 151) | def emptyline(self):
    method do_ls (line 154) | def do_ls(self, line, quiet=False):
    method do_cd (line 198) | def do_cd(self, path):
    method getsubelement (line 228) | def getsubelement(self, element, path):
    method getsubelements (line 242) | def getsubelements(self, element):
    method generic_complete (line 249) | def generic_complete(self, text, line, beginidx, endidx):
    method complete_cd (line 265) | def complete_cd(self, text, line, beginidx, endidx):
    method complete_ls (line 268) | def complete_ls(self, text, line, beginidx, endidx):
    method do_cat (line 271) | def do_cat(self, line):
    method do_debug_create (line 277) | def do_debug_create(self, line):
    method do_debug_disable (line 326) | def do_debug_disable(self, line):
    method do_debug_enable (line 341) | def do_debug_enable(self, line):
    method do_debug_delete (line 356) | def do_debug_delete(self, line):
    method do_debug_list_enabled (line 373) | def do_debug_list_enabled(self, line):
    method do_debug_list_disabled (line 379) | def do_debug_list_disabled(self, line):
    method do_debug_list (line 385) | def do_debug_list(self, line):
    method do_quit (line 394) | def do_quit(self, line):
    method list_to_dict (line 400) | def list_to_dict(self, something):
    method do_python (line 409) | def do_python(self, line):
    method help_quit (line 428) | def help_quit(self):
    method search_recursive (line 434) | def search_recursive(self, dict_object, path, name, leaf_method):
    method print_path (line 451) | def print_path(self, path, name, element):
    method check_gt (line 454) | def check_gt(self, path, name, element):
    method check_abs (line 459) | def check_abs(self, path, name, element):
    method check_lt (line 464) | def check_lt(self, path, name, element):
    method check_nans (line 469) | def check_nans(self, path, name, element):
    method check_id (line 474) | def check_id(self, path, name, element):
    method run_noninteractive (line 478) | def run_noninteractive(self):
    method do_nanscheck (line 492) | def do_nanscheck(self, line):
    method do_gtcheck (line 498) | def do_gtcheck(self, line):
    method do_ltcheck (line 514) | def do_ltcheck(self, line):
    method do_abscheck (line 530) | def do_abscheck(self, line):
    method do_findid (line 546) | def do_findid(self, line):
    method do_findrefs (line 562) | def do_findrefs(self, line):
    method do_pause (line 582) | def do_pause(self, line):
    method do_step (line 588) | def do_step(self, line):
    method do_finish (line 598) | def do_finish(self, line):
    method do_toggle_display (line 605) | def do_toggle_display(self, line):
    method do_record (line 611) | def do_record(self, line):
    method do_stop_record (line 627) | def do_stop_record(self, line):
    method do_toggle_dream (line 634) | def do_toggle_dream(self, line):
    method do_toggle_blindspot (line 643) | def do_toggle_blindspot(self, line):
    method do_toggle_gray (line 652) | def do_toggle_gray(self, line):
    method do_toggle_noise (line 661) | def do_toggle_noise(self, line):
    method do_toggle_noise_spot (line 670) | def do_toggle_noise_spot(self, line):
    method do_toggle_inverse_spot (line 679) | def do_toggle_inverse_spot(self, line):
    method do_toggle_inverse_spot_noise (line 688) | def do_toggle_inverse_spot_noise(self, line):
    method do_toggle_partial_dream (line 697) | def do_toggle_partial_dream(self, line):
    method do_toggle_dream_spot (line 706) | def do_toggle_dream_spot(self, line):
    method do_toggle_deep_dream (line 715) | def do_toggle_deep_dream(self, line):
    method do_toggle_blinks (line 724) | def do_toggle_blinks(self, line):
    method do_toggle_noisy_signal (line 733) | def do_toggle_noisy_signal(self, line):
    method do_disable_lateral (line 742) | def do_disable_lateral(self, line):
    method do_disable_feedback (line 748) | def do_disable_feedback(self, line):
    method do_enable_lateral (line 754) | def do_enable_lateral(self, line):
    method do_enable_feedback (line 760) | def do_enable_feedback(self, line):
    method do_freeze_learning (line 766) | def do_freeze_learning(self, line):
    method do_unfreeze_learning (line 772) | def do_unfreeze_learning(self, line):
    method do_resume (line 778) | def do_resume(self, line):
    method do_dump (line 784) | def do_dump(self, line):

FILE: PVM_framework/PVM_display_helper.py
  class VideoRecorder (line 35) | class VideoRecorder(object):
    method __init__ (line 36) | def __init__(self, rec_filename):
    method set_filename (line 50) | def set_filename(self, filename):
    method _get_filename (line 53) | def _get_filename(self):
    method record (line 59) | def record(self, image):
    method finish (line 76) | def finish(self):
  class DisplayHelperObject (line 88) | class DisplayHelperObject(object):
    method __init__ (line 92) | def __init__(self, width=1920, height=1080, margin=8, grid=(4, 5)):
    method grid_to_pix (line 101) | def grid_to_pix(self, x, y):
    method place_gray_image (line 108) | def place_gray_image(self, grid_x, grid_y, image, text=""):
    method place_gray_float_image (line 117) | def place_gray_float_image(self, grid_x, grid_y, image, text=""):
    method place_color_image (line 128) | def place_color_image(self, grid_x, grid_y, image, text=""):
    method place_color_float_image (line 134) | def place_color_float_image(self, grid_x, grid_y, image, text=""):
    method place_image (line 143) | def place_image(self, grid_x, grid_y, image, text=""):
    method place_color_logo (line 155) | def place_color_logo(self, grid_x, grid_y, image=None, text=""):
    method place_label (line 161) | def place_label(self, grid_x, grid_y, text=""):
    method place_text (line 167) | def place_text(self, grid_x, grid_y, voffset=0, text=""):
    method clear_cell (line 171) | def clear_cell(self, grid_x, grid_y):
    method clear_frame (line 175) | def clear_frame(self):

FILE: PVM_framework/PVM_options.py
  function get_option_help (line 188) | def get_option_help():
  function parse_options (line 205) | def parse_options(options_given, options_in_the_dict=None):

FILE: PVM_framework/SharedArray.py
  class ShmemBufferWrapper (line 43) | class ShmemBufferWrapper(object):
    method __init__ (line 47) | def __init__(self, tag, size, create=True):
    method get_address (line 62) | def get_address(self):
    method __del__ (line 67) | def __del__(self):
  function ShmemRawArray (line 74) | def ShmemRawArray(type_, size_or_initializer, tag, create=True):
  function np_type_id_to_ctypes (line 101) | def np_type_id_to_ctypes(dtype):
  class SharedNumpyArray (line 163) | class SharedNumpyArray:  # DO NOT EXTEND FROM OBJECT!
    method __init__ (line 175) | def __init__(self, shape, dtype, tag=None, create=True):
    method __getattr__ (line 184) | def __getattr__(self, name):
    method __getstate__ (line 192) | def __getstate__(self):
    method __setstate__ (line 202) | def __setstate__(self, state):
    method copyto (line 214) | def copyto(self, nparray):
  class DynamicView (line 221) | class DynamicView:
    method __init__ (line 228) | def __init__(self, array):
    method __getitem__ (line 234) | def __getitem__(self, item):
    method __getattr__ (line 240) | def __getattr__(self, name):
    method __getstate__ (line 248) | def __getstate__(self):
    method __setstate__ (line 256) | def __setstate__(self, state):
  function SharedNumpyArray_like (line 286) | def SharedNumpyArray_like(array):
  class DoubleBufferedSharedNumpyArray (line 295) | class DoubleBufferedSharedNumpyArray:  # DO NOT EXTEND FROM OBJECT!
    method __init__ (line 303) | def __init__(self, shape, dtype, parity_obj):
    method __getattr__ (line 312) | def __getattr__(self, name):
    method __getstate__ (line 318) | def __getstate__(self):
    method __setstate__ (line 327) | def __setstate__(self, state):
    method copyto (line 342) | def copyto(self, nparray):
    method copytobuffer (line 351) | def copytobuffer(self, nparray):

FILE: PVM_framework/Sync.cpp
  function atomic_get_and_set (line 34) | long atomic_get_and_set(long n, long *addr) {
  function acquire (line 38) | void acquire(long *addr) {
  function release (line 42) | void release(long *addr) {

FILE: PVM_framework/SyncUtils_python.py
  class Barrier (line 39) | class Barrier():
    method __init__ (line 45) | def __init__(self, num_proc, timeout = 0, UseSpinLock=True):
    method __inc (line 64) | def __inc(self, val, lock):
    method __check_and_reset (line 69) | def __check_and_reset(self, val, lock, check_val):
    method worker_barrier (line 77) | def worker_barrier(self):
    method parent_barrier (line 105) | def parent_barrier(self, quit_workers=False):
    method resume_workers (line 117) | def resume_workers(self, quit_workers=False):
    method quit_workers (line 127) | def quit_workers(self):
    method workers_running (line 130) | def workers_running(self):

FILE: PVM_framework/debug_logger.py
  class DebugLogger (line 34) | class DebugLogger(object):
    method __init__ (line 45) | def __init__(self, dict, buffer_len=100):
    method process_hookups (line 59) | def process_hookups(self):
    method flush (line 84) | def flush(self, hookhash):
    method flushall (line 94) | def flushall(self):
    method __del__ (line 102) | def __del__(self):

FILE: PVM_models/PVM_Manager.py
  function get_square_res (line 41) | def get_square_res(prop_dict):
  class Manager (line 57) | class Manager(AbstractExecutionManager.ExecutionManager):
    method __init__ (line 63) | def __init__(self,
    method _get_readout_targets (line 122) | def _get_readout_targets(self, channel_name="mask"):
    method start (line 136) | def start(self):
    method fast_action (line 152) | def fast_action(self):
    method calculate_errors (line 177) | def calculate_errors(self):
    method take_snapshot_and_backup (line 210) | def take_snapshot_and_backup(self):
    method construct_display (line 229) | def construct_display(self):
    method developmental_check (line 305) | def developmental_check(self):
    method slow_action (line 319) | def slow_action(self):
    method process_flags (line 360) | def process_flags(self):
    method process_dream_experiment (line 449) | def process_dream_experiment(self):
    method running (line 480) | def running(self):
    method finish (line 486) | def finish(self):
    method freeze_learning (line 506) | def freeze_learning(self):
    method un_freeze_learning (line 509) | def un_freeze_learning(self):
    method freeze_learning_synchronised (line 512) | def freeze_learning_synchronised(self):
    method un_freeze_learning_synchronised (line 529) | def un_freeze_learning_synchronised(self):

FILE: PVM_models/PVM_plot_error.py
  function runningMeanFast (line 36) | def runningMeanFast(x, N):
  function plot_weight_dists (line 40) | def plot_weight_dists(simulation_dict):
  function plot_model (line 95) | def plot_model(filename, remote, compare, display):

FILE: PVM_models/PVM_run.py
  function cleanup (line 48) | def cleanup(s):
  function run_model (line 56) | def run_model(evaluate=False,

FILE: PVM_models/PVM_tracker.py
  class Manager (line 41) | class Manager(AbstractExecutionManager.ExecutionManager):
    method __init__ (line 42) | def __init__(self, prop_dict, steps_to_run):
    method start (line 47) | def start(self):
    method fast_action (line 53) | def fast_action(self):
    method slow_action (line 60) | def slow_action(self):
    method running (line 67) | def running(self):
    method finish (line 73) | def finish(self):
  class PVMVisionTracker (line 77) | class PVMVisionTracker(GenericVisionTracker):
    method __init__ (line 83) | def __init__(self, filename="", remote_filename="", cores="4", storage...
    method reset (line 107) | def reset(self):
    method _prime (line 115) | def _prime(self, im, bounding_region):
    method _track (line 129) | def _track(self, im):
    method get_heatmap (line 186) | def get_heatmap(self, heatmap_name=None):
    method finish (line 189) | def finish(self):

FILE: PVM_models/PVM_unit_2step_residual_v1.py
  class ExecutionUnit (line 37) | class ExecutionUnit(AbstractExecutionUnit.ExecutionUnit):
    method execution_steps (line 73) | def execution_steps(cls):
    method open_views_into_shmem (line 80) | def open_views_into_shmem(self, shared_memory, key, num_items):
    method __init__ (line 83) | def __init__(self, parameters):
    method upgrade_to_ver_1 (line 138) | def upgrade_to_ver_1(parameters):
    method generate_missing_parameters (line 149) | def generate_missing_parameters(parameters, options):
    method min_max_normalize (line 225) | def min_max_normalize(self, a, min_a, max_a):
    method execute0 (line 238) | def execute0(self):
    method execute1 (line 279) | def execute1(self):
    method execute2 (line 321) | def execute2(self):
    method cleanup (line 336) | def cleanup(self):
    method push_activation (line 344) | def push_activation(self):
    method pop_activation (line 353) | def pop_activation(self):

FILE: PVM_models/PVM_unit_test.py
  class ExecutionUnit (line 35) | class ExecutionUnit(AbstractExecutionUnit.ExecutionUnit):
    method execution_steps (line 65) | def execution_steps(cls):
    method open_views_into_shmem (line 72) | def open_views_into_shmem(self, shared_memory, key, num_items):
    method __init__ (line 75) | def __init__(self, parameters):
    method generate_missing_parameters (line 125) | def generate_missing_parameters(parameters, complex_unit=False, comple...
    method execute0 (line 242) | def execute0(self):
    method execute1 (line 272) | def execute1(self):
    method execute2 (line 328) | def execute2(self):
    method cleanup (line 343) | def cleanup(self):

FILE: PVM_models/PVM_unit_v1.py
  class ExecutionUnit (line 38) | class ExecutionUnit(AbstractExecutionUnit.ExecutionUnit):
    method execution_steps (line 78) | def execution_steps(cls):
    method open_views_into_shmem (line 85) | def open_views_into_shmem(self, shared_memory, key, num_items):
    method open_views_into_shmem_single (line 88) | def open_views_into_shmem_single(self, shared_memory, key):
    method __init__ (line 91) | def __init__(self, parameters):
    method upgrade_to_ver_1 (line 202) | def upgrade_to_ver_1(parameters):
    method generate_missing_parameters (line 263) | def generate_missing_parameters(parameters, options):
    method min_max_normalize (line 370) | def min_max_normalize(self, a, min_a, max_a):
    method execute0 (line 383) | def execute0(self):
    method execute1 (line 428) | def execute1(self):
    method execute2 (line 532) | def execute2(self):
    method cleanup (line 559) | def cleanup(self):
    method push_activation (line 567) | def push_activation(self):
    method pop_activation (line 575) | def pop_activation(self):
    method get_one_step_behind_activation (line 586) | def get_one_step_behind_activation(self):

FILE: PVM_models/demo00_run.py
  class Manager (line 43) | class Manager(AbstractExecutionManager.ExecutionManager):
    method __init__ (line 44) | def __init__(self, prop_dict, steps_to_run):
    method start (line 49) | def start(self):
    method fast_action (line 57) | def fast_action(self):
    method slow_action (line 64) | def slow_action(self):
    method running (line 78) | def running(self):
    method finish (line 84) | def finish(self):
  function generate_dict (line 88) | def generate_dict():
  function run_demo (line 122) | def run_demo():

FILE: PVM_models/demo00_unit.py
  class ExecutionUnit (line 35) | class ExecutionUnit(AbstractExecutionUnit.ExecutionUnit):
    method execution_steps (line 38) | def execution_steps(cls):
    method __init__ (line 44) | def __init__(self, parameters):
    method generate_missing_parameters (line 49) | def generate_missing_parameters(parameters):
    method execute0 (line 57) | def execute0(self):
    method cleanup (line 64) | def cleanup(self):

FILE: PVM_models/demo01_run.py
  class Manager (line 43) | class Manager(AbstractExecutionManager.ExecutionManager):
    method __init__ (line 44) | def __init__(self, prop_dict, steps_to_run):
    method start (line 49) | def start(self):
    method fast_action (line 56) | def fast_action(self):
    method slow_action (line 63) | def slow_action(self):
    method running (line 78) | def running(self):
    method finish (line 84) | def finish(self):
  function generate_dict (line 88) | def generate_dict():
  function run_demo (line 127) | def run_demo():

FILE: PVM_models/demo01_unit.py
  class ExecutionUnit (line 35) | class ExecutionUnit(AbstractExecutionUnit.ExecutionUnit):
    method execution_steps (line 38) | def execution_steps(cls):
    method __init__ (line 45) | def __init__(self, parameters):
    method generate_missing_parameters (line 51) | def generate_missing_parameters(parameters):
    method execute0 (line 58) | def execute0(self):
    method cleanup (line 65) | def cleanup(self):

FILE: PVM_models/demo02_run.py
  class Manager (line 44) | class Manager(AbstractExecutionManager.ExecutionManager):
    method __init__ (line 45) | def __init__(self, prop_dict, steps_to_run, cam):
    method start (line 52) | def start(self):
    method fast_action (line 64) | def fast_action(self):
    method slow_action (line 75) | def slow_action(self):
    method running (line 96) | def running(self):
    method finish (line 102) | def finish(self):
  function generate_dict (line 106) | def generate_dict():
  function run_demo (line 159) | def run_demo(movie_file):

FILE: PVM_models/demo02_unit.py
  class ExecutionUnit (line 36) | class ExecutionUnit(AbstractExecutionUnit.ExecutionUnit):
    method execution_steps (line 39) | def execution_steps(cls):
    method __init__ (line 46) | def __init__(self, parameters):
    method generate_missing_parameters (line 55) | def generate_missing_parameters(parameters):
    method execute0 (line 88) | def execute0(self):
    method cleanup (line 100) | def cleanup(self):

FILE: PVM_models/demo03_run.py
  class Manager (line 44) | class Manager(AbstractExecutionManager.ExecutionManager):
    method __init__ (line 45) | def __init__(self, prop_dict, steps_to_run, cam):
    method start (line 52) | def start(self):
    method fast_action (line 64) | def fast_action(self):
    method slow_action (line 78) | def slow_action(self):
    method running (line 106) | def running(self):
    method finish (line 112) | def finish(self):
  function get_neighbours (line 116) | def get_neighbours(x, y, sizex, sizey):
  function create_unit_parameters (line 129) | def create_unit_parameters(learning_rate, momentum, block, array, predic...
  function generate_dict (line 144) | def generate_dict(blocks_per_dim = 8, block_size = 8):
  function run_demo (line 291) | def run_demo(movie_file):

FILE: PVM_models/demo03_unit.py
  class ExecutionUnit (line 36) | class ExecutionUnit(AbstractExecutionUnit.ExecutionUnit):
    method execution_steps (line 39) | def execution_steps(cls):
    method __init__ (line 46) | def __init__(self, parameters):
    method generate_missing_parameters (line 56) | def generate_missing_parameters(parameters):
    method execute0 (line 91) | def execute0(self):
    method execute1 (line 103) | def execute1(self):
    method cleanup (line 107) | def cleanup(self):

FILE: PVM_models/demo04_run.py
  class Manager (line 44) | class Manager(AbstractExecutionManager.ExecutionManager):
    method __init__ (line 45) | def __init__(self, prop_dict, steps_to_run, cam):
    method start (line 52) | def start(self):
    method fast_action (line 66) | def fast_action(self):
    method slow_action (line 78) | def slow_action(self):
    method running (line 104) | def running(self):
    method finish (line 110) | def finish(self):
  function generate_dict (line 114) | def generate_dict():
  function run_demo (line 176) | def run_demo(movie_file):

FILE: PVM_models/demo04_unit.py
  class ExecutionUnit (line 36) | class ExecutionUnit(AbstractExecutionUnit.ExecutionUnit):
    method execution_steps (line 39) | def execution_steps(cls):
    method __init__ (line 46) | def __init__(self, parameters):
    method generate_missing_parameters (line 58) | def generate_missing_parameters(parameters):
    method execute0 (line 92) | def execute0(self):
    method cleanup (line 125) | def cleanup(self):

FILE: PVM_models/process_dream_data.py
  function disp (line 36) | def disp(winname, im):
  function normalized_dot (line 41) | def normalized_dot(a, b):
  function rotate (line 45) | def rotate(li, x):
  function process_data (line 49) | def process_data(filename, s):

FILE: PVM_tests/test_MLP.py
  function test_perceptron01 (line 34) | def test_perceptron01():
  function test_perceptron02 (line 54) | def test_perceptron02():
  function test_perceptron_two_layers (line 74) | def test_perceptron_two_layers():
  function test_MLPN_eval00 (line 94) | def test_MLPN_eval00():
  function test_MLPN_eval01 (line 121) | def test_MLPN_eval01():
  function test_MLPN_xor (line 168) | def test_MLPN_xor():
  function test_MLPN_xor_poly (line 204) | def test_MLPN_xor_poly():

FILE: PVM_tests/test_SharedArray.py
  function save (line 39) | def save(pobject, filename, protocol = -1):
  function load (line 48) | def load(filename):
  function test_pickling_array (line 57) | def test_pickling_array(tmpdir):
  function test_pickling_darray (line 68) | def test_pickling_darray(tmpdir):
  function worker_test_sharednes_of_an_array (line 86) | def worker_test_sharednes_of_an_array(Array, A):
  function test_sharedness_of_an_array (line 95) | def test_sharedness_of_an_array():
  function worker_test_sharedness_of_a_darray (line 107) | def worker_test_sharedness_of_a_darray(Array, Parity):
  function test_sharedness_of_a_darray (line 118) | def test_sharedness_of_a_darray():
  function worker_test_IPC_attachability (line 132) | def worker_test_IPC_attachability():
  function test_IPC_attachability (line 138) | def test_IPC_attachability():

FILE: PVM_tests/test_SyncUtils.py
  function ver_to_float (line 43) | def ver_to_float(ver):
  function worker_test_barrier (line 53) | def worker_test_barrier(barrier, array, proc_id):
  function parrent_test_barrier (line 64) | def parrent_test_barrier(UseSpinLock=False):
  function test_barrier (line 88) | def test_barrier():
  function test_barrier_spinlocks (line 95) | def test_barrier_spinlocks():

FILE: PVM_tests/test_bounding_region.py
  function test_initialization (line 34) | def test_initialization():
  function test_fundamentals (line 54) | def test_fundamentals():
  function test_area (line 78) | def test_area():
  function test_scaling (line 105) | def test_scaling():
  function test_box_intersection (line 154) | def test_box_intersection():
  function test_box_intersection_randomized (line 175) | def test_box_intersection_randomized():
  function test_mask_randomized (line 198) | def test_mask_randomized():

FILE: PVM_tests/test_create.py
  function test_PVM_crate (line 32) | def test_PVM_crate():

FILE: PVM_tests/test_fast_routines.py
  function test_derivative_dot (line 34) | def test_derivative_dot():
  function test_dot_transpose (line 52) | def test_dot_transpose():
  function test_dot_transpose_simple (line 71) | def test_dot_transpose_simple():
  function test_generalized_outer (line 89) | def test_generalized_outer():
  function dot_sigmoid (line 110) | def dot_sigmoid(append_bias):
  function test_dot_sigmoid (line 130) | def test_dot_sigmoid():
  function test_dot_add (line 135) | def test_dot_add():

FILE: PVM_tests/test_labeled_movie.py
  function test_basic_channels_1 (line 33) | def test_basic_channels_1():
  function test_basic_channels_2 (line 47) | def test_basic_channels_2():

FILE: PVM_tools/abstract_bounding_boxer.py
  class AbstractBoundingBoxer (line 47) | class AbstractBoundingBoxer(object):
    method set_current_bounding_box (line 54) | def set_current_bounding_box(self, bounding_region):
    method process (line 66) | def process(self, heatmap, previous_bb=None):
    method reset (line 78) | def reset(self):

FILE: PVM_tools/abstract_tracker.py
  class AbstractVisionTracker (line 57) | class AbstractVisionTracker(object):
    method prime (line 64) | def prime(self, im=None, bounding_box=None, **kwargs):
    method track (line 82) | def track(self, im):
    method get_bbox (line 96) | def get_bbox(self):
    method get_heatmap (line 108) | def get_heatmap(self, heatmap_name=None):
  class GenericVisionTracker (line 124) | class GenericVisionTracker(AbstractVisionTracker):
    method _prime (line 133) | def _prime(self, im, bounding_box=None, **kwargs):
    method prime (line 142) | def prime(self, im=None, bounding_box=None, **kwargs):
    method _track (line 169) | def _track(self, im):
    method track (line 179) | def track(self, im):
    method get_bbox (line 192) | def get_bbox(self):
    method get_heatmap (line 204) | def get_heatmap(self, heatmap_name=None):
    method get_name (line 219) | def get_name(self):
    method finish (line 228) | def finish(self):

FILE: PVM_tools/benchmark.py
  class TrackerBenchmark (line 65) | class TrackerBenchmark(object):
    method __init__ (line 67) | def __init__(self, output_dir="~/benchmark_results", resolution=None, ...
    method evaluate_on_file (line 145) | def evaluate_on_file(self,
    method save_results (line 318) | def save_results(self):
    method load_results (line 329) | def load_results(self, filename, timing_filename=None):
    method process_results (line 342) | def process_results(self, filename=None):
    method calc_precision (line 508) | def calc_precision(self, filename, target, tracker, thresholds, precis...
    method plot_precision (line 522) | def plot_precision(self, plot_individual_passes=False, precision_level...
    method calc_precision_rel (line 568) | def calc_precision_rel(self, filename, target, tracker, thresholds, pr...
    method plot_precision_rel (line 582) | def plot_precision_rel(self, plot_individual_passes=False, precision_l...
    method calc_success (line 624) | def calc_success(self, filename, target, tracker, absolute):
    method plot_success (line 644) | def plot_success(self, plot_individual_passes=False, absolute=True):
    method plot_performance (line 687) | def plot_performance(self, plot_individual_passes=True):
    method plot_presence (line 732) | def plot_presence(self, plot_individual_passes=True):
    method plot_timing (line 767) | def plot_timing(self, plot_individual_passes=True):
    method calc_accuracy (line 832) | def calc_accuracy(self, filename, target, tracker, thresholds):
    method plot_accuracy (line 839) | def plot_accuracy(self, plot_individual_passes=False):
    method print_results (line 884) | def print_results(self, file=sys.stdout, moviename=None):
    method get_summary (line 912) | def get_summary(self):
    method run (line 915) | def run(self, file_list, tracker_list, print_on_the_fly=True, channel=...
    method plot_all (line 947) | def plot_all(self):
  function time_dir_name (line 957) | def time_dir_name(prefix, suffix):

FILE: PVM_tools/bounding_region.py
  class BoundingRegion (line 50) | class BoundingRegion(object):
    method __init__ (line 113) | def __init__(self, image_shape=None, box=None, contour=None, confidenc...
    method _contour_from_box (line 168) | def _contour_from_box(self):
    method _box_from_contour (line 181) | def _box_from_contour(self):
    method _update_internals (line 185) | def _update_internals(self):
    method set_image_shape (line 189) | def set_image_shape(self, shape):
    method get_box_pixels (line 199) | def get_box_pixels(self):
    method get_box_relative (line 210) | def get_box_relative(self):
    method get_contour_pixels (line 223) | def get_contour_pixels(self):
    method get_contour_relative (line 234) | def get_contour_relative(self):
    method get_box_center_pixels (line 251) | def get_box_center_pixels(self):
    method get_box_center_relative (line 263) | def get_box_center_relative(self):
    method get_centroid_pixels (line 278) | def get_centroid_pixels(self):
    method get_centroid_relative (line 291) | def get_centroid_relative(self):
    method scale (line 305) | def scale(self, factor_x, factor_y=None):
    method draw_box (line 366) | def draw_box(self, image, color=(0, 0, 255), thickness=2, annotation=N...
    method draw_contour (line 403) | def draw_contour(self, image, color=(0, 0, 255), thickness=2, linetype...
    method get_min_area_rect_pixels (line 419) | def get_min_area_rect_pixels(self):
    method get_min_enclosing_circle_pixels (line 431) | def get_min_enclosing_circle_pixels(self):
    method get_ellipse_fit_pixels (line 444) | def get_ellipse_fit_pixels(self):
    method get_area_pixels (line 453) | def get_area_pixels(self):
    method get_area_relative (line 465) | def get_area_relative(self):
    method get_perimeter_pixels (line 480) | def get_perimeter_pixels(self):
    method empty (line 493) | def empty(self):
    method confidence (line 503) | def confidence(self):
    method get_mask (line 512) | def get_mask(self, contour=False):
    method get_box_intersection (line 537) | def get_box_intersection(self, bounding_region):
    method copy (line 568) | def copy(self):
    method scale_to_new_image_shape (line 577) | def scale_to_new_image_shape(self, new_image_shape, old_image_shape=No...
    method get_version (line 615) | def get_version(self):
    method is_keyframe (line 626) | def is_keyframe(self):
    method set_keyframe (line 637) | def set_keyframe(self, key_status):

FILE: PVM_tools/labeled_movie.py
  class LabeledMovieFrame (line 62) | class LabeledMovieFrame(object):
    method __init__ (line 104) | def __init__(self, internal_storage_method="raw", compression_level=80):
    method create_channel (line 118) | def create_channel(self, channel):
    method set_default_channel (line 144) | def set_default_channel(self, channel, rename_previous_default="channe...
    method set_image (line 178) | def set_image(self, image, channel="default", storage_method=None):
    method set_label (line 208) | def set_label(self, label, channel="default", target="default"):
    method set_timestamp (line 223) | def set_timestamp(self, timestamp, channel="default"):
    method set_metadata (line 234) | def set_metadata(self, metadata, channel="default"):
    method set_audio (line 245) | def set_audio(self, audio, channel="default"):
    method get_channels (line 256) | def get_channels(self):
    method get_image (line 265) | def get_image(self, channel="default"):
    method get_label (line 283) | def get_label(self, channel="default", target="default"):
    method get_targets (line 297) | def get_targets(self, channel="default"):
    method get_audio (line 311) | def get_audio(self, channel="default"):
    method get_timestamp (line 325) | def get_timestamp(self, channel="default"):
    method get_metadata (line 339) | def get_metadata(self, channel="default"):
    method version (line 354) | def version(self):
    method storage_method (line 358) | def storage_method(self):
    method compression (line 365) | def compression(self):
    method upgrade_to_latest_version (line 372) | def upgrade_to_latest_version(cls, Frame):
    method __setstate__ (line 407) | def __setstate__(self, state):
  class LabeledMovieHeader (line 421) | class LabeledMovieHeader(object):
    method __init__ (line 435) | def __init__(self, fps=20.0, created_date=time.ctime(), author="", cop...
    method fps (line 443) | def fps(self):
    method version (line 453) | def version(self):
    method created_date (line 463) | def created_date(self):
    method author (line 473) | def author(self):
    method copyright (line 483) | def copyright(self):
  class LabeledMovieWriter (line 492) | class LabeledMovieWriter(object):
    method __init__ (line 515) | def __init__(self, filename, movie_header=LabeledMovieHeader()):
    method write_frame (line 520) | def write_frame(self, Frame):
    method finish (line 530) | def finish(self):
  class LabeledMovieReader (line 537) | class LabeledMovieReader(object):
    method __init__ (line 544) | def __init__(self, filename):
    method fps (line 553) | def fps(self):
    method frames (line 564) | def frames(self):
    method get_header (line 596) | def get_header(self):
  class FrameCollection (line 603) | class FrameCollection(object):
    method __init__ (line 627) | def __init__(self, channel="default", movie_header=LabeledMovieHeader(...
    method append (line 632) | def append(self, Frame):
    method set_active_channel (line 641) | def set_active_channel(self, channel):
    method delete (line 651) | def delete(self, Frame):
    method __getitem__ (line 663) | def __getitem__(self, item):
    method __iter__ (line 666) | def __iter__(self):
    method __len__ (line 670) | def __len__(self):
    method reverse (line 673) | def reverse(self):
    method fps (line 682) | def fps(self):
    method write_to_file (line 690) | def write_to_file(self, filename):
    method load_from_file (line 702) | def load_from_file(self, filename):
    method Frame (line 715) | def Frame(self, index):

FILE: other_trackers/ASMSearcher.hpp
  type cvmat_t (line 39) | struct cvmat_t {
  type iplimage_t (line 46) | struct iplimage_t {
  function convert_from_cvmat (line 53) | cv::Mat convert_from_cvmat(PyObject *o, const char* name) {
  function convert_from_cviplimage (line 79) | cv::Mat convert_from_cviplimage(PyObject *o, const char *name) {
  function convertObj2Mat (line 102) | cv::Mat convertObj2Mat(boost::python::object image) {

FILE: other_trackers/backprojection.py
  class ColorHistogramBackProjection (line 33) | class ColorHistogramBackProjection(object):
    method __init__ (line 57) | def __init__(self, n_bins=(16, 16, 16), channels=(0, 1, 2), ranges=(0,...
    method calculateHistogram (line 67) | def calculateHistogram(self, image, bb=None):
    method _update_hist (line 100) | def _update_hist(self):
    method get_color_histogram (line 112) | def get_color_histogram(self):
    method add_color_histogram (line 120) | def add_color_histogram(self, hist):
    method subtract_color_histogram (line 132) | def subtract_color_histogram(self, hist):
    method reset (line 143) | def reset(self):
    method calculate (line 151) | def calculate(self, image):

FILE: other_trackers/bounding_boxer.py
  class CamshiftBoundingBoxer (line 35) | class CamshiftBoundingBoxer(AbstractBoundingBoxer):
    method __init__ (line 44) | def __init__(self, recovery_kernel_size=20, min_recovered_box_area=400...
    method reset (line 60) | def reset(self):
    method _find_new_box (line 66) | def _find_new_box(self, heatmap):
    method process (line 78) | def process(self, heatmap):
    method set_current_bounding_box (line 121) | def set_current_bounding_box(self, bounding_box):
  class FloodFillBoundingBoxer (line 132) | class FloodFillBoundingBoxer(AbstractBoundingBoxer):
    method __init__ (line 138) | def __init__(self, heatmap_threshold=200, distance_threshold=20):
    method reset (line 143) | def reset(self):
    method process (line 146) | def process(self, heatmap, previous_bb=None, peak=None):
    method set_current_bounding_box (line 183) | def set_current_bounding_box(self, bounding_region):

FILE: other_trackers/center_vision_tracker.py
  class CenterVisionTracker (line 34) | class CenterVisionTracker(GenericVisionTracker):
    method __init__ (line 40) | def __init__(self, size_factor=0.2, new_name=None):
    method reset (line 50) | def reset(self):
    method _prime (line 58) | def _prime(self, im, bounding_region):
    method _track (line 75) | def _track(self, im):
    method get_heatmap (line 88) | def get_heatmap(self, heatmap_name=None):

FILE: other_trackers/cmt_vision_tracker.py
  class CMTVisionTracker (line 39) | class CMTVisionTracker(GenericVisionTracker):
    method __init__ (line 43) | def __init__(self):
    method reset (line 50) | def reset(self):
    method _prime (line 59) | def _prime(self, im, bounding_region):
    method _track (line 84) | def _track(self, im):
    method get_heatmap (line 104) | def get_heatmap(self, heatmap_name=None):

FILE: other_trackers/color_vision_tracker.py
  class BasicHistogramBackprojectionTracker (line 36) | class BasicHistogramBackprojectionTracker(GenericVisionTracker):
    method __init__ (line 84) | def __init__(self,
    method _prime (line 104) | def _prime(self, im, bounding_box=None):
    method _track (line 126) | def _track(self, im):
    method reset (line 144) | def reset(self):
    method get_heatmap (line 153) | def get_heatmap(self, heatmap_name=None):
  class HSHistogramBackprojectionTracker (line 164) | class HSHistogramBackprojectionTracker(GenericVisionTracker):
    method __init__ (line 178) | def __init__(self):
    method _preprocess (line 182) | def _preprocess(self, image):
    method _prime (line 191) | def _prime(self, im, bounding_box=None, **kwargs):
    method _track (line 205) | def _track(self, im):
    method reset (line 217) | def reset(self):
    method get_heatmap (line 225) | def get_heatmap(self, heatmap_name=None):
  class UVHistogramBackprojectionTracker (line 236) | class UVHistogramBackprojectionTracker(GenericVisionTracker):
    method __init__ (line 250) | def __init__(self):
    method _preprocess (line 254) | def _preprocess(self, image):
    method _prime (line 263) | def _prime(self, im, bounding_box=None, **kwargs):
    method _track (line 277) | def _track(self, im):
    method reset (line 289) | def reset(self):
    method get_heatmap (line 297) | def get_heatmap(self, heatmap_name=None):

FILE: other_trackers/null_vision_tracker.py
  class NullVisionTracker (line 32) | class NullVisionTracker(GenericVisionTracker):
    method __init__ (line 38) | def __init__(self, scaling=1.0, new_name=None):
    method reset (line 48) | def reset(self):
    method _prime (line 56) | def _prime(self, im, bounding_region):
    method _track (line 70) | def _track(self, im):
    method get_heatmap (line 83) | def get_heatmap(self, heatmap_name=None):

FILE: other_trackers/opentld_python.cpp
  type tld (line 47) | namespace tld
    class TLD2 (line 50) | class TLD2
  function BOOST_PYTHON_MODULE (line 102) | BOOST_PYTHON_MODULE(tld)

FILE: other_trackers/struck.cpp
  function struck_init (line 43) | void struck_init(unsigned char* frame_data, int nrows, int ncols, const ...
  function struck_track (line 59) | void struck_track(unsigned char* frame_data){
  function BoundingBox (line 72) | BoundingBox struck_get_bbox(){

FILE: other_trackers/struck.h
  type BoundingBox (line 37) | struct BoundingBox{

FILE: other_trackers/struck_tracker.py
  class StruckTracker (line 82) | class StruckTracker(GenericVisionTracker):
    method __init__ (line 86) | def __init__(self):
    method reset (line 96) | def reset(self):
    method _prime (line 104) | def _prime(self, im, bounding_region):
    method _track (line 117) | def _track(self, im):
    method get_heatmap (line 137) | def get_heatmap(self, heatmap_name=None):

FILE: other_trackers/test_tld_basic.py
  function test_tld (line 36) | def test_tld():

FILE: other_trackers/tld_vision_tracker.py
  class TLDVisionTracker (line 36) | class TLDVisionTracker(GenericVisionTracker):
    method __init__ (line 42) | def __init__(self):
    method reset (line 49) | def reset(self):
    method _prime (line 58) | def _prime(self, im, bounding_region):
    method _track (line 86) | def _track(self, im):
    method get_heatmap (line 107) | def get_heatmap(self, heatmap_name=None):

FILE: setup.py
  function ver_to_float (line 38) | def ver_to_float(ver):

FILE: tracker_tools/export_to_zip.py
  function export_to_zip (line 52) | def export_to_zip(infile, outfile, channel, compression_level=80):

FILE: tracker_tools/images_to_PVM_pickle.py
  class ImagesToLabeledMovie (line 69) | class ImagesToLabeledMovie(object):
    method __init__ (line 71) | def __init__(self, label_file=None, image_dir=None, output_file=None, ...
    method get_next_image_file (line 84) | def get_next_image_file(self):
    method run (line 95) | def run(self):

FILE: tracker_tools/label_PVM_pickle.py
  class LabelingApp (line 158) | class LabelingApp(object):
    method __init__ (line 159) | def __init__(self, filename, output=None, channel="default", target="d...
    method reset (line 169) | def reset(self, reload=False):
    method fill_up_the_buffer (line 206) | def fill_up_the_buffer(self):
    method create_windows (line 216) | def create_windows(self):
    method set_trim_start (line 244) | def set_trim_start(self, frame_index):
    method set_trim_end (line 247) | def set_trim_end(self, frame_index):
    method jump_to_frame_callback (line 250) | def jump_to_frame_callback(self, frame_index):
    method update_completeness_window (line 255) | def update_completeness_window(self):
    method update_trackbar_window (line 266) | def update_trackbar_window(self):
    method refresh (line 286) | def refresh(self):
    method trackbar_window_onMouse (line 296) | def trackbar_window_onMouse(self, event, x, y, flags, _):
    method set_target_absent (line 358) | def set_target_absent(self):
    method set_bounding_box_with_center (line 361) | def set_bounding_box_with_center(self, x, y):
    method schedule_callback (line 369) | def schedule_callback(self):
    method timer_callback (line 374) | def timer_callback(self):
    method advance_current_frame (line 379) | def advance_current_frame(self, increment=1):
    method save_and_advance (line 386) | def save_and_advance(self, keyframe=False):
    method toggle_tracker (line 402) | def toggle_tracker(self):
    method advance_tracker (line 412) | def advance_tracker(self):
    method export_movie (line 417) | def export_movie(self):
    method interpolate (line 430) | def interpolate(self, start, end):
    method make_keyframe (line 446) | def make_keyframe(self):
    method run (line 471) | def run(self):

FILE: tracker_tools/movie_to_PVM_pickle.py
  function convert_to_pickle (line 79) | def convert_to_pickle(infilename, outfilename, dsize, channel, rotate, f...

FILE: tracker_tools/raw_to_PVM_pickle.py
  class ImagesToLabeledMovie (line 83) | class ImagesToLabeledMovie(object):
    method __init__ (line 85) | def __init__(self,
    method get_next_image_file (line 119) | def get_next_image_file(self):
    method run (line 126) | def run(self):

FILE: tracker_tools/scale_PVM_pickle.py
  function scale_content (line 72) | def scale_content(filename, dsize, source_channel, destination_channel, ...
Condensed preview — 106 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (891K chars).
[
  {
    "path": ".gitmodules",
    "chars": 372,
    "preview": "[submodule \"other_trackers/original_struck\"]\n\tpath = other_trackers/original_struck\n\turl = https://github.com/gnebehay/S"
  },
  {
    "path": "LICENSE.TXT",
    "chars": 15817,
    "preview": "\nLicense\n\nTHE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE (\"CCPL\" OR \"LI"
  },
  {
    "path": "PVM_framework/AbstractExecutionManager.py",
    "chars": 3215,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/AbstractExecutionUnit.py",
    "chars": 2738,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/Accelerated.cpp",
    "chars": 10497,
    "preview": "/*# ==================================================================================\n# Copyright (c) 2016, Brain Corpo"
  },
  {
    "path": "PVM_framework/Accelerated.h",
    "chars": 2638,
    "preview": "/*# ==================================================================================\n# Copyright (c) 2016, Brain Corpo"
  },
  {
    "path": "PVM_framework/CoreUtils.py",
    "chars": 17605,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/LowLevelCPUControlx86.pyx",
    "chars": 2783,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/MLP.py",
    "chars": 20162,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/PVM_Create.py",
    "chars": 40389,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/PVM_SignalProvider.py",
    "chars": 12522,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/PVM_Storage.py",
    "chars": 15090,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/PVM_datasets.py",
    "chars": 15234,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/PVM_debug_console.py",
    "chars": 31671,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/PVM_display_helper.py",
    "chars": 8054,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/PVM_options.py",
    "chars": 10811,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/PVM_tracker.json",
    "chars": 1152,
    "preview": "{\n    \"nodes\": [\n    {\n        \"name\": \"camera\",\n        \"params\": {\"camera_index\": 0, \"desired_width\": 160, \"desired_he"
  },
  {
    "path": "PVM_framework/PVM_upgrade_dict.py",
    "chars": 3607,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/SharedArray.py",
    "chars": 13209,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/Sync.cpp",
    "chars": 1808,
    "preview": "/*# ==================================================================================\n# Copyright (c) 2016, Brain Corpo"
  },
  {
    "path": "PVM_framework/Sync.h",
    "chars": 1623,
    "preview": "/*\n# ==================================================================================\n# Copyright (c) 2016, Brain Corp"
  },
  {
    "path": "PVM_framework/SyncUtils.pyx",
    "chars": 6870,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/SyncUtils_python.py",
    "chars": 5412,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/__init__.py",
    "chars": 1534,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/debug_logger.py",
    "chars": 4161,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_framework/fast_routines.pyx",
    "chars": 12318,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/PVM_Manager.py",
    "chars": 31077,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/PVM_plot_error.py",
    "chars": 8610,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/PVM_run.py",
    "chars": 17269,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/PVM_tracker.py",
    "chars": 7856,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/PVM_unit_2step_residual_v1.py",
    "chars": 20113,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/PVM_unit_test.py",
    "chars": 19077,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/PVM_unit_v1.py",
    "chars": 33090,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/__init__.py",
    "chars": 1534,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo00_run.py",
    "chars": 5906,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo00_unit.py",
    "chars": 2925,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo01_run.py",
    "chars": 6011,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo01_unit.py",
    "chars": 2995,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo02_run.py",
    "chars": 8774,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo02_unit.py",
    "chars": 5535,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo03_run.py",
    "chars": 16929,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo03_unit.py",
    "chars": 5896,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo04_run.py",
    "chars": 10747,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/demo04_unit.py",
    "chars": 7636,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/model_zoo/classic_medium.json",
    "chars": 1763,
    "preview": " {\n    \"autoencoder\": \"0\", \n    \"backpropagate_readout_error\": \"1\",\n    \"bias_free\": \"0\", \n    \"bias_free_readout\": \"0\","
  },
  {
    "path": "PVM_models/model_zoo/experiment1_face.json",
    "chars": 1757,
    "preview": " {\n    \"autoencoder\": \"0\", \n    \"backpropagate_readout_error\": \"1\",\n    \"bias_free\": \"0\", \n    \"bias_free_readout\": \"0\","
  },
  {
    "path": "PVM_models/model_zoo/experiment1_green_ball.json",
    "chars": 1763,
    "preview": " {\n    \"autoencoder\": \"0\", \n    \"backpropagate_readout_error\": \"1\",\n    \"bias_free\": \"0\", \n    \"bias_free_readout\": \"0\","
  },
  {
    "path": "PVM_models/model_zoo/experiment1_stop_sign.json",
    "chars": 1762,
    "preview": " {\n    \"autoencoder\": \"0\", \n    \"backpropagate_readout_error\": \"1\",\n    \"bias_free\": \"0\", \n    \"bias_free_readout\": \"0\","
  },
  {
    "path": "PVM_models/model_zoo/experiment2.json",
    "chars": 1761,
    "preview": " {\n    \"autoencoder\": \"0\", \n    \"backpropagate_readout_error\": \"0\",\n    \"bias_free\": \"0\", \n    \"bias_free_readout\": \"0\","
  },
  {
    "path": "PVM_models/model_zoo/very_deep.json",
    "chars": 1926,
    "preview": " {\n    \"autoencoder\": \"0\", \n    \"backpropagate_readout_error\": \"0\",\n    \"bias_free\": \"0\", \n    \"bias_free_readout\": \"0\","
  },
  {
    "path": "PVM_models/process_dream_data.py",
    "chars": 4098,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_models/run_tracking_benchmark.py",
    "chars": 8478,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tests/test_MLP.py",
    "chars": 8935,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tests/test_SharedArray.py",
    "chars": 5169,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tests/test_SyncUtils.py",
    "chars": 3946,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tests/test_bounding_region.py",
    "chars": 9306,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tests/test_create.py",
    "chars": 6579,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tests/test_fast_routines.py",
    "chars": 5890,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tests/test_labeled_movie.py",
    "chars": 2871,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tools/__init__.py",
    "chars": 1534,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tools/abstract_bounding_boxer.py",
    "chars": 2919,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tools/abstract_tracker.py",
    "chars": 8513,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tools/benchmark.py",
    "chars": 57878,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "PVM_tools/bounding_region.py",
    "chars": 26472,
    "preview": "\"\"\"\nThis module contains the class called BoundingRegion which encapsulates\na number of methods used in any bounding box"
  },
  {
    "path": "PVM_tools/labeled_movie.py",
    "chars": 26050,
    "preview": "\"\"\"\nThis module contains a number of classes representing handling, loading and encoding a movie object with labels.\n\nTh"
  },
  {
    "path": "README.md",
    "chars": 18866,
    "preview": "# Predictive Vision Model \n\n## Introduction\nThis repository contains necessary files and demos used for running multicor"
  },
  {
    "path": "add_thirdparty_tracker_submodules.sh",
    "chars": 544,
    "preview": "git submodule add https://github.com/gnebehay/STRUCK other_trackers/original_struck\ncd other_trackers/original_struck\ngi"
  },
  {
    "path": "docs/Makefile",
    "chars": 6838,
    "preview": "# Makefile for Sphinx documentation\n#\n\n# You can set these variables from the command line.\nSPHINXOPTS    =\nSPHINXBUILD "
  },
  {
    "path": "docs/conf.py",
    "chars": 8596,
    "preview": "# -*- coding: utf-8 -*-\n#\n# Predictive Vision Framework documentation build configuration file, created by\n# sphinx-quic"
  },
  {
    "path": "docs/generate.sh",
    "chars": 37,
    "preview": "sphinx-apidoc -o . ../ -e\nmake html\n\n"
  },
  {
    "path": "docs/index.rst",
    "chars": 486,
    "preview": ".. Predictive Vision Framework documentation master file, created by\n   sphinx-quickstart on Wed Jun 22 17:54:18 2016.\n "
  },
  {
    "path": "docs/make.bat",
    "chars": 6739,
    "preview": "@ECHO OFF\r\n\r\nREM Command file for Sphinx documentation\r\n\r\nif \"%SPHINXBUILD%\" == \"\" (\r\n\tset SPHINXBUILD=sphinx-build\r\n)\r\n"
  },
  {
    "path": "download_data.sh",
    "chars": 1119,
    "preview": "cd\necho \"Downloading the labeled movie sequences (3GB). Be patient.\"\nwget http://pvm.braincorporation.net/PVM_data_seque"
  },
  {
    "path": "install_local.sh",
    "chars": 224,
    "preview": "git submodule init\ngit submodule update\npython setup.py build_ext --inplace\nexport PYTHONPATH=\"$PYTHONPATH:`pwd`\"\ncd oth"
  },
  {
    "path": "install_ubuntu_dependencies.sh",
    "chars": 633,
    "preview": "#!/bin/bash\napt-get -y install git\napt-get -y install wget\napt-get -y install python-opencv\napt-get -y install python-nu"
  },
  {
    "path": "other_trackers/ASMSearcher.hpp",
    "chars": 3443,
    "preview": "/**\n# ==================================================================================\n# Copyright (c) 2016, Brain Cor"
  },
  {
    "path": "other_trackers/__init__.py",
    "chars": 1534,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/backprojection.py",
    "chars": 6399,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/bounding_boxer.py",
    "chars": 9747,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/center_vision_tracker.py",
    "chars": 3518,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/cmt_vision_tracker.py",
    "chars": 4144,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/color_vision_tracker.py",
    "chars": 11801,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/null_vision_tracker.py",
    "chars": 3181,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/opentld_python.cpp",
    "chars": 4186,
    "preview": "/**\n# ==================================================================================\n# Copyright (c) 2016, Brain Cor"
  },
  {
    "path": "other_trackers/setup_opentld.py",
    "chars": 3465,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/setup_struck.py",
    "chars": 3209,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/struck.cpp",
    "chars": 3158,
    "preview": "/*\n# ==================================================================================\n# Copyright (c) 2016, Brain Corp"
  },
  {
    "path": "other_trackers/struck.h",
    "chars": 2041,
    "preview": "/*\n# ==================================================================================\n# Copyright (c) 2016, Brain Corp"
  },
  {
    "path": "other_trackers/struck_bindings.pyx",
    "chars": 2495,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/struck_tracker.py",
    "chars": 4653,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/test_struck_bindings.py",
    "chars": 2371,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/test_tld_basic.py",
    "chars": 2350,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "other_trackers/tld_vision_tracker.py",
    "chars": 4281,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "pytest.ini",
    "chars": 970,
    "preview": "# Configuration of py.test\n[pytest]\n# Run each test in an isolated environment\n# Ignore specialised tests\n# Our test plu"
  },
  {
    "path": "requirements.txt",
    "chars": 360,
    "preview": "# pip requirements file\n# example to include bc repos. Don't forget egg name (the same as repo name)\n\n# Test functionali"
  },
  {
    "path": "setup.py",
    "chars": 3804,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "test",
    "chars": 206,
    "preview": "#!/bin/bash\n# Check for python27 (so this works on centos)\nif type python27 > /dev/null 2>/dev/null ; then\n\tPYTHONEXEC=p"
  },
  {
    "path": "tracker_tools/__init__.py",
    "chars": 1534,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "tracker_tools/export_to_zip.py",
    "chars": 6192,
    "preview": "\"\"\"\nExports a movie in pickled labeled movie format to a zip archive containing the images and text description\nof the b"
  },
  {
    "path": "tracker_tools/images_to_PVM_pickle.py",
    "chars": 5543,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "tracker_tools/label_PVM_pickle.py",
    "chars": 24808,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "tracker_tools/movie_to_PVM_pickle.py",
    "chars": 6478,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "tracker_tools/play_PVM_pickle.py",
    "chars": 4957,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "tracker_tools/raw_to_PVM_pickle.py",
    "chars": 8273,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "tracker_tools/scale_PVM_pickle.py",
    "chars": 6522,
    "preview": "# ==================================================================================\n# Copyright (c) 2016, Brain Corpora"
  },
  {
    "path": "tracker_tools/upgrade_cloud_lib.py",
    "chars": 2846,
    "preview": "\"\"\"\nThis small tool will go over all the pkl files stores in the ts amazon bucket,\ndownload each one of them, load and u"
  }
]

About this extraction

This page contains the full source code of the braincorp/PVM GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 106 files (835.1 KB), approximately 201.1k tokens, and a symbol index with 709 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!