| 14 | | class RecordingManager(object): |
|---|
| 15 | | |
|---|
| 16 | | def __init__(self, setup_function, get_function): |
|---|
| 17 | | """ |
|---|
| 18 | | `setup_function` should take a variable, a source list, and an optional filename |
|---|
| 19 | | and return an identifier. |
|---|
| 20 | | `get_function` should take the identifier returned by `setup_function` and |
|---|
| 21 | | return the recorded spikes, Vm trace, etc. |
|---|
| 22 | | |
|---|
| 23 | | Example: |
|---|
| 24 | | rm = RecordingManager(_nest_record, _nest_get) |
|---|
| 25 | | """ |
|---|
| 26 | | # create temporary directory |
|---|
| 27 | | tempdir = tempfile.mkdtemp() |
|---|
| 28 | | # initialise mapping from recording identifiers to temporary filenames |
|---|
| 29 | | self.recorder_list = [] |
|---|
| 30 | | # for parallel simulations, determine if this is the master node |
|---|
| 31 | | self._setup = setup_function |
|---|
| 32 | | self._get = get_function |
|---|
| 33 | | |
|---|
| 34 | | def add_recording(self, sources, variable, filename=None): |
|---|
| 35 | | recorder = self._setup(variable, sources, filename) |
|---|
| 36 | | self.recorder_list.append(recorder) |
|---|
| 37 | | |
|---|
| 38 | | def get_recording(self, recording_id): |
|---|
| 39 | | return self._get(recording_id) |
|---|
| 40 | | |
|---|
| 41 | | def write(self, recording_id, filename_or_obj, format="compatible", gather=True): |
|---|
| 42 | | pass |
|---|
| | 14 | #class RecordingManager(object): |
|---|
| | 15 | # |
|---|
| | 16 | # def __init__(self, setup_function, get_function): |
|---|
| | 17 | # """ |
|---|
| | 18 | # `setup_function` should take a variable, a source list, and an optional filename |
|---|
| | 19 | # and return an identifier. |
|---|
| | 20 | # `get_function` should take the identifier returned by `setup_function` and |
|---|
| | 21 | # return the recorded spikes, Vm trace, etc. |
|---|
| | 22 | # |
|---|
| | 23 | # Example: |
|---|
| | 24 | # rm = RecordingManager(_nest_record, _nest_get) |
|---|
| | 25 | # """ |
|---|
| | 26 | # # create temporary directory |
|---|
| | 27 | # tempdir = tempfile.mkdtemp() |
|---|
| | 28 | # # initialise mapping from recording identifiers to temporary filenames |
|---|
| | 29 | # self.recorder_list = [] |
|---|
| | 30 | # # for parallel simulations, determine if this is the master node |
|---|
| | 31 | # self._setup = setup_function |
|---|
| | 32 | # self._get = get_function |
|---|
| | 33 | # |
|---|
| | 34 | # def add_recording(self, sources, variable, filename=None): |
|---|
| | 35 | # recorder = self._setup(variable, sources, filename) |
|---|
| | 36 | # self.recorder_list.append(recorder) |
|---|
| | 37 | # |
|---|
| | 38 | # def get_recording(self, recording_id): |
|---|
| | 39 | # return self._get(recording_id) |
|---|
| | 40 | # |
|---|
| | 41 | # def write(self, recording_id, filename_or_obj, format="compatible", gather=True): |
|---|
| | 42 | # pass |
|---|
| 117 | | |
|---|
| | 117 | |
|---|
| | 118 | def write_compatible_output1(data_source, user_filename, variable, input_format, population, dt): |
|---|
| | 119 | """ |
|---|
| | 120 | Rewrite simulation data in a standard format: |
|---|
| | 121 | spiketime (in ms) cell_id-min(cell_id) |
|---|
| | 122 | """ |
|---|
| | 123 | if isinstance(data_source, numpy.ndarray): |
|---|
| | 124 | logging.info("Writing %s in compatible format (from memory)" % user_filename) |
|---|
| | 125 | N = len(data_source) |
|---|
| | 126 | else: # assume data is a filename or open file object |
|---|
| | 127 | logging.info("Writing %s in compatible format (was %s)" % (user_filename, data_source)) |
|---|
| | 128 | try: |
|---|
| | 129 | N = os.path.getsize(data_source) |
|---|
| | 130 | except Exception: |
|---|
| | 131 | N = 0 |
|---|
| | 132 | |
|---|
| | 133 | if N > 0: |
|---|
| | 134 | user_file = StandardTextFile(user_filename) |
|---|
| | 135 | # Write header info (e.g., dimensions of the population) |
|---|
| | 136 | metadata = {} |
|---|
| | 137 | if population is not None: |
|---|
| | 138 | metadata.update({ |
|---|
| | 139 | 'dimensions': "\t".join([str(d) for d in population.dim]), |
|---|
| | 140 | 'first_id': population.first_id, |
|---|
| | 141 | 'last_id': population.first_id + len(population)-1 |
|---|
| | 142 | }) |
|---|
| | 143 | id_offset = population.first_id |
|---|
| | 144 | else: |
|---|
| | 145 | id_offset = 0 |
|---|
| | 146 | metadata['dt'] = dt |
|---|
| | 147 | |
|---|
| | 148 | input_format = input_format.split() |
|---|
| | 149 | time_column = input_format.index('t') |
|---|
| | 150 | id_column = input_format.index('id') |
|---|
| | 151 | |
|---|
| | 152 | if variable == 'conductance': |
|---|
| | 153 | ge_column = input_format.index('ge') |
|---|
| | 154 | gi_column = input_format.index('gi') |
|---|
| | 155 | column_map = [ge_column, gi_column, id_column] |
|---|
| | 156 | raise Exception("Not yet implemented") |
|---|
| | 157 | elif variable == 'v': # voltage files |
|---|
| | 158 | v_column = input_format.index('v') |
|---|
| | 159 | column_map = [v_column, id_column] |
|---|
| | 160 | elif variable == 'spikes': # spike files |
|---|
| | 161 | column_map = [time_column, id_column] |
|---|
| | 162 | else: |
|---|
| | 163 | raise Exception("Invalid variable") |
|---|
| | 164 | |
|---|
| | 165 | # Read/select data |
|---|
| | 166 | if isinstance(data_source, numpy.ndarray): |
|---|
| | 167 | data_array = data_source[:, column_map] |
|---|
| | 168 | else: # assume data is a filename or open file object |
|---|
| | 169 | #data_array = numpy.loadtxt(data_source, usecols=column_map) |
|---|
| | 170 | data_array = readArray(sim_filename, sepchar=None) |
|---|
| | 171 | data_array[:,-1] -= id_offset # replies on fact that id is always last column |
|---|
| | 172 | metadata['n'] = data_array.shape[0] |
|---|
| | 173 | |
|---|
| | 174 | user_file.write(data_array, metadata) |
|---|
| | 175 | else: |
|---|
| | 176 | logging.warning("%s is empty" % sim_filename) |
|---|
| | 177 | |
|---|
| | 202 | |
|---|
| | 203 | |
|---|
| | 204 | class StandardTextFile(object): |
|---|
| | 205 | |
|---|
| | 206 | |
|---|
| | 207 | def __init__(self, filename, gather=False): |
|---|
| | 208 | self.name = filename |
|---|
| | 209 | self.gather = gather |
|---|
| | 210 | self.fileobj = open(self.name, 'w', DEFAULT_BUFFER_SIZE) |
|---|
| | 211 | |
|---|
| | 212 | def write(self, data, metadata): |
|---|
| | 213 | # can we write to the file more than once? In this case, should use seek,tell |
|---|
| | 214 | # to always put the header information at the top? |
|---|
| | 215 | # write header |
|---|
| | 216 | header_lines = ["# %s = %s" % item for item in metadata.items()] |
|---|
| | 217 | self.fileobj.write("\n".join(header_lines) + '\n') |
|---|
| | 218 | # write data |
|---|
| | 219 | numpy.savetxt(self.fileobj, data, fmt = '%s', delimiter='\t') |
|---|
| | 220 | self.fileobj.close() |
|---|
| | 221 | |
|---|