def should_round_trip_multi_element_data_frame(self):
        arr1 = np.random.randn(10,10) * pq.s
        arr1.labels = [u'volts', u'other']
        arr1.name = u'name'
        arr1.sampling_rates = [1.0 * pq.Hz, 10.0 * pq.Hz]

        arr2 = np.random.randn(10,10) * pq.V
        arr2.labels = [u'volts', u'other']
        arr2.name = u'name'
        arr2.sampling_rates = [1.0 * pq.Hz, 10.0 * pq.Hz]

        epoch = self.expt.insertEpoch(DateTime(), DateTime(), self.protocol, None, None)

        ar = epoch.addAnalysisRecord("record", to_map({}), self.protocol, to_map({}))

        result_name1 = 'result'
        result_name2 = 'other-result'
        expected = {result_name1: arr1,
                    result_name2: arr2}
        record_name = "record-name"
        artifact = insert_numeric_analysis_artifact(ar, record_name, expected)

        assert artifact is not None

        sleep(0.5)

        actual = as_data_frame(ar.getOutputs().get(record_name))

        assert_data_frame_equals(expected, actual)
def calculate_cell_means(project):
    cells = {}
    epochs = []
    for expt in iterable(project.getExperiments()):
        for epoch in iterable(expt.getEpochs()): # ctx.getObjectsWithTag('demo')
            epochs += [epoch]
            cell = epoch.getInputSources().get('cell')
            if len(list(iterable(epoch.getMeasurements()))) > 0:
                m = epoch.getMeasurement('Membrane current')
                data = as_data_frame(m)
                peak = max(data['current']).item()
                k = "{0} ({1})  {2}".format(cell.getLabel(), cell.getIdentifier(), cell.getURI().toString())
                peaks = cells.get(k, {})
                pk = "{0} mV".format(epoch.getProtocolParameters().get('step_amplitude_mV'))
                cell_peaks = peaks.get(pk, [])
                peaks[pk] = cell_peaks + [peak]
                cells[k] = peaks
    
    for (k,v) in cells.iteritems():
        for ck, cv in v.iteritems():
            v[ck] = sp.mean(cv)
    
        cells[k] = v
        
    return (cells, epochs)
    def should_import_spike_trains(self):
        expt2 = self.ctx.insertProject("project2","project2",DateTime()).insertExperiment("purpose", DateTime())
        protocol2 = self.ctx.insertProtocol("protocol", "description")
        epoch_start = DateTime()

        epoch = expt2.insertEpoch(Maps.newHashMap(),
                                  Maps.newHashMap(),
                                  epoch_start,
                                  DateTime(),
                                  protocol2,
                                  to_map({}),
                                  to_map({}))

        segment = self.block.segments[0]

        times = [.1, .2, .3, .4]
        waveforms = np.random.rand(2,3,4) * pq.mV

        train_name = 'spike train 1'
        spike_train = SpikeTrain(times, name=train_name, t_stop=2.0 * pq.s, units="s", waveforms=waveforms)

        segment.spiketrains.append(spike_train)
        try:
            import_spiketrains(epoch, protocol2, segment)

            records = list(iterable(epoch.getAnalysisRecords()))

            assert_equals(1, len(records))

            ar = records[0]

            assert_equals(train_name, ar.getName())

            expected_params = {'t_start_ms': spike_train.t_start.rescale(pq.ms).item(),
                  't_stop_ms': spike_train.t_stop.rescale(pq.ms).item(),
                  'sampling_rate_hz': spike_train.sampling_rate.rescale(pq.Hz).item(),
                  'description': spike_train.description,
                  'file_origin': spike_train.file_origin}

            for (k,v) in expected_params.iteritems():
                actual = ar.getProtocolParameters().get(k)
                if actual:
                    assert_equals(v, actual)

            assert_equals(len(expected_params), ar.getProtocolParameters().size())

            data_map = ar.getDataElements()
            df = as_data_frame(data_map.get(spike_train.name))

            check_signal(spike_train, df['spike times'])
            check_signal(spike_train.waveforms, df['spike waveforms'])
        finally:
            segment.spiketrains.remove(spike_train)
def _round_trip_array(arr, experiment, protocol):
    epoch = experiment.insertEpoch(DateTime(), DateTime(), protocol, None, None)
    trace_name = 'trace1'
    expected = { trace_name: arr }

    sourceName = 'source'
    s = epoch.getDataContext().insertSource('source-name', 'source-id')
    epoch.addInputSource(sourceName, s)

    m = insert_numeric_measurement(epoch, set([sourceName]), set(['amp']), trace_name, expected)

    sleep(0.5)

    actual = as_data_frame(m)
    return (expected, actual)
    def should_round_trip_pandas_data_frame_artifact(self):
        expected = pd.DataFrame({
            'ColA': np.random.randn(10),
            'ColB': np.random.randn(10)
        })

        project = list(self.expt.getProjects())[0]
        record = project.addAnalysisRecord('name', to_map({}), None,
                                           to_map({}))

        m = add_tabular_analysis_artifact(record, 'tabular', expected)

        while (m.getDataContext().getFileService().hasPendingUploads()):
            pass

        m = asclass('Measurement', m.refresh())

        actual = as_data_frame(m, index_col=0)

        assert_frame_equal(expected, actual)
    def should_round_trip_pandas_data_frame(self):

        expected = pd.DataFrame({
            'ColA': np.random.randn(10),
            'ColB': np.random.randn(10)
        })

        epoch = self.expt.insertEpoch(DateTime(), DateTime(), self.protocol,
                                      None, None)

        m = insert_tabular_measurement(epoch, set(), set(), 'tabular',
                                       expected)

        while (m.getDataContext().getFileService().hasPendingUploads()):
            pass

        m = asclass('Measurement', m.refresh())

        actual = as_data_frame(m, index_col=0)

        assert_frame_equal(expected, actual)
def check_numeric_measurement(signal, m):
    data_frame = as_data_frame(m)
    for (name, data) in data_frame.iteritems():
        check_signal(data, signal)