def _load_item_data(self):
     """ """
     self.dir_path = app_core.DesktopAppSync().get_workspace()
     self.survey_name = app_core.DesktopAppSync().get_selected_survey()
     #
     try:
         self._items_model.clear()
         h5_wavefiles = hdf54bats.Hdf5Wavefiles(self.dir_path,
                                                self.survey_name)
         from_top_node = ''
         nodes = h5_wavefiles.get_child_nodes(from_top_node)
         for node_key in sorted(nodes):
             node_dict = nodes.get(node_key, {})
             node_item_id = node_dict.get('item_id', '')
             node_item_type = node_dict.get('item_type', '')
             if node_item_type == 'wavefile':
                 item = QtGui.QStandardItem(node_item_id)
                 item.setCheckState(QtCore.Qt.Unchecked)
                 item.setCheckable(True)
                 self._items_model.appendRow(item)
     except Exception as e:
         debug_info = self.__class__.__name__ + ', row  ' + str(
             sys._getframe().f_lineno)
         app_utils.Logging().error('Exception: (' + debug_info + '): ' +
                                   str(e))
    def _load_item_data(self):
        """ """
        try:
            self._items_model.clear()
            h5_wavefiles = hdf54bats.Hdf5Wavefiles(self.dir_path,
                                                   self.survey_name)
            from_top_node = ''
            nodes = h5_wavefiles.get_child_nodes(from_top_node)
            for node_key in sorted(nodes):
                node_dict = nodes.get(node_key, {})
                node_item_id = node_dict.get('item_id', '')
                node_item_type = node_dict.get('item_type', '')
                if node_item_type == 'wavefile':
                    item = QtGui.QStandardItem(node_item_id)
                    item.setCheckState(QtCore.Qt.Unchecked)
                    item.setCheckable(True)
                    self._items_model.appendRow(item)

#             nodes = h5wavefile.get_child_nodes(item_id)
#             for node_key in sorted(nodes):
#                 node_dict = nodes.get(node_key, {})
#                 node_row = []
#                 node_item_id = node_dict.get('item_id', '')
#                 node_item_type = node_dict.get('item_type', '')
#                 node_row.append(node_item_id)
#                 node_row.append(node_item_type)
#                 node_row.append(node_dict.get('item_title', ''))
#                 dataset_table.append_row(node_row)

#             self._items_model.clear()
#             wave = hdf54bats.Hdf5Wavefiles(self.dir_path, self.survey_name)
#
#             from_top_node = ''
#             wavefiles_dict = wave.get_wavefiles(from_top_node)
#
#             for wave_id in sorted(wavefiles_dict):
# #                 self.groupid_combo.addItem(event_group)
#                 item = QtGui.QStandardItem(wave_id)
#                 item.setCheckState(QtCore.Qt.Unchecked)
#                 item.setCheckable(True)
#                 self._items_model.appendRow(item)
#
        except Exception as e:
            debug_info = self.__class__.__name__ + ', row  ' + str(
                sys._getframe().f_lineno)
            app_utils.Logging().error('Exception: (' + debug_info + '): ' +
                                      str(e))
 def _delete_marked_items(self):
     """ """
     try:
         try:
             h5_wavefiles = hdf54bats.Hdf5Wavefiles(self.dir_path,
                                                    self.survey_name)
             for rowindex in range(self._items_model.rowCount()):
                 item = self._items_model.item(rowindex, 0)
                 if item.checkState() == QtCore.Qt.Checked:
                     item_id = str(item.text())
                     h5_wavefiles.remove_wavefile(item_id, recursive=True)
         finally:
             self.accept()  # Close dialog box.
     #
     except Exception as e:
         debug_info = self.__class__.__name__ + ', row  ' + str(
             sys._getframe().f_lineno)
         app_utils.Logging().error('Exception: (' + debug_info + '): ' +
                                   str(e))
Beispiel #4
0
 def update_metadata_dict(self):
     """ """
     self.metadata_dict = {}
     if self.workspace and self.survey:
         try:
             if not pathlib.Path(self.workspace).exists():
                 return
             if not pathlib.Path(self.workspace, self.survey).exists():
                 return
             #
             h5_wavefiles = hdf54bats.Hdf5Wavefiles(self.workspace,
                                                    self.survey)
             self.metadata_dict = h5_wavefiles.get_user_metadata(
                 self.item_id)
             self.metadata_dict['item_title'] = h5_wavefiles.get_item_title(
                 self.item_id)
         except Exception as e:
             debug_info = self.__class__.__name__ + ', row  ' + str(
                 sys._getframe().f_lineno)
             app_utils.Logging().error('Exception: (' + debug_info + '): ' +
                                       str(e))
    def scan_files(self):
        """ """
        try:
            lowfreqfilter = str(self.lowfreqfilter_edit.text()).replace(
                ',', '.')
            highfreqfilter = str(self.highfreqfilter_edit.text()).replace(
                ',', '.')
            amplevel = str(self.amplevel_edit.text()).replace(',', '.')
            params = {}
            params['low_frequency_hz'] = float(lowfreqfilter) * 1000.0
            if self.usehighlimit_checkbox.isChecked():
                params['high_frequency_hz'] = float(highfreqfilter) * 1000.0
            params['min_amp_level_dbfs'] = float(amplevel)
            params[
                'min_amp_level_relative'] = self.amplevel_checkbox.isChecked()
            #
            h5_wavefiles = hdf54bats.Hdf5Wavefiles(self.dir_path,
                                                   self.survey_name)
            item_id_list = []
            for rowindex in range(self._items_model.rowCount()):
                item = self._items_model.item(rowindex, 0)
                if item.checkState() == QtCore.Qt.Checked:

                    item_id = str(item.text())
                    if not h5_wavefiles.exists(item_id, 'wavefile_peaks'):
                        item_id_list.append(item_id)

            params['item_id_list'] = item_id_list
            #
            scanner = app_core.WaveFileScanner()
            scanner.scan_files_in_thread(params)
        #
        except Exception as e:
            debug_info = self.__class__.__name__ + ', row  ' + str(
                sys._getframe().f_lineno)
            app_utils.Logging().error('Exception: (' + debug_info + '): ' +
                                      str(e))
    def create_spectrogram(self, workspace, survey, item_id):
        """ """
        try:
            if not item_id:
                self.axes.cla()
                self._canvas.draw()
                return
            #
            h5_wavefiles = hdf54bats.Hdf5Wavefiles(workspace, survey)
            signal = h5_wavefiles.get_wavefile(wavefile_id=item_id)
            item_metadata = h5_wavefiles.get_user_metadata(item_id)
            #
            sampling_freq_hz = item_metadata.get('rec_frame_rate_hz', '')
            if not sampling_freq_hz:
                sampling_freq_hz = item_metadata.get('frame_rate_hz', '')
            if not sampling_freq_hz:
                sampling_freq_hz = item_metadata.get('rec_framerate', '')
            if not sampling_freq_hz:
                sampling_freq = 500000 # Correct for D500X.
            else:
                sampling_freq = int(sampling_freq_hz)
            #
            if len(signal) > (10 * sampling_freq):
                signal = signal[0:10 * sampling_freq]
#                 print('DEBUG: Warning: Signal truncated to 10 sec.')
                app_utils.Logging().warning('Spectrogram: Signal truncated to 10 sec.')
            # Settings.
            window_size = int(self.windowsize_combo.currentText())
            timeresolution = self.timeresolution_combo.currentText()
            window_function = 'hann'
            jump = int(sampling_freq / 1000)
            if timeresolution == '2 ms':
                window_function = 'hann'
                jump = int(sampling_freq / 500)
            elif timeresolution == '1 ms':
                window_function = 'hann'
                jump = int(sampling_freq / 1000) # 1 ms.
            elif timeresolution == '1/2 ms':
                window_function = 'hann'
                jump = int(sampling_freq / 2000)
            elif timeresolution == '1/4 ms':
                window_function = 'blackh'
                jump = int(sampling_freq / 4000)
            elif timeresolution == '1/8 ms':
                window_function = 'kaiser'
                jump = int(sampling_freq / 8000)
            #
            viewpart = self.viewpart_combo.currentText()
            if viewpart == 'All':
                pass
            elif viewpart == '0-1 s':
                signal = signal[sampling_freq*0:sampling_freq*1]
            elif viewpart == '1-2 s':
                signal = signal[sampling_freq*1:sampling_freq*2]
            elif viewpart == '2-3 s':
                signal = signal[sampling_freq*2:sampling_freq*3]
            elif viewpart == '3-4 s':
                signal = signal[sampling_freq*3:sampling_freq*4]
            elif viewpart == '4-5 s':
                signal = signal[sampling_freq*4:sampling_freq*5]
            elif viewpart == '5-6 s':
                signal = signal[sampling_freq*5:sampling_freq*6]
            elif viewpart == '6-7 s':
                signal = signal[sampling_freq*6:sampling_freq*7]
            elif viewpart == '7-8 s':
                signal = signal[sampling_freq*7:sampling_freq*8]
            elif viewpart == '8-9 s':
                signal = signal[sampling_freq*8:sampling_freq*9]
            elif viewpart == '9-10 s':
                signal = signal[sampling_freq*9:sampling_freq*10]
            pos_in_sec_from = 0.0
            pos_in_sec_to = len(signal) / sampling_freq
            # Cut part from from the whole signal.
            signal_short = signal[int(pos_in_sec_from * sampling_freq):int(pos_in_sec_to * sampling_freq)]
            # Create util.
            dbsf_util = dsp4bats.DbfsSpectrumUtil(window_size=window_size, 
                                                  window_function=window_function)
            # Create matrix.
            ### jump = int(sampling_freq/1000/jumps_per_ms)
            size = int(len(signal_short) / jump)
            matrix = dbsf_util.calc_dbfs_matrix(signal_short, matrix_size=size, jump=jump)
            # Plot.
            max_freq = sampling_freq / 1000 / 2 # kHz and Nyquist.
            
            # Plot.            
            self.axes.cla()
            #
            self.axes.imshow(matrix.T, 
                      cmap='viridis', 
                      origin='lower',
                      extent=(pos_in_sec_from, pos_in_sec_to, 
                              0, max_freq)
                     )
            self.axes.axis('tight')
#             self.axes.set_title(item_title)
            self.axes.set_ylabel('Frequency (kHz)')
            self.axes.set_xlabel('Time (s)')
            #ax.set_ylim([0,160])
            #
            self._canvas.draw()
            
        except Exception as e:
            debug_info = self.__class__.__name__ + ', row  ' + str(sys._getframe().f_lineno)
            app_utils.Logging().error('Exception: (' + debug_info + '): ' + str(e))
Beispiel #7
0
    def _scan_files(self, item_id_list, low_freq_hz, high_freq_hz,
                    min_amp_level_dbfs, min_amp_level_relative):
        """ """
        workspace = app_core.DesktopAppSync().get_workspace()
        survey = app_core.DesktopAppSync().get_selected_survey()
        h5_wavefiles = hdf54bats.Hdf5Wavefiles(workspace, survey)

        print('DEBUG: Scanner', ' low freq: ', low_freq_hz, ' high freq: ',
              high_freq_hz, ' min dbfs: ', min_amp_level_dbfs, ' relative: ',
              min_amp_level_relative)

        counter = 0
        counter_max = len(item_id_list)
        for item_id in item_id_list:

            app_utils.Logging().info('Scanning: ' + item_id)
            counter += 1
            print('DEBUG: Scanning ', counter, ' (', counter_max, ')   ',
                  item_id)

            item_metadata = h5_wavefiles.get_user_metadata(item_id)
            signal = h5_wavefiles.get_wavefile(wavefile_id=item_id)

            sampling_freq_hz = item_metadata.get('rec_frame_rate_hz', '')
            sampling_freq_hz = int(sampling_freq_hz)

            signal = signal / 32767  # To interval -1.0 to 1.0

            extractor = sound4bats.PulsePeaksExtractor(debug=True)
            extractor.setup(sampling_freq_hz=sampling_freq_hz)
            signal_filtered = extractor.filter(signal,
                                               filter_low_hz=low_freq_hz,
                                               filter_high_hz=high_freq_hz)
            extractor.new_result_table()
            extractor.extract_peaks(
                signal_filtered,
                min_amp_level_dbfs=min_amp_level_dbfs,
                min_amp_level_relative=min_amp_level_relative)

            result_table = extractor.get_result_table()

            print('DEBUG: TABLE len:', len(result_table), ' for item_id: ',
                  item_id)

            h5_wavefiles.add_wavefile_peaks(item_id, result_table)

#             extractor.save_result_table(file_path='debug_wavefile_peaks' + item_id + '.txt')

#         # Plot.
#         import matplotlib.pyplot
#
#         time = []
#         freq = []
#         amp = []
#         for row in extractor.get_result_table():
#             if row[0] == 1:
#                 if row[3] > -100: # -100 means silent.
#                     time.append(float(row[1]))
#                     freq.append(float(row[2]))
#                     amp.append(float(row[3]))
#         #
#         amp_min = abs(min(amp))
#         sizes = [((x+amp_min)**1.2) * 0.1 for x in amp]
#
#     #     matplotlib.pyplot.scatter(time, freq, c=sizes, s=sizes, cmap='Blues')
# #         matplotlib.pyplot.scatter(time, freq, c=amp, s=sizes, cmap='Reds')
# #        matplotlib.pyplot.scatter(time, freq, c=amp, s=0.5, cmap='Reds') #, origin='lower')
#         matplotlib.pyplot.scatter(time, freq, s=0.5 )
#         matplotlib.pyplot.show()

        return  #####################################################
    def _import_wavefiles(self):
        """ """
        try:
            try:
                detectorgroup = self.detector_combo.currentText()
                self._parentwidget._write_to_status_bar(
                    '- Busy: Importing wave files.')

                h5_wavefiles = hdf54bats.Hdf5Wavefiles(self.dir_path,
                                                       self.survey_name)
                #                 wurb_utils = dsp4bats.WurbFileUtils()

                for rowindex in range(self._items_model.rowCount()):
                    try:
                        item = self._items_model.item(rowindex, 0)
                        if item.checkState() == QtCore.Qt.Checked:
                            wave_file_path = str(item.text())
                            #                     wave.remove_wavefile(item_id)

                            wave_reader = None
                            try:
                                #
                                detector_type = self.detectortype_combo.currentText(
                                )
                                if detector_type == 'CloudedBats-WURB/Pathfinder':
                                    metadata_reader = metadata4bats.MetadataWavefileWurb(
                                        wave_file_path)
                                elif detector_type == 'Generic':
                                    metadata_reader = metadata4bats.MetadataWavefile(
                                        wave_file_path)
                                elif detector_type == '(Generic GUANO)':
                                    metadata_reader = metadata4bats.MetadataWavefile(
                                        wave_file_path)
                                elif detector_type == 'AudioMoth version 1.0':
                                    metadata_reader = metadata4bats.MetadataWavefileAudiomoth(
                                        wave_file_path)
                                elif detector_type == '(AudioMoth version 1.2)':
                                    metadata_reader = metadata4bats.MetadataWavefileAudiomoth(
                                        wave_file_path)
                                elif detector_type == '(Pettersson-M500X)':
                                    metadata_reader = metadata4bats.MetadataWavefile(
                                        wave_file_path)
                                else:
                                    metadata_reader = metadata4bats.MetadataWavefile(
                                        wave_file_path)
                                #
                                metadata_reader.extract_metadata()
                                metadata = metadata_reader.get_metadata()
                                #
                                file_name = metadata['rec_file_name']
                                title = file_name
                                if 'rec_datetime_local' in metadata:
                                    datetime_str = metadata[
                                        'rec_datetime_local'][0:15]
                                    name = 'wave_' + hdf54bats.str_to_ascii(
                                        datetime_str)
                                else:
                                    name = metadata['rec_file_stem'].lower()

                                if self.location_combo.currentText(
                                ) == 'Get from wave files':
                                    pass  # Already done.
                                elif self.location_combo.currentText(
                                ) == 'Enter manually':
                                    metadata[
                                        'rec_latitude_dd'] = self.latitude_dd_edit.text(
                                        )
                                    metadata[
                                        'rec_longitude_dd'] = self.logitude_dd_edit.text(
                                        )
                                elif self.location_combo.currentText(
                                ) == 'Unknown location':
                                    metadata['rec_latitude_dd'] = ''
                                    metadata['rec_longitude_dd'] = ''

                                name = hdf54bats.str_to_ascii(name)
                                self._parentwidget._write_to_status_bar(
                                    '- Busy: Importing: ' + file_name)

                                app_utils.Logging().info(
                                    'Importing wavefile: ' + title)
                                QtWidgets.QApplication.processEvents()

                                wave_reader = dsp4bats.WaveFileReader(
                                    wave_file_path)
                                signal = np.array([], dtype=np.int16)
                                buffer = wave_reader.read_buffer(
                                    convert_to_float=False)
                                while len(buffer) > 0:
                                    signal = np.append(signal, buffer)
                                    buffer = wave_reader.read_buffer(
                                        convert_to_float=False)
                            finally:
                                if wave_reader:
                                    wave_reader.close()

                            wavefile_id = h5_wavefiles.add_wavefile(
                                parent_id=detectorgroup,
                                node_id=name,
                                title=title,
                                array=signal)
                            h5_wavefiles.set_user_metadata(wavefile_id,
                                                           metadata=metadata)
                    #
                    except Exception as e:
                        debug_info = self.__class__.__name__ + ', row  ' + str(
                            sys._getframe().f_lineno)
                        app_utils.Logging().error('Exception: (' + debug_info +
                                                  '): ' + str(e))
            finally:
                self._parentwidget._write_to_status_bar('')
                self.accept()  # Close dialog box.
        #
        except Exception as e:
            debug_info = self.__class__.__name__ + ', row  ' + str(
                sys._getframe().f_lineno)
            app_utils.Logging().error('Exception: (' + debug_info + '): ' +
                                      str(e))