def load_file(self): for i in reversed(range(self.namespace_layout.count() - 1)): self.namespace_layout.itemAt(i).widget().setParent(None) for namespce_chk in self.namespace_chks.values(): namespce_chk.setChecked(False) self.namespaces = OrderedDict() fname = self.ui.file_name.text() f = Path(fname) try: exists = f.exists() except: exists = False if exists: try: g = GuanoFile(fname) except: msg = f"There was a problem loading the Guano MD from:\n{fname}\n\nPlease verify that it is a valid wav file" QMessageBox.warning(self, "File Error", msg) return None self.guano_content = {key: {} for key in g.get_namespaces()} for item in g.items_namespaced(): self.guano_content[item[0]][item[1]] = item[2] for namespace in g.get_namespaces(): if namespace == '': namespace = 'guano_base' namespace_fname = utils.resource_path( f"../resources/specs/{namespace}.csv") if Path(namespace_fname).exists(): spec = utils.read_namespace(namespace_fname) else: # if we have a namespace we've never seen, load it up as if it was a complete spec spec = [{ 'tag': tag } for tag in self.guano_content[namespace].keys()] this_namespace = NamespaceGroup(namespace, spec) if namespace == 'guano_base': this_namespace.load_data(self.guano_content['']) else: this_namespace.load_data(self.guano_content[namespace]) index = self.ui.scrollAreaWidgetContents_2.layout().count() - 1 self.ui.scrollAreaWidgetContents_2.layout().insertWidget( index, this_namespace) self.namespaces[namespace] = this_namespace try: self.namespace_chks[namespace].setChecked(True) except KeyError: pass
def get_row_from_guano(fname): row = get_empty_row() try: g = GuanoFile(fname) for i, keyvalue in row_lookup.iterrows(): value = g.get(keyvalue.guano_tag2, '') if value.lower() == 'nan': value = '' row[keyvalue.df_columns] = value if g.get('NABat|Site coordinates'): # print('.') lat, long = g.get('NABat|Site coordinates').split() row['latitude'] = lat row['longitude'] = long # parse the software type from vendor namespaces if 'SB' in g.get_namespaces(): software = 'Sonobat ' if g.get('SB|Version').startswith('4.2'): software += '4.2' elif g.get('SB|Version').startswith('4.'): software += '4.x' elif g.get('SB|Version').startswith('3.'): software += '3.x' row['software_type'] = software else: # TODO: add logic for Kaleidoscope pass # Make sure we're using a auto/manual ID if available if row['auto_id'] == '': row['auto_id'] = g.get('GUANO|Species Auto ID', '') if row['manual_id'] == '': row['manual_id'] = g.get('GUANO|Species Manual ID', '') # convert nan to empty string for which in ['auto', 'manual']: if row[f'{which}_id'].lower() == 'nan': row[f'{which}_id'] = '' except: # Something went dreadfully wrong. We'll populate with what we have parts = parse_nabat_fname(fname) row['grts_cell_id'] = parts['GrtsId'] row['location_name'] = parts['SiteName'] row['detector'] = "Problem extracting row from Guano" row['audio_recording_name'] = Path(fname).name return row
def test_delete_namespaced(self): """Verify that we can delete namespaced fields""" g = GuanoFile() g['Foo|Bar'] = 'xyz' self.assertTrue('Foo|Bar' in g) self.assertTrue('Foo' in g.get_namespaces()) del g['Foo|Bar'] self.assertFalse('Foo|Bar' in g) self.assertFalse('Foo' in g.get_namespaces()) try: del g['Foo|Bar'] self.fail('Deleting a deleted key should throw KeyError') except KeyError: pass g['Foo|Bar1'] = 'xyz' g['Foo|Bar2'] = 'abc' del g['Foo|Bar1'] self.assertFalse('Foo|Bar1' in g) self.assertTrue('Foo|Bar2' in g) self.assertTrue('Foo' in g.get_namespaces())