Esempio n. 1
0
    def test_correct_data(self, filename):
        file1 = tf.get_abs_path('chimera_1event_2levels.log')
        file2 = tf.get_abs_path('chimera_1event.log')

        concat_files([file1, file2], output_filename=filename)

        reader1 = get_reader_from_filename(file1)
        reader2 = get_reader_from_filename(file2)

        reader_out = get_reader_from_filename(filename)

        sample_rate1 = reader1.get_sample_rate()
        sample_rate2 = reader2.get_sample_rate()
        sample_rate_out = reader_out.get_sample_rate()

        self.assertEqual(sample_rate_out, sample_rate1,
                         "Unexpected sample rate. Should be {0}, was {1}.".format(sample_rate1, sample_rate_out))
        self.assertEqual(sample_rate_out, sample_rate2,
                         "Unexpected sample rate. Should be {0}, was {1}.".format(sample_rate2, sample_rate_out))

        data1 = reader1.get_all_data()[0]
        data2 = reader2.get_all_data()[0]

        data_out_should_be = np.zeros(data1.size + data2.size)
        data_out_should_be[:data1.size] = data1[:]
        data_out_should_be[data1.size:] = data2[:]

        data_out = reader_out.get_all_data()[0]

        np.testing.assert_array_equal(data_out, data_out_should_be)

        reader1.close()
        reader2.close()
        reader_out.close()
Esempio n. 2
0
    def test_set_output_filename(self, filename):
        file_names = [tf.get_abs_path('chimera_1event_2levels.log'), tf.get_abs_path('chimera_1event.log')]

        output_filename = concat_files(file_names, output_filename=filename)

        self.assertEqual(output_filename, filename)
        self.assertTrue(os.path.exists(output_filename))
Esempio n. 3
0
class TestDataFileReader(unittest.TestCase, ReaderTests):
    reader_class = DataFileReader

    default_test_data_files = [
        tf.get_abs_path('chimera_small.h5'),
        tf.get_abs_path('spheres_20140114_154938_beginning.h5')
    ]

    def _test_small_chimera_file_help(self, data_all):
        self.assertEqual(len(data_all), 1, 'Too many data channels returned.')
        data = data_all[0]
        self.assertEqual(data.size, 10, 'Wrong data size returned.')
        self.assertAlmostEqual(data[0], 17.45518, 3)
        self.assertAlmostEqual(data[9], 18.0743, 3)

    def test_get_all_data(self):
        # Make sure path to file is correct.
        filename = tf.get_abs_path('chimera_small.h5')
        reader = self.reader_class(filename)
        data = reader.get_all_data(False)

        self._test_small_chimera_file_help(data)
        reader.close()

    def help_scaling(self):
        filename = tf.get_abs_path('spheres_20140114_154938_beginning.h5')
        mean_should_be = 7.57604  # Value gotten from original MATLAB script
        std_should_be = 1.15445  # Value gotten from original MATLAB script
        return [filename], [mean_should_be], [std_should_be]

    def help_scaling_decimated(self):
        filename = tf.get_abs_path('spheres_20140114_154938_beginning.h5')
        return [filename]
Esempio n. 4
0
    def test_different_sample_rate_no_resample(self, filename):
        """
        Tests that an error is thrown if the files have different sample rates and we do not want to resample.
        """

        file_names = [tf.get_abs_path('heka_1.5s_mean5.32p_std2.76p.hkd'), tf.get_abs_path('chimera_1event.log')]

        self.assertRaises(SamplingRatesMismatchError, concat_files, file_names, output_filename=filename)
Esempio n. 5
0
    def test_open_file_from_extension(self):
        """
        Tests that the file extension is parsed correctly and the correct Reader is opened.
        """
        # Chimera file
        filename = tf.get_abs_path('spheres_20140114_154938_beginning.log')
        f = pypore.open_file(filename)
        self.assertTrue(isinstance(f, ChimeraReader))
        f.close()

        # Heka file
        filename = tf.get_abs_path('heka_1.5s_mean5.32p_std2.76p.hkd')
        f = pypore.open_file(filename)
        self.assertTrue(isinstance(f, HekaReader))
        f.close()
Esempio n. 6
0
 def test_heka_format_error_raises_binary_file(self):
     """
     Tests that trying to open a completely binary file that doesn't fit the Heka specs raises an IOError.
     """
     # Test with a complete binary file that has no text header.
     filename = tf.get_abs_path('chimera_small.log')
     self.assertRaises(IOError, self.SEGMENT_CLASS, filename)
Esempio n. 7
0
    def test_good_thresholds(self, filename):
        """
        Tests that we find the correct number of events when the starting and ending thresholds are appropriate.
        """
        data_file = tf.get_abs_path('chimera_1event.log')

        parameters = Parameters(
            threshold_strategy=AbsoluteChangeThresholdStrategy(2., 1.))

        event_databases = find_events([data_file],
                                      parameters=parameters,
                                      save_file_names=[filename],
                                      debug=True)

        h5file = ed.open_file(filename, mode='r')

        #Check the number of events
        event_count = h5file.get_event_count()
        self.assertEqual(
            event_count, 1,
            "Unexpected event count. Should be {0}, was {1}.".format(
                event_count, 0))

        #Check the event length
        sample_rate = h5file.get_sample_rate()
        event_length = h5file.get_event_row(0)['event_length'] / sample_rate
        event_length_should_be = 0.00024
        percent_diff = abs(event_length -
                           event_length_should_be) / event_length_should_be
        self.assertLessEqual(
            percent_diff, 0.05,
            "Unexpected event length. Should be {0}, was {1}.".format(
                event_length_should_be, event_length))

        h5file.close()
Esempio n. 8
0
 def test_constructor_no_mat_spec(self):
     """
     Tests that an IOError is raised when no .cfg config file is next to the .hex file.
     """
     test_no_cfg_file = tf.get_abs_path('cnp_empty.hex')
     for filename in test_no_cfg_file:
         self.assertRaises(IOError, CNP2Reader, filename)
Esempio n. 9
0
    def test_filtered_baseline(self, filename):
        """
        Tests that the filtered baseline is the same as the unfiltered.
        """
        data_filename = tf.get_abs_path('chimera_1event.log')

        reader = get_reader_from_filename(data_filename)
        data_all = reader.get_all_data()
        data = data_all[0]
        reader.close()

        baseline = np.mean(data[:150])

        # Check at different filter frequencies and re-sample rates
        for rates in ([1.e4, 1.e6], [1.e4, 0], [5.e5, 4.e6], [7.7e4, 1.e6]):
            filter_freq = rates[0]
            re_sample_rate = rates[1]

            out_filename = filter_file(data_filename, filter_frequency=filter_freq, out_sample_rate=re_sample_rate,
                                       output_filename=filename)

            reader = get_reader_from_filename(out_filename)
            data2 = reader.get_all_data()[0]
            reader.close()

            # Note we re-sampled, which is why only take 30 data points.
            baseline2 = np.mean(data2[:20])

            ratio = abs((baseline - baseline2) / baseline)
            print "ratio:", ratio
            self.assertLessEqual(ratio, 0.05, "Filtered baseline different from original. "
                                              "Should be {0}, got {1}.".format(baseline, baseline2))

            os.remove(out_filename)
Esempio n. 10
0
 def test_constructor_no_mat_spec(self):
     """
     Tests that an IOError is raised when no .cfg config file is next to the .hex file.
     """
     test_no_cfg_file = tf.get_abs_path('cnp_empty.hex')
     for filename in test_no_cfg_file:
         self.assertRaises(IOError, CNP2Reader, filename)
Esempio n. 11
0
    def test_incomplete_heka_file_raises(self):
        """
        Tests that opening a Heka file with incomplete blocks raises an IOError.
        """
        filename = tf.get_abs_path('heka_incomplete.hkd')

        self.assertRaises(IOError, self.SEGMENT_CLASS, filename)
Esempio n. 12
0
 def test_get_all_data(self):
     # Make sure path to chimera file is correct.
     filename = tf.get_abs_path('chimera_small.log')
     chimera_reader = ChimeraReader(filename)
     data = chimera_reader.get_all_data(False)
     self._test_small_chimera_file_help(data)
     chimera_reader.close()
Esempio n. 13
0
    def test_convert_chimera_file_equality(self, filename):
        """
        Test that the original/converted matrices and sample rates are the same for one-channel data.
        """
        data_filename = tf.get_abs_path('chimera_1event.log')

        output_filename = convert_file(data_filename, output_filename=filename)

        orig_reader = get_reader_from_filename(data_filename)
        orig_data_all = orig_reader.get_all_data()
        orig_sample_rate = orig_reader.get_sample_rate()

        self.assertEqual(len(orig_data_all), 1)

        out_reader = DataFileReader(output_filename)
        out_data_all = out_reader.get_all_data()
        out_sample_rate = out_reader.get_sample_rate()

        self.assertEqual(len(out_data_all), 1)

        orig_data = orig_data_all[0]
        out_data = out_data_all[0]

        # assert sample rates are equal
        self.assertAlmostEqual(1.0 * orig_sample_rate / out_sample_rate, 1, 4)

        # assert the two arrays are equal
        np.testing.assert_array_equal(orig_data, out_data)

        orig_reader.close()
        out_reader.close()
Esempio n. 14
0
    def test_good_thresholds(self, filename):
        """
        Tests that we find the correct number of events when the starting and ending thresholds are appropriate.
        """
        data_file = tf.get_abs_path('chimera_1event.log')

        parameters = Parameters(threshold_strategy=AbsoluteChangeThresholdStrategy(2., 1.))

        event_databases = find_events([data_file], parameters=parameters,
                                      save_file_names=[filename], debug=True)

        h5file = ed.open_file(filename, mode='r')

        #Check the number of events
        event_count = h5file.get_event_count()
        self.assertEqual(event_count, 1, "Unexpected event count. Should be {0}, was {1}.".format(event_count, 0))

        #Check the event length
        sample_rate = h5file.get_sample_rate()
        event_length = h5file.get_event_row(0)['event_length']/sample_rate
        event_length_should_be = 0.00024
        percent_diff = abs(event_length - event_length_should_be) / event_length_should_be
        self.assertLessEqual(percent_diff, 0.05,
                             "Unexpected event length. Should be {0}, was {1}.".format(event_length_should_be,
                                                                                       event_length))

        h5file.close()
Esempio n. 15
0
    def test_debug_option(self):
        filename = os.path.dirname(os.path.realpath(__file__))
        filename = tf.get_abs_path('chimera_nonoise_2events_1levels.log')

        output_filename = '_test_debug_option.h5'

        reader = get_reader_from_filename(filename)
        data = [reader]
        event_databases = find_events(data,
                                      save_file_names=[output_filename],
                                      debug=True)

        data = reader.get_all_data()[0]

        reader.close()

        self.assertEqual(len(event_databases), 1)

        h5file = ed.open_file(event_databases[0], mode='r')
        self._test_chimera_no_noise_2events_1levels_wrapper(h5file)

        # check that the file has the correct groups
        groups = [x._v_name for x in h5file.walk_groups()]
        self.assertIn('debug', groups, "No debug group in debug file.")

        print "data:", h5file.root.debug.data[:]
        np.testing.assert_array_equal(data, h5file.root.debug.data[0][:])

        h5file.close()

        os.remove(event_databases[0])
Esempio n. 16
0
 def test_constructor_no_mat_spec(self):
     """
     Tests that an IOError is raised when no .mat spec file is next to the .log file.
     """
     test_no_mat_chimera_files = tf.get_abs_path('chimera_empty.log')
     for filename in test_no_mat_chimera_files:
         self.assertRaises(IOError, ChimeraReader, filename)
Esempio n. 17
0
    def test_debug_option(self):
        filename = os.path.dirname(os.path.realpath(__file__))
        filename = tf.get_abs_path('chimera_nonoise_2events_1levels.log')

        output_filename = '_test_debug_option.h5'

        reader = get_reader_from_filename(filename)
        data = [reader]
        event_databases = find_events(data,
                                      save_file_names=[output_filename], debug=True)

        data = reader.get_all_data()[0]

        reader.close()

        self.assertEqual(len(event_databases), 1)

        h5file = ed.open_file(event_databases[0], mode='r')
        self._test_chimera_no_noise_2events_1levels_wrapper(h5file)

        # check that the file has the correct groups
        groups = [x._v_name for x in h5file.walk_groups()]
        self.assertIn('debug', groups, "No debug group in debug file.")

        print "data:", h5file.root.debug.data[:]
        np.testing.assert_array_equal(data, h5file.root.debug.data[0][:])

        h5file.close()

        os.remove(event_databases[0])
Esempio n. 18
0
    def test_get_all_data(self):
        # Make sure path to file is correct.
        filename = tf.get_abs_path('chimera_small.h5')
        reader = self.reader_class(filename)
        data = reader.get_all_data(False)

        self._test_small_chimera_file_help(data)
        reader.close()
Esempio n. 19
0
    def test_get_all_data(self):
        # Make sure path to file is correct.
        filename = tf.get_abs_path('chimera_small.h5')
        reader = self.reader_class(filename)
        data = reader.get_all_data(False)

        self._test_small_chimera_file_help(data)
        reader.close()
Esempio n. 20
0
    def test_multiple_files(self):
        filename1 = tf.get_abs_path('chimera_nonoise_2events_1levels.log')
        filename2 = tf.get_abs_path('chimera_nonoise_1event_2levels.log')
        file_names = [filename1, filename2]
        event_databases = find_events(file_names,
                                      save_file_names=['_testMultipleFiles_1_9238.h5', '_testMultipleFiles_2_9238.h5'])

        self.assertEqual(len(event_databases), 2)

        h5file = ed.open_file(event_databases[0], mode='r')
        self._test_chimera_no_noise_2events_1levels_wrapper(h5file)
        h5file.close()
        os.remove(event_databases[0])

        h5file = ed.open_file(event_databases[1], mode='r')
        self._test_chimera_no_noise_1event_2levels_helper(h5file)
        h5file.close()
        os.remove(event_databases[1])
Esempio n. 21
0
    def test_chimera_no_noise_1event_2levels(self):
        filename = tf.get_abs_path('chimera_nonoise_1event_2levels.log')
        event_database = find_events([filename], save_file_names=['_testChimera_nonoise_1Event_2Levels_9238.h5'])[0]

        h5file = ed.open_file(event_database, mode='r')
        self._test_chimera_no_noise_1event_2levels_helper(h5file)
        h5file.close()

        # delete the newly created event file
        os.remove(event_database)
Esempio n. 22
0
    def test_convert_file_set_output_filename(self, filename):
        """
        Tests that the output filename can be set correctly.
        """
        data_filename = tf.get_abs_path('chimera_1event.log')

        output_filename = convert_file(data_filename, output_filename=filename)

        # Test that we can set the output filename
        self.assertEqual(output_filename, filename, "output_filename not correct")
Esempio n. 23
0
    def test_default_output_filename(self):
        """
        Tests that the default output filename is correctly generated from the input file names.
        """
        file_names = [tf.get_abs_path('chimera_1event_2levels.log'), tf.get_abs_path('chimera_1event.log')]

        output_filename = concat_files(file_names)

        self.assertTrue(os.path.exists(output_filename))

        # Check that it's saved in the current directory
        self.assertEqual(output_filename[0:len('chimera_1event')], 'chimera_1event')

        # Check the correct file extension
        self.assertEqual(output_filename[-len('.h5'):], '.h5')

        self.assertIn('_concatenated_', output_filename,
                      "Default output filename ''{0}'' should contain ''_concatenated_''")

        os.remove(output_filename)
Esempio n. 24
0
    def test_chimera_no_noise_1event_2levels(self):
        filename = tf.get_abs_path('chimera_nonoise_1event_2levels.log')
        event_database = find_events(
            [filename],
            save_file_names=['_testChimera_nonoise_1Event_2Levels_9238.h5'])[0]

        h5file = ed.open_file(event_database, mode='r')
        self._test_chimera_no_noise_1event_2levels_helper(h5file)
        h5file.close()

        # delete the newly created event file
        os.remove(event_database)
Esempio n. 25
0
    def test_correct_paths(self):
        """
        Tests that the returned files are correct.
        """
        full_names = tf.get_all_file_names(with_path=True)
        short_names = tf.get_all_file_names(with_path=False)

        for i, short_name in enumerate(short_names):
            abs_path = tf.get_abs_path(short_name)
            self.assertEqual(full_names[i], abs_path,
                             msg="Absolute path not correct.\nShould be '{0}'.\nWas '{1}'.".format(full_names[i],
                                                                                                   abs_path))
Esempio n. 26
0
    def test_multiple_files(self):
        filename1 = tf.get_abs_path('chimera_nonoise_2events_1levels.log')
        filename2 = tf.get_abs_path('chimera_nonoise_1event_2levels.log')
        file_names = [filename1, filename2]
        event_databases = find_events(file_names,
                                      save_file_names=[
                                          '_testMultipleFiles_1_9238.h5',
                                          '_testMultipleFiles_2_9238.h5'
                                      ])

        self.assertEqual(len(event_databases), 2)

        h5file = ed.open_file(event_databases[0], mode='r')
        self._test_chimera_no_noise_2events_1levels_wrapper(h5file)
        h5file.close()
        os.remove(event_databases[0])

        h5file = ed.open_file(event_databases[1], mode='r')
        self._test_chimera_no_noise_1event_2levels_helper(h5file)
        h5file.close()
        os.remove(event_databases[1])
Esempio n. 27
0
    def test_open_file_with_reader_class(self):
        """
        Tests that we can pass in a Reader class to open the file with.
        """
        # Chimera file
        filename = tf.get_abs_path('spheres_20140114_154938_beginning.log')
        f = pypore.open_file(filename, ChimeraReader)
        f.close()
        self.assertTrue(isinstance(f, ChimeraReader))

        # Heka file
        filename = tf.get_abs_path('heka_1.5s_mean5.32p_std2.76p.hkd')
        f = pypore.open_file(filename)
        f.close()
        self.assertTrue(isinstance(f, HekaReader))

        # Test that passing the wrong Reader results in an error
        # Aka that the reader_class we pass in is actually used.
        filename = tf.get_abs_path('heka_1.5s_mean5.32p_std2.76p.hkd')
        # Heka files should produce an error when being ready by ChimeraReader.
        self.assertRaises(IOError, pypore.open_file, filename, ChimeraReader)
Esempio n. 28
0
    def test_original_files_unmodified(self, filename):
        file1 = tf.get_abs_path('chimera_1event_2levels.log')
        file2 = tf.get_abs_path('chimera_1event.log')

        reader1 = get_reader_from_filename(file1)
        reader2 = get_reader_from_filename(file2)

        sample_rate1_orig = reader1.get_sample_rate()
        sample_rate2_orig = reader2.get_sample_rate()

        data1_orig = reader1.get_all_data()[0]
        data2_orig = reader2.get_all_data()[0]

        reader1.close()
        reader2.close()

        concat_files([file1, file2], output_filename=filename)

        reader1 = get_reader_from_filename(file1)
        reader2 = get_reader_from_filename(file2)

        reader_out = get_reader_from_filename(filename)

        sample_rate1_final = reader1.get_sample_rate()
        sample_rate2_final = reader2.get_sample_rate()

        self.assertEqual(sample_rate1_final, sample_rate1_orig,
                         "Sample rate changed. Was {0}, now {1}.".format(sample_rate1_orig, sample_rate1_final))
        self.assertEqual(sample_rate2_final, sample_rate2_orig,
                         "Sample rate changed. Was {0}, now {1}.".format(sample_rate2_orig, sample_rate2_final))

        data1 = reader1.get_all_data()[0]
        data2 = reader2.get_all_data()[0]

        np.testing.assert_array_equal(data1, data1_orig)
        np.testing.assert_array_equal(data2, data2_orig)

        reader1.close()
        reader2.close()
        reader_out.close()
Esempio n. 29
0
    def test_chunk_size(self):
        """
        Tests that we cannot change the chunk size
        """
        filename = tf.get_abs_path('heka_1.5s_mean5.32p_std2.76p.hkd')
        reader = self.SEGMENT_CLASS(filename)

        self.assertEqual(reader.chunk_size, reader._chunk_size)

        def set_chunk(chunk):
            reader.chunk_size = chunk

        self.assertRaises(AttributeError, set_chunk, 100)
Esempio n. 30
0
class TestChimeraReader(unittest.TestCase, ReaderTests):
    reader_class = ChimeraReader

    default_test_data_files = [tf.get_abs_path('chimera_small.log'),
                               tf.get_abs_path('spheres_20140114_154938_beginning.log')]

    def test_constructor_no_mat_spec(self):
        """
        Tests that an IOError is raised when no .mat spec file is next to the .log file.
        """
        test_no_mat_chimera_files = tf.get_abs_path('chimera_empty.log')
        for filename in test_no_mat_chimera_files:
            self.assertRaises(IOError, ChimeraReader, filename)

    def test_get_all_data(self):
        # Make sure path to chimera file is correct.
        filename = tf.get_abs_path('chimera_small.log')
        chimera_reader = ChimeraReader(filename)
        data = chimera_reader.get_all_data(False)
        self._test_small_chimera_file_help(data)
        chimera_reader.close()

    def _test_small_chimera_file_help(self, data_all):
        self.assertEqual(len(data_all), 1, 'Too many data channels returned.')
        data = data_all[0]
        self.assertEqual(data.size, 10, 'Wrong data size returned.')
        self.assertAlmostEqual(data[0], 17.45518, 3)
        self.assertAlmostEqual(data[9], 18.0743, 3)

    def help_scaling(self):
        filename = tf.get_abs_path('spheres_20140114_154938_beginning.log')
        mean_should_be = 7.57604  # Value gotten from original MATLAB script
        std_should_be = 1.15445  # Value gotten from original MATLAB script
        return [filename], [mean_should_be], [std_should_be]

    def help_scaling_decimated(self):
        filename = tf.get_abs_path('spheres_20140114_154938_beginning.log')
        return [filename]
Esempio n. 31
0
    def test_set_output_sample_rate(self, filename):
        """
        Tests that we can successfully set the output sample rate, and the number of data points changes correctly.
        """
        data_file_names = [tf.get_abs_path('chimera_1event.log'),
                           tf.get_abs_path('chimera_1event_2levels.log')]

        for data_filename in data_file_names:
            # Open a reader and read the original sample rate
            orig_reader = get_reader_from_filename(data_filename)
            orig_sample_rate = orig_reader.get_sample_rate()
            orig_data = orig_reader.get_all_data()
            n_orig = orig_data[0].size
            orig_reader.close()

            for out_sample_rate in (100.e4, 1.e6, 5.e5):
                out_filename = filter_file(data_filename, 10.e4, out_sample_rate, output_filename=filename)

                # The output number of data points should be int(np.ceil(n_orig * out_sample_rate / orig_sample_rate))
                n_out = int(np.ceil(n_orig * out_sample_rate / orig_sample_rate))
                # The output sample rate should be set by n_out
                out_sample_rate = orig_sample_rate * (1.0 * n_out) / n_orig

                # Get the params from the output file
                out_reader = get_reader_from_filename(out_filename)
                o_s_r = out_reader.get_sample_rate()
                out_data = out_reader.get_all_data()
                n_o = out_data[0].size
                out_reader.close()

                self.assertEqual(n_out, n_o,
                                 "Number of re-sample points not correct. "
                                 "Original data {0}, output {1}, should be {2}.".format(n_orig, n_o, n_out))
                self.assertAlmostEqual(o_s_r, out_sample_rate, 2,
                                       "Sample rate not set correctly. Was {0}, should be {1}".format(o_s_r,
                                                                                                      out_sample_rate))

                os.remove(out_filename)
Esempio n. 32
0
    def test_correct_paths(self):
        """
        Tests that the returned files are correct.
        """
        full_names = tf.get_all_file_names(with_path=True)
        short_names = tf.get_all_file_names(with_path=False)

        for i, short_name in enumerate(short_names):
            abs_path = tf.get_abs_path(short_name)
            self.assertEqual(
                full_names[i],
                abs_path,
                msg="Absolute path not correct.\nShould be '{0}'.\nWas '{1}'.".
                format(full_names[i], abs_path))
Esempio n. 33
0
    def test_get_all_data(self):
        # Make sure path to file is correct.
        filename = tf.get_abs_path('cnp_test.hex')
        cnp_reader = CNP2Reader(filename)
        data = cnp_reader.get_all_data(False)[0]

        csv_filename = filename[:-4] + '.csv'
        csv_data = self._get_test_csv_data(csv_filename)

        ratio = np.abs((csv_data - data) / csv_data)

        np.testing.assert_array_almost_equal(ratio, np.zeros_like(data))

        cnp_reader.close()
Esempio n. 34
0
    def test_one_file_with_events(self, filename):
        """
        Tests that events are found and event databases are saved.
        """
        test_data_file_names = tf.get_all_file_names()

        test_file = tf.get_abs_path('chimera_1event.log')
        analyze_thread = AnalyzeDataThread([test_file], Parameters(), debug=True, save_file_names=[filename])
        analyze_thread.start()

        # wait until the thread has finished
        analyze_thread.wait()

        self.assertTrue(os.path.exists(filename))
Esempio n. 35
0
    def test_get_all_data(self):
        # Make sure path to file is correct.
        filename = tf.get_abs_path('cnp_test.hex')
        cnp_reader = CNP2Reader(filename)
        data = cnp_reader.get_all_data(False)[0]

        csv_filename = filename[:-4] + '.csv'
        csv_data = self._get_test_csv_data(csv_filename)

        ratio = np.abs((csv_data-data) / csv_data)

        np.testing.assert_array_almost_equal(ratio, np.zeros_like(data))

        cnp_reader.close()
Esempio n. 36
0
    def test_chimera_no_noise_2events_1levels(self):
        filename = tf.get_abs_path('chimera_nonoise_2events_1levels.log')
        event_databases = find_events([filename], save_file_names=['_testChimera_nonoise_2events_1levels_9238.h5'])

        self.assertEqual(len(event_databases), 1)

        event_database = event_databases[0]

        h5file = ed.open_file(event_database, mode='r')
        self._test_chimera_no_noise_2events_1levels_wrapper(h5file)
        h5file.close()

        # delete the newly created event file
        os.remove(event_database)
Esempio n. 37
0
    def test_set_output_name(self, filename):
        """
        Tests that setting the output filename of :func:`filter_file <pypore.file_converter.filter_file>` is correct.
        """
        data_filename = tf.get_abs_path('chimera_1event.log')

        out_filename = filter_file(data_filename, 10.e4, 100.e4, output_filename=filename)

        self.assertEqual(filename, out_filename,
                         "Output filename not set correctly. Was {0}, should be {1}".format(filename,
                                                                                            out_filename))

        # Make sure the file actually exists. Try opening it.
        f = open(out_filename)
        f.close()
Esempio n. 38
0
    def test_two_channel_channel_number(self):
        """
        """
        # TODO finish

        f = tf.get_abs_path('heka_2channel_1.3s_ch0_mean-24.84fA_rms2.09pA_ch1_mean.hkd')

        segment = self.SEGMENT_CLASS(f)

        # make sure there are 2 channels
        self.assertEqual(len(segment), 2)

        # make sure each channel has data
        self.assertGreater(len(segment[0]), 1)
        self.assertGreater(len(segment[1]), 1)
Esempio n. 39
0
    def test_saving_files(self):
        filename = tf.get_abs_path('chimera_1event.log')

        event_database = find_events([filename], save_file_names=['_testSavingFiles_9238.h5'])[0]

        self.assertTrue(os.path.isfile(event_database))

        h5file = ed.open_file(event_database, mode='r')

        self.assertTrue(h5file.isopen)

        h5file.close()

        # delete the newly created event file
        os.remove(event_database)
Esempio n. 40
0
    def test_saving_files(self):
        filename = tf.get_abs_path('chimera_1event.log')

        event_database = find_events(
            [filename], save_file_names=['_testSavingFiles_9238.h5'])[0]

        self.assertTrue(os.path.isfile(event_database))

        h5file = ed.open_file(event_database, mode='r')

        self.assertTrue(h5file.isopen)

        h5file.close()

        # delete the newly created event file
        os.remove(event_database)
Esempio n. 41
0
    def test_chimera_no_noise_2events_1levels(self):
        filename = tf.get_abs_path('chimera_nonoise_2events_1levels.log')
        event_databases = find_events(
            [filename],
            save_file_names=['_testChimera_nonoise_2events_1levels_9238.h5'])

        self.assertEqual(len(event_databases), 1)

        event_database = event_databases[0]

        h5file = ed.open_file(event_database, mode='r')
        self._test_chimera_no_noise_2events_1levels_wrapper(h5file)
        h5file.close()

        # delete the newly created event file
        os.remove(event_database)
Esempio n. 42
0
class TestChimeraReader(unittest.TestCase, ReaderTests):
    reader_class = CNP2Reader

    default_test_data_files = [tf.get_abs_path('cnp_test.hex')]

    def test_constructor_no_mat_spec(self):
        """
        Tests that an IOError is raised when no .cfg config file is next to the .hex file.
        """
        test_no_cfg_file = tf.get_abs_path('cnp_empty.hex')
        for filename in test_no_cfg_file:
            self.assertRaises(IOError, CNP2Reader, filename)

    def test_get_all_data(self):
        # Make sure path to file is correct.
        filename = tf.get_abs_path('cnp_test.hex')
        cnp_reader = CNP2Reader(filename)
        data = cnp_reader.get_all_data(False)[0]

        csv_filename = filename[:-4] + '.csv'
        csv_data = self._get_test_csv_data(csv_filename)

        ratio = np.abs((csv_data - data) / csv_data)

        np.testing.assert_array_almost_equal(ratio, np.zeros_like(data))

        cnp_reader.close()

    def _get_test_csv_data(self, csv_filename):
        data = []
        with open(csv_filename, 'rb') as csvfile:
            csvreader = csv.reader(csvfile, delimiter=',')
            # skip first row
            csvreader.next()
            for row in csvreader:
                data.append(float(row[1]))
        return np.array(data) * 1.e9  # scale the data to nA

    def help_scaling(self):
        filename = tf.get_abs_path('cnp_test.hex')
        mean_should_be = 10.363  # Value gotten from the exported CSV file
        std_should_be = 4.6866  # Value gotten from the exported CSV file
        return [filename], [mean_should_be], [std_should_be]

    def help_scaling_decimated(self):
        filename = tf.get_abs_path('cnp_test.hex')
        return [filename]
Esempio n. 43
0
    def test_one_file_with_events(self, filename):
        """
        Tests that events are found and event databases are saved.
        """
        test_data_file_names = tf.get_all_file_names()

        test_file = tf.get_abs_path('chimera_1event.log')
        analyze_thread = AnalyzeDataThread([test_file],
                                           Parameters(),
                                           debug=True,
                                           save_file_names=[filename])
        analyze_thread.start()

        # wait until the thread has finished
        analyze_thread.wait()

        self.assertTrue(os.path.exists(filename))
Esempio n. 44
0
    def test_too_large_end_threshold(self, filename):
        """
        Tests that we don't find events when the ending threshold is too large.
        """
        data_file = tf.get_abs_path('chimera_1event.log')

        parameters = Parameters(threshold_strategy=AbsoluteChangeThresholdStrategy(2., 1000.))

        event_databases = find_events([data_file], parameters=parameters,
                                      save_file_names=[filename], debug=True)

        h5file = ed.open_file(filename, mode='r')

        event_count = h5file.get_event_count()
        self.assertEqual(event_count, 0, "Unexpected event count. Should be {0}, was {1}.".format(event_count, 0))

        h5file.close()
Esempio n. 45
0
class TestHekaReader(unittest.TestCase, ReaderTests):
    reader_class = HekaReader

    default_test_data_files = [
        tf.get_abs_path('heka_1.5s_mean5.32p_std2.76p.hkd')
    ]

    def help_scaling(self):
        filename = tf.get_abs_path('heka_1.5s_mean5.32p_std2.76p.hkd')
        mean = 5.32e-12
        std_dev = 2.76e-12
        return [filename], [mean], [std_dev]

    @unittest.skip(
        "Test file is too short for decimated and un-decimated means to be equal enough."
    )
    def test_scaling_decimated(self):
        super(TestHekaReader, self).test_scaling_decimated()
Esempio n. 46
0
    def test_passing_reader(self):
        """
        Tests that passing an open subtype of :py:class:`pypore.i_o.abstract_reader.AbstractReader` works.
        """
        filename = os.path.dirname(os.path.realpath(__file__))
        filename = tf.get_abs_path('chimera_nonoise_2events_1levels.log')

        reader = get_reader_from_filename(filename)
        data = [reader]
        event_databases = find_events(
            data, save_file_names=['_test_passing_reader.h5'])

        self.assertEqual(len(event_databases), 1)

        h5file = ed.open_file(event_databases[0], mode='r')
        self._test_chimera_no_noise_2events_1levels_wrapper(h5file)
        h5file.close()
        os.remove(event_databases[0])
Esempio n. 47
0
    def test_same_sample_rate_no_change(self, filename):
        """
        Tests that if we set the output sample rate to < 0, the sampling doesn't change, but the file is filtered.
        """
        data_filename = tf.get_abs_path('chimera_1event.log')

        # Open a reader and read the original sample rate
        orig_reader = get_reader_from_filename(data_filename)
        orig_sample_rate = orig_reader.get_sample_rate()
        orig_data = orig_reader.get_all_data()
        orig_reader.close()

        for sample_rate in (-1, 0., orig_sample_rate, orig_sample_rate + 100.):
            # filter the file, with negative sample rate
            out_filename = filter_file(data_filename, 10.e4, sample_rate, output_filename=filename)

            self._test_out_sample_rate_data_len_equality(orig_data, orig_sample_rate, out_filename, sample_rate)

            os.remove(out_filename)
Esempio n. 48
0
    def test_default_output_name(self):
        """
        Tests that the default output filename of :func:`filter_file <pypore.file_converter.filter_file>` is correct.
        """
        data_filename = tf.get_abs_path('chimera_1event.log')

        output_filename_should_be = os.path.join(tf.TEST_DATA_FOLDER_PATH, 'chimera_1event_filtered.h5')

        if os.path.exists(output_filename_should_be):
            os.remove(output_filename_should_be)

        out_filename = filter_file(data_filename, 10.e4, 100.e4)

        self.assertEqual(out_filename, output_filename_should_be,
                         msg="Default filename incorrect. Was {0}, should be {1}.".format(out_filename,
                                                                                          output_filename_should_be))

        # Make sure the file actually exists. Try opening it.
        f = open(out_filename)
        f.close()

        os.remove(out_filename)