Changeset 458

Show
Ignore:
Timestamp:
09/10/08 15:20:11 (2 months ago)
Author:
apdavison
Message:

In nest2, defer creating the NEST recording device (in the Recorder class) until the first time Recorder.record() is called.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/__init__.py

    r445 r458  
    22__all__ = ["common", "random", "nest1", "nest2", "neuron", "neuron2", "pcsim", 'brian' ] 
    33 
    4 # 
  • trunk/src/nest2/__init__.py

    r439 r458  
    116116        self.population = population # needed for writing header information 
    117117        self.recorded = Set([])         
    118         # create device 
    119         device_name = RECORDING_DEVICE_NAMES[variable] 
     118        # we defer creating the actual device until it is needed. 
     119        self._device = None 
     120 
     121    def _create_device(self): 
     122        device_name = RECORDING_DEVICE_NAMES[self.variable] 
    120123        self._device = nest.Create(device_name) 
    121         device_parameters = {"withgid": True, "withtime": True, 
    122                              "to_file": True, "to_memory": False} 
     124        device_parameters = {"withgid": True, "withtime": True} 
    123125        if self.variable != 'spikes': 
    124126            device_parameters["interval"] = get_time_step() 
    125         if file is False: 
     127        if self.file is False: 
    126128            device_parameters.update(to_file=False, to_memory=True) 
     129        else: # (includes self.file is None) 
     130            device_parameters.update(to_file=True, to_memory=False) 
    127131        # line needed for old version of nest 2.0 
    128        # device_parameters.pop('to_memory') 
     132        # device_parameters.pop('to_memory') 
    129133        nest.SetStatus(self._device, device_parameters) 
    130134 
    131135    def record(self, ids): 
    132136        """Add the cells in `ids` to the set of recorded cells.""" 
     137        if self._device is None: 
     138            self._create_device() 
    133139        ids = Set(ids) 
    134140        new_ids = list( ids.difference(self.recorded) ) 
     
    150156    def get(self, gather=False): 
    151157        """Returns the recorded data.""" 
     158        if self._device is None: 
     159            raise Exception("No cells recorded, so no data to return") 
    152160        if nest.GetStatus(self._device, 'to_file')[0]: 
    153161            nest_filename = _merge_files(self._device, gather) 
     
    167175     
    168176    def write(self, file=None, gather=False, compatible_output=True): 
     177        if self._device is None: 
     178            raise Exception("No cells recorded, so no data to write to file.") 
    169179        user_file = file or self.file 
    170180        if isinstance(user_file, basestring): 
     
    811821 
    812822    def _record(self, variable, record_from=None, rng=None,to_file=True): 
    813         if to_file is False: 
    814             nest.SetStatus(self.recorders[variable]._device, {'to_file': False, 'to_memory': True}) 
    815  
    816823        # create list of neurons 
    817824        fixed_list = False 
     
    839846                rng = numpy.random 
    840847            tmp_list = rng.permutation(numpy.reshape(self.cell, (self.cell.size,)))[0:n_rec] 
    841  
     848     
    842849        self.recorders[variable].record(tmp_list) 
     850        nest.SetStatus(self.recorders[variable]._device, {'to_file': to_file, 'to_memory': not to_file}) 
    843851 
    844852    def record(self, record_from=None, rng=None, to_file=True):