Esempio n. 1
0
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
Esempio n. 2
0
def maketrapezoid(kwargs):
    """
    Makes a Holder object for an trapezoidal gradient Event.

    Parameters
    ----------
    kwargs : dict
        Key value mappings of trapezoidal gradient Event parameters_params and values.

    Returns
    -------
    grad : Holder
        Trapezoidal gradient Event configured based on supplied kwargs.
    """

    channel = kwargs.get("channel", "z")
    system = kwargs.get("system", Opts())
    duration = kwargs.get("duration", 0)
    area_result = kwargs.get("area", -1)
    flat_time_result = kwargs.get("flat_time", 0)
    flat_area_result = kwargs.get("flat_area", -1)
    amplitude_result = kwargs.get("amplitude", -1)
    max_grad = kwargs.get("max_grad", 0)
    max_slew = kwargs.get("max_slew", 0)
    rise_time = kwargs.get("rise_time", 0)

    max_grad = max_grad if max_grad > 0 else system.max_grad
    max_slew = max_slew if max_slew > 0 else system.max_slew
    rise_time = rise_time if rise_time > 0 else system.rise_time

    if area_result == -1 and flat_area_result == -1 and amplitude_result == -1:
        raise ValueError('Must supply either '
                         'area'
                         ', '
                         'flat_area'
                         ' or '
                         'amplitude'
                         '')

    if flat_time_result > 0:
        amplitude = amplitude_result if (amplitude_result != -1) else (
            flat_area_result / flat_time_result)
        if rise_time == 0:
            rise_time = abs(amplitude) / max_slew
            rise_time = ceil(
                rise_time / system.grad_raster_time) * system.grad_raster_time
        fall_time, flat_time = rise_time, flat_time_result
    elif duration > 0:
        if amplitude_result != -1:
            amplitude = amplitude_result
        else:
            if rise_time == 0:
                dC = 1 / abs(2 * max_slew) + 1 / abs(2 * max_slew)
                amplitude = (duration - sqrt(
                    pow(duration, 2) - 4 * abs(area_result) * dC)) / (2 * dC)
            else:
                amplitude = area_result / (duration - rise_time)

        if rise_time == 0:
            rise_time = ceil(amplitude / max_slew /
                             system.grad_raster_time) * system.grad_raster_time

        fall_time = rise_time
        flat_time = (duration - rise_time - fall_time)

        amplitude = area_result / (rise_time / 2 + fall_time / 2 + flat_time
                                   ) if amplitude_result == -1 else amplitude
    else:
        raise ValueError('Must supply a duration')

    if abs(amplitude) > max_grad:
        raise ValueError("Amplitude violation")

    grad = Holder()
    grad.type = "trap"
    grad.channel = channel
    grad.amplitude = amplitude
    grad.rise_time = rise_time
    grad.flat_time = flat_time
    grad.fall_time = fall_time
    grad.area = amplitude * (flat_time + rise_time / 2 + fall_time / 2)
    grad.flat_area = amplitude * flat_time

    return grad