| | 275 | |
| | 276 | |
| | 277 | |
| | 278 | class NestFile(FileHandler): |
| | 279 | |
| | 280 | def __init__(self, filename, padding=0, with_time=False, with_gid=True): |
| | 281 | self.filename = filename |
| | 282 | self.metadata = {} |
| | 283 | assert (padding >= 0) and (type(padding) == int), "ERROR ! padding should be a positive int" |
| | 284 | self.padding = padding |
| | 285 | self.with_time = with_time |
| | 286 | self.with_gid = with_gid |
| | 287 | self.standardtxtfile = StandardTextFile(filename) |
| | 288 | |
| | 289 | def write(self, object): |
| | 290 | """ |
| | 291 | Write the object to the file. |
| | 292 | |
| | 293 | Examples: |
| | 294 | >> handler.write(SpikeListObject) |
| | 295 | >> handler.write(VmListObject) |
| | 296 | """ |
| | 297 | return self.standardtxtfile.write(object) |
| | 298 | |
| | 299 | def __check_params(self, params): |
| | 300 | """ |
| | 301 | Establish a control/completion/correction of the params to create an object by |
| | 302 | using comparison and data extracted from the metadata. |
| | 303 | """ |
| | 304 | if 'dt' in params: |
| | 305 | if params['dt'] is None and 'dt' in self.metadata: |
| | 306 | logging.debug("dt is infered from the file header") |
| | 307 | params['dt'] = self.metadata['dt'] |
| | 308 | if params['id_list'] is None: |
| | 309 | print "WARNING: id_list will be infered based on active cells..." |
| | 310 | elif isinstance(params['id_list'], int): # allows to just specify the number of neurons |
| | 311 | params['id_list'] = range(params['id_list']) |
| | 312 | elif not isinstance(params['id_list'], list): |
| | 313 | raise Exception("id_list should be an int or a list !") |
| | 314 | if params['dims'] is None: |
| | 315 | if 'dimensions' in self.metadata: |
| | 316 | params['dims'] = self.metadata['dimensions'] |
| | 317 | else: |
| | 318 | raise Exception("dims can not be infered while reading %s" %self.filename) |
| | 319 | return params |
| | 320 | |
| | 321 | def get_data(self, sepchar = "\t", skipchar = "#"): |
| | 322 | """ |
| | 323 | Load data from a text file and returns a list of data |
| | 324 | """ |
| | 325 | if HAVE_TABLEIO: |
| | 326 | data = TableIO.readTableAsArray(self.filename, skipchar) |
| | 327 | else: |
| | 328 | myfile = open(self.filename, "r", DEFAULT_BUFFER_SIZE) |
| | 329 | contents = myfile.readlines() |
| | 330 | myfile.close() |
| | 331 | data = [] |
| | 332 | header = True |
| | 333 | idx = 0 |
| | 334 | while header: |
| | 335 | if contents[idx][0] != skipchar: |
| | 336 | header = False |
| | 337 | break |
| | 338 | idx += 1 |
| | 339 | for i in xrange(idx, len(contents)): |
| | 340 | line = contents[i].strip().split(sepchar) |
| | 341 | id = [float(line[0])] |
| | 342 | id += map(float, line[1:]) |
| | 343 | data.append(id) |
| | 344 | return numpy.array(data) |
| | 345 | |
| | 346 | def _fix_id_list(self, data, params): |
| | 347 | print "All gids are shifted by padding", self.padding |
| | 348 | data[:,0] = numpy.array(data[:,0], int) - self.padding |
| | 349 | if params['id_list'] is None: |
| | 350 | params['id_list'] = numpy.unique(data[:,0]) |
| | 351 | return data, params |
| | 352 | |
| | 353 | def read_spikes(self, params): |
| | 354 | """ |
| | 355 | Read a SpikeList object from a file and return the SpikeList object, created from the File and |
| | 356 | from the additional params that may have been provided |
| | 357 | |
| | 358 | Examples: |
| | 359 | >> params = {'id_list' : range(100), 't_stop' : 1000} |
| | 360 | >> handler.read_spikes(params) |
| | 361 | SpikeList Object (with params taken into account) |
| | 362 | """ |
| | 363 | p = self.__check_params(params) |
| | 364 | from NeuroTools import signals |
| | 365 | data = self.get_data() |
| | 366 | data, p = self._fix_id_list(data, p) |
| | 367 | return signals.SpikeList(data, p['id_list'], p['t_start'], p['t_stop'], p['dims']) |
| | 368 | |
| | 369 | def read_analogs(self, type, params): |
| | 370 | p = self.__check_params(params) |
| | 371 | data = self.get_data() |
| | 372 | data, p = self._fix_id_list(data, p) |
| | 373 | from NeuroTools.signals import analogs |
| | 374 | if type == "vm": |
| | 375 | return analogs.VmList(data, p['id_list'], p['dt'], p['t_start'], p['t_stop'], p['dims']) |
| | 376 | elif type == "current": |
| | 377 | return analogs.CurrentList(data, p['id_list'], p['dt'], p['t_start'], p['t_stop'], p['dims']) |
| | 378 | elif type == "conductance": |
| | 379 | if len(data[0,:]) > 2: |
| | 380 | g_exc = analogs.ConductanceList(data[:,[0,1]] , p['id_list'], p['dt'], p['t_start'], p['t_stop'], p['dims']) |
| | 381 | g_inh = analogs.ConductanceList(data[:,[0,2]] , p['id_list'], p['dt'], p['t_start'], p['t_stop'], p['dims']) |
| | 382 | return [g_exc, g_inh] |
| | 383 | else: |
| | 384 | return analogs.ConductanceList(data, p['id_list'], p['dt'], p['t_start'], p['t_stop'], p['dims']) |
| | 385 | |