def get(self, visibility): dbm = access.DBMan(LOST_CONFIG) identity = get_jwt_identity() user = dbm.get_user_by_id(identity) default_group = dbm.get_group_by_name(user.user_name) if visibility == VisLevel().USER: if not user.has_role(roles.DESIGNER): dbm.close_session() return "You are not authorized.", 401 else: root_leaves = dbm.get_all_label_trees( group_id=default_group.idx) trees = list() for root_leaf in root_leaves: trees.append( LabelTree(dbm, root_leaf.idx).to_hierarchical_dict()) dbm.close_session() return trees if visibility == VisLevel().GLOBAL: if not user.has_role(roles.ADMINISTRATOR): dbm.close_session() return "You are not authorized.", 401 else: root_leaves = dbm.get_all_label_trees(global_only=True) trees = list() for root_leaf in root_leaves: trees.append( LabelTree(dbm, root_leaf.idx).to_hierarchical_dict()) dbm.close_session() return trees dbm.close_session() return "You are not authorized.", 401
def get_voc_label_tree(dbm): tree = LabelTree(dbm) df = pd.read_csv('/code/backend/lost/pyapi/examples/label_trees/pascal_voc2012.csv') root = tree.import_df(df) if root is None: name = df[df['parent_leaf_id'].isnull()]['name'].values[0] tree = LabelTree(dbm, name=name) return tree
def test_create_delete_tree(self): dbm = DBMan(config.LOSTConfig()) tree = LabelTree(dbm) root_leaf = tree.create_root(ROOT_NAME, external_id=ROOT_EXTERNAL_ID) assert root_leaf.name == ROOT_NAME assert root_leaf.external_id == ROOT_EXTERNAL_ID assert tree.tree[root_leaf.idx] == root_leaf tree.delete_tree()
def create_label_tree(self, name, external_id=None): '''Create a new LabelTree Args: name (str): Name of the tree / name of the root leaf. external_id (str): An external id for the root leaf. Returns: :class:`lost.logic.label.LabelTree`: The created LabelTree. ''' tree = LabelTree(self._dbm) tree.create_root(name, external_id=external_id) return tree
def __label_trees(self): label_trees_json = list() for label_tree in self.available_label_trees: if len(label_tree.label_leaves) > 0: label_trees_json.append( LabelTree(self.dbm, label_tree.idx).to_hierarchical_dict()) return label_trees_json
def main(args): lostconfig = config.LOSTConfig() dbm = access.DBMan(lostconfig) if args.csv_file is not None: df = pd.read_csv(args.csv_file) name = df[df['parent_leaf_id'].isnull()]['name'].values[0] elif args.name is not None: name = args.name else: logging.error( "Either a *csv_file* or a *name* for a label tree needs to be provided!" ) return root_leaf = next( filter(lambda x: x.name == name, dbm.get_all_label_trees()), None) if root_leaf is None: logging.warning('LabelTree not present in database! {}'.format( args.csv_file)) else: try: LabelTree(dbm, root_leaf=root_leaf, logger=logging).delete_tree() logging.info('Deleted tree with name: {}'.format(name)) except: logging.error( 'Can not delete label tree. One of the labels is used by a pipeline!' ) dbm.close_session()
def get(self, label_leaf_id): dbm = access.DBMan(LOST_CONFIG) identity = get_jwt_identity() user = dbm.get_user_by_id(identity) if not user.has_role(roles.DESIGNER): dbm.close_session() return "You are not authorized.", 401 else: label_tree = LabelTree(dbm, root_id=label_leaf_id) ldf = label_tree.to_df() dbm.close_session() f = BytesIO() ldf.to_csv(f) f.seek(0) resp = make_response(f.read()) resp.headers[ "Content-Disposition"] = f"attachment; filename={label_tree.root.name}.csv" resp.headers["Content-Type"] = "blob" return resp
def test_import_df(self, tree_plus_childs): df = tree_plus_childs.to_df() df2 = df.copy() root_idx = df2[df2['parent_leaf_id'].isnull()].index.values[0] no_root_idx = df2[~df2['parent_leaf_id'].isnull()].index df2.loc[root_idx, 'name'] = 'second tree' df2.loc[root_idx, 'idx'] = 0 df2.loc[no_root_idx, 'parent_leaf_id'] = 0 dbm = DBMan(config.LOSTConfig()) tree2 = LabelTree(dbm) root_leaf = tree2.import_df(df2) if root_leaf is None: raise Exception( 'A label tree with name "{}" already exists. Clean your Test Database!' .format(df2.loc[root_idx, 'name'])) for key, val in tree2.tree.items(): print(val.to_df()[['idx', 'name', 'external_id', 'parent_leaf_id']]) for ll in tree2.root.label_leaves: assert ll.name == CHILD_COW_NAME or ll.name == CHILD_HORSE_NAME tree2.delete_tree()
def tree_plus_childs(): dbm = DBMan(config.LOSTConfig()) tree = LabelTree(dbm) root_leaf = tree.create_root(ROOT_NAME, external_id=ROOT_EXTERNAL_ID) horse = tree.create_child(tree.root.idx, CHILD_HORSE_NAME, external_id=CHILD_HORSE_EXTERNAL_ID) cow = tree.create_child(tree.root.idx, CHILD_COW_NAME, external_id=CHILD_COW_EXTERNAL_ID) yield tree # type: lost.logic.label.LabelTree tree.delete_tree()
def get(self): dbm = access.DBMan(LOST_CONFIG) identity = get_jwt_identity() user = dbm.get_user_by_id(identity) if not user.has_role(roles.DESIGNER): dbm.close_session() return "You are not authorized.", 401 else: root_leaves = dbm.get_all_label_trees() trees = list() for root_leaf in root_leaves: trees.append( LabelTree(dbm, root_leaf.idx).to_hierarchical_dict()) dbm.close_session() return trees
def get_label_tree(self, name): '''Get a LabelTree by name. Args: name (str): Name of the desired LabelTree. Retruns: :class:`lost.logic.label.LabelTree` or None: If a label tree with the given name exists it will be returned. Otherwise None will be returned''' root_list = self._dbm.get_all_label_trees() root = next(filter(lambda x: x.name == name, root_list), None) if root is None: return None else: return LabelTree(self._dbm, root_leaf=root)
#!/usr/bin/env python3 import argparse from lost.db import model, access from lost.logic import config from lost.logic.label import LabelTree import logging import os import pandas as pd logging.basicConfig(level=logging.INFO, format='(%(levelname)s): %(message)s') if __name__ == "__main__": parser = argparse.ArgumentParser( description='Import a label tree into this lost instance') parser.add_argument('csv_file', nargs='?', action='store', help='Path to the label tree in csv style.') # parser.add_argument('group_name', nargs='?', action='store', # help='Name of group that pipeline should be visible for.') args = parser.parse_args() lostconfig = config.LOSTConfig() dbm = access.DBMan(lostconfig) tree = LabelTree(dbm, logger=logging) df = pd.read_csv(args.csv_file) if tree.import_df(df) is None: logging.warning('LabelTree already present in database! {}'.format( args.csv_file)) dbm.close_session()
from lost.api.api import api from lost.api.label.api_definition import label_leaf from lost.api.label.parsers import update_label_parser, create_label_parser from lost.db import model, roles, access from lost.settings import LOST_CONFIG from lost.logic.label import LabelTree import logging logger = logging.getLogger(__name__) # Adding initial label tree if it doesn't exist dbm = access.DBMan(LOST_CONFIG) root_leaves = dbm.get_all_label_trees() foundcovid = False for root_leaf in root_leaves: if LabelTree(dbm, root_leaf.idx).to_hierarchical_dict().get('name') == 'Covid19': foundcovid = True if not foundcovid: rootlabel = model.LabelLeaf(name='Covid19', abbreviation=None, description='Covid19 dataset labels contains 5 labels', external_id=None, is_root=True) rootlabel.parent_leaf_id = None dbm.save_obj(rootlabel) for l in ['Parenchyma', 'Emphysema', 'Ground glass', 'Crazy Paving', 'Consolidation']: label = model.LabelLeaf(name=l, abbreviation=None, description=None, external_id=None, parent_leaf_id=rootlabel.idx,
import pandas as pd logging.basicConfig(level=logging.INFO, format='(%(levelname)s): %(message)s') if __name__ == "__main__": parser = argparse.ArgumentParser( description='Export a label tree to a csv file') parser.add_argument('tree_name', nargs='?', action='store', help='Name of the label tree.') parser.add_argument('out_path', nargs='?', action='store', help='Path to store the label tree in csv style.') args = parser.parse_args() lostconfig = config.LOSTConfig() dbm = access.DBMan(lostconfig) root_leaf = next( filter(lambda x: x.name == args.tree_name, dbm.get_all_label_trees()), None) if root_leaf is None: logging.warning('LabelTree "{}" not present in database!'.format( args.tree_name)) else: tree = LabelTree(dbm, root_leaf=root_leaf) df = tree.to_df() df.to_csv(args.out_path, index=False) logging.info('Exported label tree to: {}'.format(args.out_path)) dbm.close_session()
def tree(): dbm = DBMan(config.LOSTConfig()) tree = LabelTree(dbm) root_leaf = tree.create_root(ROOT_NAME, external_id=ROOT_EXTERNAL_ID) yield tree tree.delete_tree()