def test_ROIEvents():

    trace = np.linspace(0, 2.0, 5)
    events = np.arange(5, dtype=int)
    roi_events = dc_types.ROIEvents()
    roi_events['trace'] = trace
    roi_events['events'] = events

    np_almost(roi_events['trace'], np.linspace(0, 2.0, 5), decimal=10)
    np_equal(roi_events['events'], np.arange(5, dtype=int))
def test_ROIEventSet():
    event_set = dc_types.ROIEventSet()
    rng = np.random.RandomState(888812)
    true_trace_s = []
    true_event_s = []
    true_trace_c = []
    true_event_c = []
    for ii in range(3):
        t = rng.random_sample(13)
        e = rng.randint(0, 111, size=13)
        signal = dc_types.ROIEvents()
        signal['trace'] = t
        signal['events'] = e
        true_trace_s.append(t)
        true_event_s.append(e)

        t = rng.random_sample(13)
        e = rng.randint(0, 111, size=13)
        crosstalk = dc_types.ROIEvents()
        crosstalk['trace'] = t
        crosstalk['events'] = e
        true_trace_c.append(t)
        true_event_c.append(e)

        channels = dc_types.ROIEventChannels()
        channels['signal'] = signal
        channels['crosstalk'] = crosstalk
        event_set[ii] = channels

    for ii in range(3):
        np_almost(event_set[ii]['signal']['trace'],
                  true_trace_s[ii], decimal=10)
        np_equal(event_set[ii]['signal']['events'],
                 true_event_s[ii])
        np_almost(event_set[ii]['crosstalk']['trace'],
                  true_trace_c[ii], decimal=10)
        np_equal(event_set[ii]['crosstalk']['events'],
                 true_event_c[ii])
    assert 0 in event_set
    assert 1 in event_set
    assert 2 in event_set
    assert 3 not in event_set
    keys = event_set.keys()
    keys.sort()
    assert keys == [0, 1, 2]
    channels = event_set.pop(1)
    np_almost(channels['signal']['trace'],
              true_trace_s[1], decimal=10)
    np_equal(channels['signal']['events'],
             true_event_s[1])
    np_almost(channels['crosstalk']['trace'],
              true_trace_c[1], decimal=10)
    np_equal(channels['crosstalk']['events'],
             true_event_c[1])
    assert 0 in event_set
    assert 2 in event_set
    assert 1 not in event_set
    keys = event_set.keys()
    keys.sort()
    assert keys == [0, 2]
def test_ROIEventChannels():
    rng = np.random.RandomState(1245)
    traces = list([rng.random_sample(10) for ii in range(2)])
    events = list([rng.randint(0, 20, size=10) for ii in range(2)])

    event_set = dc_types.ROIEventChannels()
    for ii, k in enumerate(('signal', 'crosstalk')):
        ee = dc_types.ROIEvents()
        ee['trace'] = traces[ii]
        ee['events'] = events[ii]
        event_set[k] = ee

    np_almost(event_set['signal']['trace'], traces[0], decimal=10)
    np_equal(event_set['signal']['events'], events[0])
    np_almost(event_set['crosstalk']['trace'], traces[1], decimal=10)
    np_equal(event_set['crosstalk']['events'], events[1])
def test_flag_to_events():

    rng = np.random.RandomState(77123)

    n_t = 30
    input_events = []
    input_traces = []
    output_events = []

    len_ne = 2

    ff = np.zeros(n_t, dtype=bool)
    ff[:10] = True
    tt = rng.random_sample(n_t)
    input_events.append(ff)
    input_traces.append(tt)
    output_events.append(np.arange(12, dtype=int))

    ff = np.zeros(n_t, dtype=bool)
    ff[:10] = True
    ff[18:22] = True
    tt = rng.random_sample(n_t)
    input_events.append(ff)
    input_traces.append(tt)
    output_events.append(
        np.concatenate(
            [np.arange(12, dtype=int),
             np.arange(16, 24, dtype=int)]))

    ff = np.zeros(n_t, dtype=bool)
    ff[:10] = True
    ff[18:22] = True
    ff[27:] = True
    tt = rng.random_sample(n_t)
    input_events.append(ff)
    input_traces.append(tt)
    output_events.append(
        np.concatenate([
            np.arange(12, dtype=int),
            np.arange(16, 24, dtype=int),
            np.arange(25, n_t, dtype=int)
        ]))

    ff = np.zeros(n_t, dtype=bool)
    ff[5:10] = True
    ff[18:22] = True
    ff[27:] = True
    tt = rng.random_sample(n_t)
    input_events.append(ff)
    input_traces.append(tt)
    output_events.append(
        np.concatenate([
            np.arange(3, 12, dtype=int),
            np.arange(16, 24, dtype=int),
            np.arange(25, n_t, dtype=int)
        ]))

    ff = np.zeros(n_t, dtype=bool)
    ff[5:10] = True
    ff[18:21] = True
    ff[26] = True
    tt = rng.random_sample(n_t)
    input_events.append(ff)
    input_traces.append(tt)
    output_events.append(
        np.concatenate([
            np.arange(3, 12, dtype=int),
            np.arange(16, 23, dtype=int),
            np.arange(24, 29, dtype=int)
        ]))

    ff = np.zeros(n_t, dtype=bool)
    ff[5:10] = True
    ff[18:21] = True
    ff[23:25] = True
    tt = rng.random_sample(n_t)
    input_events.append(ff)
    input_traces.append(tt)
    output_events.append(
        np.concatenate(
            [np.arange(3, 12, dtype=int),
             np.arange(16, 27, dtype=int)]))

    ff = np.zeros(n_t, dtype=bool)
    ff[:10] = True
    ff[12:15] = True
    ff[23:25] = True
    tt = rng.random_sample(n_t)
    input_events.append(ff)
    input_traces.append(tt)
    output_events.append(
        np.concatenate(
            [np.arange(17, dtype=int),
             np.arange(21, 27, dtype=int)]))

    ff = np.zeros(n_t, dtype=bool)
    ff[5:10] = True
    ff[18:21] = True
    ff[23:] = True
    tt = rng.random_sample(n_t)
    input_events.append(ff)
    input_traces.append(tt)
    output_events.append(
        np.concatenate(
            [np.arange(3, 12, dtype=int),
             np.arange(16, n_t, dtype=int)]))

    input_traces = np.array(input_traces)
    input_events = np.array(input_events)

    (test_traces, test_events) = at._flag_to_events(input_traces,
                                                    input_events,
                                                    len_ne=len_ne)

    for ii in range(len(input_traces)):
        np_equal(test_events[ii], output_events[ii])
        np_almost_equal(test_traces[ii],
                        input_traces[ii][output_events[ii]],
                        decimal=10)