def compress_shape(decompressed_shape): """ Returns a run-length encoded compressed shape. Parameters ---------- decompressed_shape : ndarray Decompressed shape. Returns ------- compressed_shape : Holder A Holder object containing the shape of the compressed shape ndarray and the compressed shape ndarray itself. """ if decompressed_shape.shape[0] != 1: raise ValueError("input should be of shape (1,x)") if not isinstance(decompressed_shape, np.ndarray): raise TypeError("input should be of type numpy.ndarray") data = np.array([decompressed_shape[0][0]]) data = np.concatenate((data, np.diff(decompressed_shape[0]))) mask_changes = np.array([1]) diff_as_ones = (abs(np.diff(data)) > 1e-8).astype(int) mask_changes = np.concatenate((mask_changes, diff_as_ones)) vals = data[np.nonzero(mask_changes)].astype(float) k = np.array(np.nonzero(np.append(mask_changes, 1))) k = k.reshape((1, k.shape[1])) n = np.diff(k)[0] n_extra = (n - 2).astype(float) vals2 = np.copy(vals) vals2[np.where(n_extra < 0)] = np.NAN n_extra[np.where(n_extra < 0)] = np.NAN v = np.array([vals, vals2, n_extra]) v = np.concatenate(np.hsplit(v, v.shape[1])) finite_vals = np.isfinite(v) v = v[finite_vals] v_abs = abs(v) smallest_indices = np.where(v_abs < 1e-10) v[smallest_indices] = 0 compressed_shape = Holder() compressed_shape.num_samples = decompressed_shape.shape[1] compressed_shape.data = v.reshape([1, v.shape[0]]) return compressed_shape
def makeadc(kwargs): """ Makes a Holder object for an ADC Event. Parameters ---------- kwargs : dict Key value mappings of ADC Event parameters_params and values. Returns ------- adc : Holder ADC Event. """ num_samples = kwargs.get("num_samples", 0) system = kwargs.get("system", Opts()) dwell = kwargs.get("dwell", 0) duration = kwargs.get("duration", 0) delay = kwargs.get("delay", 0) freq_offset = kwargs.get("freq_offset", 0) phase_offset = kwargs.get("phase_offset", 0) adc = Holder() adc.type = 'adc' adc.num_samples = num_samples adc.dwell = dwell adc.delay = delay adc.freq_offset = freq_offset adc.phase_offset = phase_offset adc.dead_time = system.adc_dead_time if (dwell == 0 and duration == 0) or (dwell > 0 and duration > 0): raise ValueError("Either dwell or duration must be defined, not both") if duration > 0: # getcontext().prec = 4 adc.dwell = float(Decimal(duration) / Decimal(num_samples)) adc.duration = dwell * num_samples if dwell > 0 else 0 return adc
def get_block(self, block_index): """ Returns Block at position specified by block_index. Parameters ---------- block_index : int Index of Block to be retrieved. Returns ------- block : dict Block at position specified by block_index. """ block = {} event_ind = self.block_events[block_index] if event_ind[0] > 0: delay = Holder() delay.type = 'delay' delay.delay = self.delay_library.data[event_ind[0]] block['delay'] = delay elif event_ind[1] > 0: rf = Holder() rf.type = 'rf' lib_data = self.rf_library.data[event_ind[1]] amplitude, mag_shape, phase_shape = lib_data[0], lib_data[1], lib_data[ 2] shape_data = self.shape_library.data[mag_shape] compressed = Holder() compressed.num_samples = shape_data[0][0] compressed.data = shape_data[0][1:] compressed.data = compressed.data.reshape( (1, compressed.data.shape[0])) mag = decompress_shape(compressed) shape_data = self.shape_library.data[phase_shape] compressed.num_samples = shape_data[0][0] compressed.data = shape_data[0][1:] compressed.data = compressed.data.reshape( (1, compressed.data.shape[0])) phase = decompress_shape(compressed) rf.signal = 1j * 2 * np.pi * phase rf.signal = amplitude * mag * np.exp(rf.signal) rf.t = [(x * self.rf_raster_time) for x in range(1, max(mag.shape) + 1)] rf.t = np.reshape(rf.t, (1, len(rf.t))) rf.freq_offset = lib_data[3] rf.phase_offset = lib_data[4] if max(lib_data.shape) < 6: lib_data = np.append(lib_data, 0) rf.dead_time = lib_data[5] block['rf'] = rf grad_channels = ['gx', 'gy', 'gz'] for i in range(1, len(grad_channels) + 1): if event_ind[2 + (i - 1)] > 0: grad, compressed = Holder(), Holder() type = self.grad_library.type[event_ind[2 + (i - 1)]] lib_data = self.grad_library.data[event_ind[2 + (i - 1)]] grad.type = 'trap' if type == 't' else 'grad' grad.channel = grad_channels[i - 1][1] if grad.type == 'grad': amplitude = lib_data[0] shape_id = lib_data[1] shape_data = self.shape_library.data[shape_id] compressed.num_samples = shape_data[0][0] compressed.data = np.array([shape_data[0][1:]]) g = decompress_shape(compressed) grad.waveform = amplitude * g grad.t = np.array([[ x * self.grad_raster_time for x in range(1, g.size + 1) ]]) else: grad.amplitude, grad.rise_time, grad.flat_time, grad.fall_time = [ lib_data[x] for x in range(4) ] grad.area = grad.amplitude * ( grad.flat_time + grad.rise_time / 2 + grad.fall_time / 2) grad.flat_area = grad.amplitude * grad.flat_time block[grad_channels[i - 1]] = grad if event_ind[5] > 0: lib_data = self.adc_library.data[event_ind[5]] if max(lib_data.shape) < 6: lib_data = np.append(lib_data, 0) adc = Holder() adc.num_samples, adc.dwell, adc.delay, adc.freq_offset, adc.phase_offset, adc.dead_time = [ lib_data[x] for x in range(6) ] adc.type = 'adc' block['adc'] = adc return block