def testreadwrite(self): # create a file with all of the supported data types timecol = [2.01, 2.02, 2.03, 2.04] listcol = [1.0, 2.1, 3.4, 5.3] seqcol = axographio.linearsequence(4, 0.12345, 0.54321) scaledcol = axographio.scaledarray([12, -18, 16, 42], 0.001, 0.1) floatcol = np.array([1.8218, 4.3672, 5.1484, 7.3235], dtype=np.float32) doublecol = np.array( [1.82187845, 4.367244252, 5.14842452445, 7.322452435], dtype=np.float64) shortcol = np.array([11043, -21274, -1834, 32341], dtype=np.int16) intcol = np.array([1213104371, -2027483727, -18, 3234], dtype=np.int32) originalfile = axographio.file_contents( ['time', '', 'seq', 'float', 'double', 'short', 'int'], [timecol, listcol, seqcol, floatcol, doublecol, shortcol, intcol]) # try rewriting and rereading it multiple times for fileformat in axographio.supported_formats: currentfile = copy.deepcopy( originalfile) # to protect the original currentfile.fileformat = fileformat # the older formats may lose precision; that's OK for this # test. if fileformat == axographio.old_graph_format: accuracy = 5 # digits elif fileformat == axographio.old_digitized_format: accuracy = 3 # digits else: accuracy = 16 # digits # Do more than one pass of reading and writing to make sure the # data remains consistent. for pass_number in range(2): handle, tempfilename = tempfile.mkstemp() try: currentfile.write(tempfilename) currentfile = axographio.read(tempfilename) finally: os.close(handle) os.remove(tempfilename) # make sure the newly read data matches the original version self.assertEqual(currentfile.fileformat, fileformat) self.assertEqual(len(originalfile.names), len(currentfile.names)) self.assertEqual(len(originalfile.data), len(currentfile.data)) self.assertTrue( np.all([ a == b for a, b in zip(originalfile.names, currentfile.names) ])) for a, b in zip(originalfile.data, currentfile.data): self.assertTrue( np.all( self.roughly(a, accuracy) == self.roughly( b, accuracy)))
def read_block(self, **kargs): if self.filename is not None: self.axo_obj = axographio.read(self.filename) # Build up the block blk = Block() blk.rec_datetime = None if self.filename is not None: # modified time is not ideal but less prone to # cross-platform issues than created time (ctime) blk.file_datetime = datetime.fromtimestamp(os.path.getmtime(self.filename)) # store the filename if it is available blk.file_origin = self.filename # determine the channel names and counts _, channel_ordering = np.unique(self.axo_obj.names[1:], return_index=True) channel_names = np.array(self.axo_obj.names[1:])[np.sort(channel_ordering)] channel_count = len(channel_names) # determine the time signal and sample period sample_period = self.axo_obj.data[0].step * pq.s start_time = self.axo_obj.data[0].start * pq.s # Attempt to read units from the channel names channel_unit_names = [x.split()[-1].strip('()') for x in channel_names] channel_units = [] for unit in channel_unit_names: try: channel_units.append(pq.Quantity(1, unit)) except LookupError: channel_units.append(None) # Strip units from channel names channel_names = [' '.join(x.split()[:-1]) for x in channel_names] # build up segments by grouping axograph columns for seg_idx in range(1, len(self.axo_obj.data), channel_count): seg = Segment(index=seg_idx) # add in the channels for chan_idx in range(0, channel_count): signal = pq.Quantity( self.axo_obj.data[seg_idx + chan_idx], channel_units[chan_idx]) analog = AnalogSignal(signal, sampling_period=sample_period, t_start=start_time, name=channel_names[chan_idx], channel_index=chan_idx) seg.analogsignals.append(analog) blk.segments.append(seg) blk.create_many_to_one_relationship() return blk
def test_graphfile(self): file = axographio.read(example_files['old_graph_format']) # do some sanity checks to make sure the file loaded as expected self.assertEqual(file.fileformat, axographio.old_graph_format) self.assertEqual(len(file.names), len(file.data)) self.assertEqual(len(file.names), 3) self.assertEqual(len(file.names), len(file.data)) self.assertEqual(file.names[0], 'Time (sec)') self.assertEqual(file.names[1], 'Current (A)') self.assertEqual(file.names[2], 'Current (A)') for d in file.data: self.assertEqual(len(d), 2048) self.assertEqual(round(sum(file.data[1]), 13), -2.1483816e-6) self.assertEqual(round(sum(file.data[2]), 13), -1.4318602e-6) self.assertEqual(round(float(file.data[0][2047]), 6), 1.024)
def test_digitizedfile(self): file = axographio.read(example_files['old_digitized_format']) # do some sanity checks to make sure the file loaded as expected self.assertEqual(file.fileformat, axographio.old_digitized_format) self.assertEqual(len(file.names), len(file.data)) self.assertEqual(len(file.names), 29) self.assertEqual(len(file.names), len(file.data)) self.assertEqual(file.names[0], 'Time (s)') self.assertEqual(file.names[1], 'Current (A)') self.assertEqual(file.names[28], 'Column29') for d in file.data: self.assertEqual(len(d), 200) self.assertEqual(round(sum(file.data[1]), 13), -1.973206e-07) self.assertEqual(round(sum(file.data[28]), 7), -0.0101875) self.assertEqual(round(file.data[0][199], 6), 0.02)
def test_axograph_x_file(self): file = axographio.read(example_files['axograph_x_format']) # do some sanity checks to make sure the file loaded as expected self.assertEqual(file.fileformat, axographio.axograph_x_format) self.assertEqual(len(file.names), len(file.data)) self.assertEqual(len(file.names), 7) self.assertEqual(len(file.names), len(file.data)) self.assertEqual(file.names[0], 'Time (s)') self.assertEqual(file.names[1], 'Current (A)') self.assertEqual(file.names[2], '') self.assertEqual(file.names[6], '') for d in file.data: self.assertEqual(len(d), 1000) self.assertEqual(round(sum(file.data[1]), 15), -2.3021573e-8) self.assertEqual(round(sum(file.data[6]), 15), -2.5514375e-8) self.assertEqual(file.data[0][999], 0.05)
def __init__(self, meta): self._path = os.path.join(d.BASE_PATH, meta[d.PATH]) # build up child traces try: axo = axographio.read(self._path) # pylint:ignore E1101 except OSError: print(f"Unable to find file - {self._path}") raise self.protocol = d.PARAMS[meta[d.PAR]] self._len = (len(axo.data) - 1) // self.protocol.column_count offsets = self.protocol.offsets(self._len) # read in the axograph data self.times = axo.data[0] print("Processing %s which has %s runs" % (self._path, self._len)) # convert optical powers converted_opt_pow = self.convert_optical_power(meta[d.O_P]) converted_opt_thresh = self.convert_optical_power(meta[d.O_T]) # store cell metadata self._meta = meta self.opt_pow = converted_opt_pow self.elec_pow = meta[d.E_P] self.opt_thresh = int(math.floor(100. * (converted_opt_pow / converted_opt_thresh))) self.opt_thresh_bucket = self._num_to_bucket(self.opt_thresh) self.elec_thresh = int(math.floor(100. * (meta[d.E_P] / meta[d.E_T]))) self.elec_thresh_bucket = self._num_to_bucket(self.elec_thresh) self.cell = meta[d.CEL] # print(f"{converted_opt_pow}-{meta[d.O_P]} / {converted_opt_thresh}-{meta[d.O_T]} = {self.opt_thresh}") self.runs = [ CostimulationRun( self, self.protocol, x, offsets[i], self.elec_thresh, self.opt_thresh, self.cell ) for i, x in enumerate(self.protocol.get_groups(axo)) ] self.any_aps = any(x.costim_aps for x in self.runs)
def __init__(self, file_path, trace_nr = 0): # load trace from axograph file self.file = file_path self.date = RE.search(r'([0-9]{6})', self.file ).group(0) self.filenum = int(RE.search(r' ([0-9]{3}) ', self.file ).group(0)) self.episode = RE.sub('ep', 'episode ', RE.search(r'ep([0-9]{2})', self.file ).group(0) ) axodata = AG.read(self.file) # extract time times = [not(elmt is None) for elmt in [RE.search(r'(?i)(Time)', nstr) for nstr in axodata.names]] trace_t = (NP.array([axodata.data[idx2][:] for idx2 in [idx for idx in range(len(times)) if times[idx]] ] , dtype = float)[0]) * units['ms'] # extract current restr = r'(?i)(Ipatch)' # current da = [not(elmt is None) for elmt in [RE.search(restr, nstr) \ for nstr in axodata.names]] trace_i = [NP.array(axodata.data[idx2]) \ for idx2 in [idx for idx in range(len(da)) if da[idx]] ] pd_dict = { \ "time": trace_t \ , "current": NP.array(trace_i[trace_nr]) * units['pA'] \ } self.raw = PD.DataFrame.from_dict(pd_dict) # print self.raw[self.raw.time <= 1].current self.sampling_rate = (len(self.raw.current)-1) / ((NP.max(self.raw.time)-NP.min(self.raw.time))/units['ms']) print self.sampling_rate #+trace_t[:-1]/2 pd_dict = { \ "time": trace_t[1:] \ , "current": NP.diff(self.raw.current) \ } self.diff = PD.DataFrame.from_dict(pd_dict)
def read_block(self, **kargs): if self.filename is not None: self.axo_obj = axographio.read(self.filename) # Build up the block blk = Block() blk.rec_datetime = None if self.filename is not None: # modified time is not ideal but less prone to # cross-platform issues than created time (ctime) blk.file_datetime = datetime.fromtimestamp( os.path.getmtime(self.filename)) # store the filename if it is available blk.file_origin = self.filename # determine the channel names and counts _, channel_ordering = np.unique(self.axo_obj.names[1:], return_index=True) channel_names = np.array( self.axo_obj.names[1:])[np.sort(channel_ordering)] channel_count = len(channel_names) # determine the time signal and sample period sample_period = self.axo_obj.data[0].step * pq.s start_time = self.axo_obj.data[0].start * pq.s # Attempt to read units from the channel names channel_unit_names = [x.split()[-1].strip('()') for x in channel_names] channel_units = [] for unit in channel_unit_names: try: channel_units.append(pq.Quantity(1, unit)) except LookupError: channel_units.append(None) # Strip units from channel names channel_names = [' '.join(x.split()[:-1]) for x in channel_names] # build up segments by grouping axograph columns for seg_idx in range(1, len(self.axo_obj.data), channel_count): seg = Segment(index=seg_idx) # add in the channels for chan_idx in range(0, channel_count): signal = pq.Quantity(self.axo_obj.data[seg_idx + chan_idx], channel_units[chan_idx]) analog = AnalogSignal(signal, sampling_period=sample_period, t_start=start_time, name=channel_names[chan_idx], channel_index=chan_idx) seg.analogsignals.append(analog) blk.segments.append(seg) blk.create_many_to_one_relationship() return blk