def test_002_write_events(self):
     fname = os.path.join(self.save_path, 'test.fast5')
     with Fast5File(fname, 'w') as fh:
         fh.add_channel_info({'channel_number': 1, 'sampling_rate': 4000})
         fh.add_read(12, 'unique_snowflake', 12345, 111, 0, 120.75)
         with EventDetectionTools(fh,
                                  group_name='EventDetection_000',
                                  meta={
                                      'name': 'test',
                                      'version': '0.1.0'
                                  }) as evdet:
             data = np.zeros(100,
                             dtype=[('start', int), ('length', int),
                                    ('mean', float), ('stdv', float)])
             read_attrs = {'read_number': 12}
             evdet.set_event_data(data, read_attrs)
     with Fast5File(fname, 'r') as fh:
         self.assertEqual(1, len(fh.status.read_info))
         read_info = fh.status.read_info[0]
         self.assertEqual(12, read_info.read_number)
         group = fh.get_latest_analysis('EventDetection')
         self.assertEqual('EventDetection_000', group)
         with EventDetectionTools(fh) as evdet:
             self.assertTrue(evdet.has_event_data())
             data, attrs = evdet.get_event_data()
             self.assertDictEqual(
                 {
                     u'read_number': 12,
                     u'read_id': 'unique_snowflake',
                     u'start_time': 12345,
                     u'duration': 111,
                     u'start_mux': 0,
                     u'median_before': 120.75
                 }, attrs)
             self.assertEqual(100, data.size)
 def get_event_data(self, section, time_in_seconds=False):
     """ Get the template or complement event data.
     
     :param section: Either template, complement, or both.
     :param time_in_seconds: Return the start and length fields
         in seconds, rather than samples.
     :return: The event dataset for the section. If section=both
         then it returns a tuple with both sections. Returns None
         if the section does not exist.
     """
     if not section in ['template', 'complement', 'both']:
         raise Exception('Unrecognized value of section argument.')
     results = self.get_results()
     if results is None:
         return None, None if section is 'both' else None
     if section == 'both':
         sections = ['template', 'complement']
     else:
         sections = [section]
     evdet_group, _ = self._find_event_data()
     with EventDetectionTools(self.handle, group_name=evdet_group) as evdet:
         event_data, _ = evdet.get_event_data(time_in_seconds=time_in_seconds)
     datasets = [None, None]
     for n, this_section in enumerate(sections):
         if not results['has_{}'.format(this_section)]:
             continue
         ev1 = results['start_event_{}'.format(this_section)]
         ev2 = results['end_event_{}'.format(this_section)]
         datasets[n] = event_data[ev1:ev2]
     if section == 'both':
         return tuple(datasets)
     return datasets[0]
 def test_002_events_only(self):
     fname = self.generate_temp_filename()
     with Fast5File(fname, mode='w') as fh:
         fh.add_channel_info({
             'channel_number': 1,
             'sampling_rate': 4000,
             'digitisation': 8192,
             'range': 819.2,
             'offset': 0
         })
         fh.add_read(12, 'unique_snowflake', 10000, 1000, 0, 120.75)
         with EventDetectionTools(fh,
                                  group_name='EventDetection_000',
                                  meta={'name': 'test'}) as evdet:
             data = np.zeros(100,
                             dtype=[('start', int), ('length', int),
                                    ('mean', float), ('stdv', float)])
             data['start'][2] = 10010
             data['start'][46] = 10470
             data['length'][46] = 10
             data['start'][53] = 10520
             data['start'][97] = 10960
             data['length'][97] = 20
             read_attrs = {'read_number': 12}
             evdet.set_event_data(data, read_attrs)
         attrs = {
             'name': 'test',
             'version': 0,
             'time_stamp': 'just now',
             'event_detection': 'Analyses/EventDetection_000'
         }
         fh.add_analysis('segmentation', 'Segmentation_000', attrs)
         segment_data = {
             'has_template': 1,
             'has_complement': 1,
             'start_event_template': 2,
             'end_event_template': 47,
             'start_event_complement': 53,
             'end_event_complement': 98
         }
         fh.set_summary_data('Segmentation_000', 'segmentation',
                             segment_data)
         with SegmentationTools(fh,
                                group_name='Segmentation_000') as segment:
             results = segment.get_results()
             self.assertDictEqual(
                 {
                     'has_template': True,
                     'has_complement': True,
                     'start_event_template': 2,
                     'end_event_template': 47,
                     'start_event_complement': 53,
                     'end_event_complement': 98,
                     'first_sample_template': 10,
                     'duration_template': 470,
                     'first_sample_complement': 520,
                     'duration_complement': 460
                 }, results)
 def test_001_read_events(self):
     # Check that it is recognized properly.
     fname = os.path.join(test_data, 'read_file_v1.0_single.fast5')
     with EventDetectionTools(fname, mode='r', ) as fh:
         self.assertTrue(fh.has_event_data)
         self.assertTrue(fh.has_event_data(read_number=59))
         self.assertEqual('EventDetection_000', fh.group_name)
         data, attrs = fh.get_event_data()
         self.assertDictEqual({'read_number': 59,
                               'strand_id': 60,
                               'start_mux': 1,
                               'end_mux': 1,
                               'start_time': 32463855,
                               'duration': 729468}, attrs)
         self.assertEqual(7875, data.size)
         self.assertEqual(118, data[0]['length'])
         data, attrs = fh.get_event_data(time_in_seconds=True)
         self.assertEqual(0.0236, data[0]['length'])