"""run_controller.py in less memory, with the same model, the same arithmetic and the same export.

  cd MODEL_DIR && FELISYN_SEQUENCE=seq.json FELISYN_EXPORT=out.json.gz \
      python ../run_controller_mem.py nest THREADS param_spont/defaults [path value ...] RUN_NAME

Takes the arguments of run_controller.py and runs that file unchanged through runpy, so its SHA-256 (99d215f2...) stays
true and sys.argv[1:] reaches it as given. Every change here frees memory that nothing reads again. None of them
touches an RNG, a connection, a floating-point value or the order of anything. Byte-identity proof on this Mac, with
the failed-comparison control: logs/proof-bytes.txt. Why each change is safe, with file and line: memory-study.md
section 3, corrected in logs/PROGRESS-proof.txt. Line numbers below are the pinned commits': PyNN 981853d, mozaik f5c09a0.

R0  MallocSpaceEfficient=1. macOS keeps freed blocks in the footprint by default. It must be set before the process
    starts, so if it is missing this file re-executes itself once with the same arguments.
R1  Drop FromListConnector.conn_list once PyNN has made the connections. pyNN/common/projections.py:100 keeps the
    connector for the whole run, and only connect() reads the list (pyNN/connectors.py:562-597). The empty array
    has to be a copy: conn_list[:0] alone is a view, and a view keeps its parent array alive, so nothing would be freed.
R3  Clear the LGN input cache when process_input returns. The cache has one reader, the membership test at
    spatiotemporalfilter.py:578. run_controller.py already clears the cache on entry to every process_input, so that
    test is always false in these runs, and clearing earlier changes no read. NEST holds its own copy of the values
    (stepcurrent iaf_cond_exp_sc_nc.cpp:285-286).
R4a Drop CellWithReceptiveField.va after view(). Line 266 is its only use in mozaik.
R4b Drop a cell's response arrays when response_current() returns. It is the last reader: the cells are made fresh in
    _calculate_input_currents (:728), and it is called once for each of them (:777). provide_null_input makes its own.
R4c Keep one copy of 'times', which is the same for every cell: temporal_resolution * arange(n). Line 598 adds to it
    into a new array and never writes it. It is shared only if numpy.array_equal says so, cell by cell.
R10 Stop a leak in pinned NEST. NodeCollection[integer numpy array] goes through take_array_index (pynestkernel.pyx:250),
    which never deletes the datum it gets from node_collection_array_index, so the new collection lives for the whole
    run. PyNN does this once per target neuron and projection (pyNN/nest/projections.py:233): 0.84 GiB on the 2 mm
    model. nest.NodeCollection(list of node ids) makes the same collection through the same NodeCollection::create and
    frees it. The source file is unchanged. Repeats keep raising the ValueError PyNN relies on, and every other key
    (slice, list, bool mask) takes NEST's own path.
"""
import os
import runpy
import sys

if os.environ.get('MallocSpaceEfficient') != '1':   # R0
    os.environ['MallocSpaceEfficient'] = '1'
    os.execv(sys.executable, [sys.executable] + sys.argv)

import matplotlib                                   # noqa: E402
matplotlib.use('Agg')                               # run_controller.py does this before its other imports
import nest                                         # noqa: E402
import numpy                                        # noqa: E402
import pyNN.connectors                              # noqa: E402
from mozaik.models.vision import spatiotemporalfilter as stf   # noqa: E402

_connect = pyNN.connectors.FromListConnector.connect


def connect(self, projection):                      # R1
    out = _connect(self, projection)
    self.conn_list = numpy.array(self.conn_list[:0])
    return out


pyNN.connectors.FromListConnector.connect = connect

_process_input = stf.SpatioTemporalFilterRetinaLGN.process_input


def process_input(self, *args, **kwargs):           # R3
    out = _process_input(self, *args, **kwargs)
    self.internal_stimulus_cache.clear()
    return out


stf.SpatioTemporalFilterRetinaLGN.process_input = process_input

_view = stf.CellWithReceptiveField.view


def view(self):                                     # R4a
    _view(self)
    self.va = None


stf.CellWithReceptiveField.view = view

_response_current = stf.CellWithReceptiveField.response_current
_times = []


def response_current(self):                         # R4b, R4c
    out = _response_current(self)
    self.contrast_response = self.luminance_response = self.mean = None
    if _times and numpy.array_equal(_times[0], out['times']):
        out['times'] = _times[0]
    else:
        _times[:] = [out['times']]
    return out


stf.CellWithReceptiveField.response_current = response_current

_getitem = nest.NodeCollection.__getitem__
_node_ids = {}


def getitem(self, key):                             # R10
    if (isinstance(key, numpy.ndarray) and key.ndim == 1 and len(key) > 0 and numpy.issubdtype(key.dtype, numpy.integer)
            and key.min() >= 0 and key.max() < len(self)):
        if len(numpy.unique(key)) != len(key):
            raise ValueError('All node IDs in a NodeCollection have to be unique')
        entry = _node_ids.get(id(self._datum))
        if entry is None:
            if len(_node_ids) >= 64:
                _node_ids.clear()
            entry = _node_ids[id(self._datum)] = (self._datum, numpy.array(self.tolist()))
        return nest.NodeCollection(entry[1][key].tolist())
    return _getitem(self, key)


nest.NodeCollection.__getitem__ = getitem

runpy.run_path(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'run_controller.py'), run_name='__main__')
