Ejemplo n.º 1
0
 def test_large_file_from_usb(self):
     sample_count = 2000000 * 2
     fh, sample_count = self._create_large_file(sample_count)
     r = DataReader().open(fh)
     self.assertEqual([0, sample_count], r.sample_id_range)
     reduction = r.get_reduction()
     self.assertEqual(sample_count // 20000, len(reduction))
Ejemplo n.º 2
0
def on_cmd(args):
    r = DataReader().open(args.filename)
    print(r.summary_string())
    start = args.start
    stop = args.stop
    if stop < 0:
        stop = r.sample_id_range[1] + 1 + stop

    if args.export is not None:
        i, v = r.get_calibrated(start, stop)
        data = np.hstack((i.reshape((-1, 1)), (v.reshape((-1, 1)))))
        if args.export.endswith('npy'):
            np.save(args.export, data)
        else:
            np.savetxt(args.export, data, fmt='%.5g', delimiter=',')

    if args.plot:
        import matplotlib.pyplot as plt
        y = r.get_reduction(start, stop)
        x = np.arange(len(y)) * (r.config['samples_per_reduction'] /
                                 r.config['sampling_frequency'])
        f = plt.figure()
        for axis in range(3):
            ax = f.add_subplot(3, 1, axis + 1)
            ax.plot(x, y[:, axis, 0], color='blue')
            ax.plot(x, y[:, axis, 2], color='red')
            ax.plot(x, y[:, axis, 3], color='red')

        plt.show()
        plt.close(f)

    r.close()
    return 0
Ejemplo n.º 3
0
 def test_write_read_get_reduction(self):
     fh = self._create_file(0, 32)
     dfr = datafile.DataFileReader(fh)
     dfr.pretty_print()
     fh.seek(0)
     r = DataReader().open(fh)
     data = r.get_reduction(0, 4000)
     np.testing.assert_allclose(np.arange(999, 7000, 2000), data[:,
                                                                 0]['mean'])
Ejemplo n.º 4
0
 def test_empty_file(self):
     fh = io.BytesIO()
     d = DataRecorder(fh)
     d.close()
     fh.seek(0)
     r = DataReader().open(fh)
     self.assertEqual([0, 0], r.sample_id_range)
     self.assertEqual(1.0, r.sampling_frequency)
     self.assertEqual(1.0, r.input_sampling_frequency)
     self.assertEqual(1.0, r.output_sampling_frequency)
     self.assertEqual(1.0, r.reduction_frequency)
     self.assertEqual(0.0, r.duration)
     self.assertEqual(0, r.voltage_range)
     self.assertEqual(0, len(r.get_reduction(0, 0)))
     self.assertEqual(0, len(r.data_get(0, 0)))
     self.assertEqual(0, r.time_to_sample_id(0.0))
     self.assertEqual(0.0, r.sample_id_to_time(0))
     self.assertIsNone(r.samples_get(0, 0))
     r.close()
Ejemplo n.º 5
0
 def test_write_read_get_reduction_offset(self):
     fh = self._create_file(0, 32)
     r = DataReader().open(fh)
     data = r.get_reduction(1000, 4000)
     np.testing.assert_allclose([2999, 4999, 6999], data[:, 0]['mean'])
Ejemplo n.º 6
0
 def test_write_read_get_reduction_offset(self):
     fh = self._create_file(0, 2)
     r = DataReader().open(fh)
     data = r.get_reduction(30, 95)
     np.testing.assert_allclose(np.arange(69, 180, 20), data[:, 0, 0])
Ejemplo n.º 7
0
 def test_write_read_get_reduction(self):
     fh = self._create_file(0, 2)
     r = DataReader().open(fh)
     data = r.get_reduction(0, 100)
     np.testing.assert_allclose(np.arange(9, 200, 20), data[:, 0, 0])
Ejemplo n.º 8
0
def on_cmd(args):
    if args.plot_reduction or args.plot or args.plot_raw:
        try:
            import matplotlib.pyplot as plt
        except:
            print(MATPLOTLIB_IMPORT_ERROR)
            return 1

    r = DataReader().open(args.filename)
    print(r.summary_string())
    start = args.start
    stop = args.stop
    if stop < 0:
        stop = r.sample_id_range[1] + 1 + stop

    if args.pretty_print:
        with open(args.filename, 'rb') as f:
            rf = DataFileReader(f)
            rf.pretty_print()

    if args.export is not None:
        k = r.samples_get(start, stop, units='samples', fields=['current', 'voltage'])
        i = k['signals']['current']['value']
        v = k['signals']['voltage']['value']
        data = np.hstack((i.reshape((-1, 1)), (v.reshape((-1, 1)))))
        if args.export.endswith('npy'):
            np.save(args.export, data)
        else:
            np.savetxt(args.export, data, fmt='%.5g', delimiter=',')

    if args.plot_reduction:
        y = r.get_reduction(start, stop)
        x = np.arange(len(y)) * (r.config['samples_per_reduction'] / r.config['sampling_frequency'])
        f = plt.figure()
        fields = r.config['reduction_fields']
        for axis, name in enumerate(fields):
            ax = f.add_subplot(len(fields), 1, axis + 1)
            ax.fill_between(x, y[:, axis]['min'], y[:, axis]['max'], color=(0.5, 0.5, 1.0, 0.5))
            ax.plot(x, y[:, axis]['mean'], color='blue')
            ax.set_ylabel(name)
            ax.grid(True)

        plt.show()
        plt.close(f)

    if args.plot:
        k = r.samples_get(start, stop, units='samples', fields=['current', 'voltage'])
        i = k['signals']['current']['value']
        v = k['signals']['voltage']['value']
        x = np.arange(len(i)) * (1.0 / r.config['sampling_frequency'])
        f = plt.figure()

        ax_i = f.add_subplot(2, 1, 1)
        ax_i.plot(x, i)
        ax_i.set_ylabel('Current (A)')
        ax_i.grid(True)

        ax_v = f.add_subplot(2, 1, 2, sharex=ax_i)
        ax_v.plot(x, v)
        ax_v.set_ylabel('Voltage (V)')
        ax_v.grid(True)

        ax_v.set_xlabel('Time (s)')

        plt.show()
        plt.close(f)

    if args.plot_raw:
        if stop - start > 2000000:
            print('Time range too long, cannot --plot-raw')
        else:
            plot_idx_total = len(args.plot_raw)
            link_axis = None
            plot_idx = 1
            rv = r.samples_get(start=start, stop=stop, units='samples', fields=['raw', 'current_range'])
            d_raw = rv['signals']['raw']['value']
            i_sel = rv['signals']['current_range']['value']
            i_raw = np.right_shift(d_raw[:, 0], 2)
            v_raw = np.right_shift(d_raw[:, 1], 2)
            x = np.arange(len(i_raw)) * (1.0 / r.config['sampling_frequency'])

            f = plt.figure()
            f.suptitle('Joulescope Raw Data')

            for c in args.plot_raw:
                if c == 'i':
                    ax = f.add_subplot(plot_idx_total, 1, plot_idx, sharex=link_axis)
                    ax.plot(x, i_raw)
                    ax.set_ylabel('Current (LSBs)')
                elif c == 'v':
                    ax = f.add_subplot(plot_idx_total, 1, plot_idx, sharex=link_axis)
                    ax.plot(x, v_raw)
                    ax.set_ylabel('Voltage (LSBs)')
                elif c == 'r':
                    ax = f.add_subplot(plot_idx_total, 1, plot_idx, sharex=link_axis)
                    ax.plot(x, i_sel)
                    ax.set_ylabel('Current Range')
                else:
                    raise ValueError('unsupported plot: %s' % c)

                ax.grid(True)
                if link_axis is None:
                    link_axis = ax
                plot_idx += 1

            # plt.tight_layout()
            ax.set_xlabel('Time (s)')
            plt.show()
            plt.close(f)

    r.close()
    return 0
Ejemplo n.º 9
0
def on_cmd(args):
    r = DataReader().open(args.filename)
    print(r.summary_string())
    start = args.start
    stop = args.stop
    if stop < 0:
        stop = r.sample_id_range[1] + 1 + stop

    if args.export is not None:
        i, v = r.get_calibrated(start, stop, units='samples')
        data = np.hstack((i.reshape((-1, 1)), (v.reshape((-1, 1)))))
        if args.export.endswith('npy'):
            np.save(args.export, data)
        else:
            np.savetxt(args.export, data, fmt='%.5g', delimiter=',')

    if args.plot_reduction:
        import matplotlib.pyplot as plt
        y = r.get_reduction(start, stop)
        x = np.arange(len(y)) * (r.config['samples_per_reduction'] / r.config['sampling_frequency'])
        f = plt.figure()
        fields = r.config['reduction_fields']
        for axis, name in enumerate(fields):
            ax = f.add_subplot(len(fields), 1, axis + 1)
            ax.plot(x, y[:, axis, 0], color='blue')
            ax.plot(x, y[:, axis, 2], color='red')
            ax.plot(x, y[:, axis, 3], color='red')
            ax.set_ylabel(name)

        plt.show()
        plt.close(f)

    if args.plot:
        import matplotlib.pyplot as plt
        i, v = r.get_calibrated(start, stop)
        x = np.arange(len(i)) * (1.0 / r.config['sampling_frequency'])
        f = plt.figure()

        ax_i = f.add_subplot(2, 1, 1)
        ax_i.plot(x, i)
        ax_i.set_ylabel('Current (A)')
        ax_i.grid(True)

        ax_v = f.add_subplot(2, 1, 2, sharex=ax_i)
        ax_v.plot(x, v)
        ax_v.set_ylabel('Voltage (V)')
        ax_v.grid(True)

        ax_v.set_xlabel('Time (s)')

        plt.show()
        plt.close(f)

    if args.plot_raw:
        import matplotlib.pyplot as plt
        if stop - start > 2000000:
            print('Time range too long, cannot --plot-raw')
        else:
            plot_idx_total = len(args.plot_raw)
            link_axis = None
            plot_idx = 1
            d_raw, d_bits, d_cal = r.raw(start=start, stop=stop)
            i_raw = np.right_shift(d_raw[:, 0], 2)
            v_raw = np.right_shift(d_raw[:, 1], 2)
            x = np.arange(len(i_raw)) * (1.0 / r.config['sampling_frequency'])

            i_sel = np.bitwise_and(d_bits, 0x000F)
            f = plt.figure()
            f.suptitle('Joulescope Raw Data')

            for c in args.plot_raw:
                if c == 'i':
                    ax = f.add_subplot(plot_idx_total, 1, plot_idx, sharex=link_axis)
                    ax.plot(x, i_raw)
                    ax.set_ylabel('Current (LSBs)')
                elif c == 'v':
                    ax = f.add_subplot(plot_idx_total, 1, plot_idx, sharex=link_axis)
                    ax.plot(x, v_raw)
                    ax.set_ylabel('Voltage (LSBs)')
                elif c == 'r':
                    ax = f.add_subplot(plot_idx_total, 1, plot_idx, sharex=link_axis)
                    ax.plot(x, i_sel)
                    ax.set_ylabel('Current Range')
                else:
                    raise ValueError('unsupported plot: %s' % c)

                ax.grid(True)
                if link_axis is None:
                    link_axis = ax
                plot_idx += 1

            # plt.tight_layout()
            ax.set_xlabel('Time (s)')
            plt.show()
            plt.close(f)

    r.close()
    return 0