def insert_admin_account() -> None:
    """Insert default admin account."""
    user_email = '*****@*****.**'
    password = '******'
    password_hash = hash_password(password)

    with db_session() as session:
        user_exists = session.query(
            exists().where(User.email == user_email)).scalar()
        if user_exists:
            logger.warning('Admin user already exists with email "%s"',
                           user_email)
            return

        role = Role.query.filter_by(name='admin').first()
        if not role:
            logger.error(
                'Role not found! You have probably forgot to apply fixtures.')
            return

        user = User(user_email, password_hash, 'Admin', 'Medtagger')
        user.roles.append(role)
        session.add(user)
    logger.info('User added with email "%s" and password "%s"', user_email,
                password)
예제 #2
0
def add_new_user(new_user: User) -> int:
    """Add new user.

    :return: id of the new user
    """
    with db_session() as session:
        session.add(new_user)
    return new_user.id
예제 #3
0
 def add_new_label(scan_id: ScanID, user: User,
                   labeling_time: LabelingTime) -> Label:
     """Add new Label for given Scan."""
     with db_session() as session:
         label = Label(user, labeling_time)
         label.scan_id = scan_id
         session.add(label)
     return label
예제 #4
0
def get_task_by_key(key: str) -> Task:
    """Fetch Task from database.

    :param key: key for a Task
    :return: Task object
    """
    with db_session() as session:
        task = session.query(Task).filter(Task.key == key).first()
    return task
예제 #5
0
def get_dataset_by_key(key: str) -> Dataset:
    """Fetch Dataset from database.

    :param key: key for a Dataset
    :return: Dataset object
    """
    with db_session() as session:
        dataset = session.query(Dataset).filter(Dataset.key == key).one()
    return dataset
예제 #6
0
파일: scans.py 프로젝트: aiedward/MedTagger
 def reduce_number_of_declared_slices(scan_id: ScanID) -> None:
     """Decrease number of declared Slices by one."""
     with db_session() as session:
         query = session.query(Scan)
         query = query.filter(Scan.id == scan_id)
         query.update({
             "declared_number_of_slices":
             (Scan.declared_number_of_slices - 1)
         })
예제 #7
0
    def add_slice(self, orientation: SliceOrientation = SliceOrientation.Z) -> 'Slice':
        """Add new slice into this Scan.

        :return: ID of a Slice
        """
        with db_session() as session:
            new_slice = Slice(orientation)
            new_slice.scan = self
            session.add(new_slice)
        return new_slice
예제 #8
0
def unassign_label_tag(tag: LabelTag, task_key: str) -> None:
    """Unassign Label Tag from Task.

    :param tag: tag that should be unassigned from Task
    :param task_key: key that will identify such Task
    """
    with db_session():
        task = Task.query.filter(Task.key == task_key).one()
        task.available_tags.remove(tag)
        task.save()
예제 #9
0
def get_slices_by_scan_id(scan_id: ScanID, orientation: SliceOrientation = SliceOrientation.Z) -> List[Slice]:
    """Fetch Slice from database."""
    with db_session() as session:
        query = session.query(Slice)
        query = query.join(Scan)
        query = query.filter(Scan.id == scan_id)
        query = query.filter(Slice.orientation == orientation)
        query = query.order_by(Slice.location)
        slices = query.all()
    return slices
예제 #10
0
    def get_category_by_key(key: str) -> ScanCategory:
        """Fetch Scan Category from database.

        :param key: key for a Scan Category
        :return: Scan Category object
        """
        with db_session() as session:
            category = session.query(ScanCategory).filter(
                ScanCategory.key == key).one()
        return category
예제 #11
0
def assign_label_tag(tag: LabelTag, task_key: str) -> None:
    """Assign existing Label Tag to Task.

    :param tag: tag that should be assigned to Task
    :param task_key: key that will identify such Task
    """
    with db_session():
        task = Task.query.filter(Task.key == task_key).one()
        task.available_tags.append(tag)
        task.save()
예제 #12
0
def add_new_dataset(key: str, name: str) -> Dataset:
    """Add new Dataset to the database.

    :param key: key that will identify such Dataset
    :param name: name that will be used in the Use Interface for such Dataset
    :return: Dataset object
    """
    with db_session() as session:
        dataset = Dataset(key, name)
        session.add(dataset)
    return dataset
예제 #13
0
def increase_skip_count_of_a_scan(scan_id: ScanID) -> bool:
    """Increase skip_count of a Scan with given scan_id.

    :param scan_id: ID of a Scan which skip_count should be increased
    :return: boolean information whether the Scan was skipped or not
    """
    with db_session() as session:
        query = session.query(Scan)
        query = query.filter(Scan.id == scan_id)
        updated = query.update({"skip_count": (Scan.skip_count + 1)})
        return bool(updated)
예제 #14
0
    def add_new_tag(key: str, name: str) -> LabelTag:
        """Add new Label Tag to the database.

        :param key: key that will identify such Label Tag
        :param name: name that will be used in the User Interface for such Label Tag
        :return: Label Tag object
        """
        with db_session() as session:
            label_tag = LabelTag(key, name)
            session.add(label_tag)
        return label_tag
예제 #15
0
    def add_new_category(key: str, name: str, image_path: str) -> ScanCategory:
        """Add new Scan Category to the database.

        :param key: key that will identify such Scan Category
        :param name: name that will be used in the Use Interface for such Scan Category
        :param image_path: path to the image that represents such Scan Category (used in User Interface)
        :return: Scan Category object
        """
        with db_session() as session:
            category = ScanCategory(key, name, image_path)
            session.add(category)
        return category
예제 #16
0
def delete_slice(_slice: Slice) -> None:
    """Remove Slice from SQL database and Storage."""
    slice_id = _slice.id
    scan_id = _slice.scan_id

    with db_session() as session:
        query = session.query(Scan).filter(Scan.id == scan_id)
        query.update({'declared_number_of_slices': Scan.declared_number_of_slices - 1})
        session.query(Slice).filter(Slice.id == slice_id).delete()

    OriginalSlice.filter(id=slice_id).delete()
    ProcessedSlice.filter(id=slice_id).delete()
예제 #17
0
파일: scans.py 프로젝트: aiedward/MedTagger
    def add_new_scan(category: ScanCategory, number_of_slices: int,
                     user: Optional[User]) -> Scan:
        """Add new Scan to the database.

        :param category: Scan's Category object
        :param number_of_slices: number of Slices that will be uploaded
        :param user: User that uploaded scan
        :return: Scan object
        """
        with db_session() as session:
            scan = Scan(category, number_of_slices, user)
            session.add(scan)
        return scan
예제 #18
0
파일: roles.py 프로젝트: aiedward/MedTagger
 def set_user_role(user_id: int, role_name: str) -> None:
     """Set user's role. Old role will be replaced."""
     try:
         user = UsersRepository.get_user_by_id(user_id)
     except NoResultFound:
         raise InvalidArgumentsException('User with this id does not exist.')
     try:
         role = RolesRepository.get_role_with_name(role_name)
     except NoResultFound:
         raise InvalidArgumentsException('Role with this name does not exist.')
     with db_session() as session:
         user.roles = [role]
         session.add(user)
예제 #19
0
def insert_user_roles() -> None:
    """Insert default user Roles."""
    with db_session() as session:
        for row in ROLES:
            role_name = row.get('name', '')
            role_exists = session.query(
                exists().where(Role.name == role_name)).scalar()
            if role_exists:
                logger.info('Role exists with name "%s"', role_name)
                continue

            role = Role(**row)
            session.add(role)
            logger.info('Role added for name "%s"', role_name)
예제 #20
0
def insert_scan_categories() -> None:
    """Insert all default Scan Categories if don't exist."""
    with db_session() as session:
        for row in CATEGORIES:
            category_key = row.get('key', '')
            category_exists = session.query(
                exists().where(ScanCategory.key == category_key)).scalar()
            if category_exists:
                logger.info('Scan Category exists with key "%s"', category_key)
                continue

            category = ScanCategory(**row)
            session.add(category)
            logger.info('Scan Category added for key "%s"', category_key)
예제 #21
0
def add_new_scan(dataset: Dataset,
                 number_of_slices: int,
                 user: User = None) -> Scan:
    """Add new Scan to the database.

    :param dataset: Dataset object
    :param number_of_slices: number of Slices that will be uploaded
    :param user: (optional) User that uploaded scan
    :return: Scan object
    """
    with db_session() as session:
        scan = Scan(dataset, number_of_slices, user)
        session.add(scan)
    return scan
예제 #22
0
def add_new_point_label_element(label_id: LabelID, position: LabelPosition,
                                label_tag: LabelTag) -> LabelElementID:
    """Add new Point Element for given Label.

    :param label_id: Label's ID
    :param position: position (x, y, slice_index) of the Label
    :param label_tag: Label Tag object
    :return: ID of a Element
    """
    with db_session() as session:
        point_label_element = PointLabelElement(position, label_tag)
        point_label_element.label_id = label_id
        session.add(point_label_element)

    return point_label_element.id
예제 #23
0
def add_new_tag(key: str, name: str, tools: List[LabelTool],
                task_id: TaskID) -> LabelTag:
    """Add new Label Tag to the database.

    :param key: key that will identify such Label Tag
    :param name: name that will be used in the User Interface for such Label Tag
    :param tools: list of tools for given LabelTag that will be available on labeling page
    :param task_id: id of Task that owns this Label Tag
    :return: Label Tag object
    """
    with db_session() as session:
        label_tag = LabelTag(key, name, tools)
        label_tag.task_id = task_id
        session.add(label_tag)
    return label_tag
예제 #24
0
def add_action_response(action_id: ActionID, response: Dict) -> ActionResponse:
    """Add response for given Action."""
    action = Action.query.filter(Action.id == action_id).one()
    if action.action_type == 'Survey':
        valid = action.validate_response(response)
        if not valid:
            raise InvalidResponseException(
                'Your answers does not match keys in Survey.')
        with db_session() as session:
            survey_id = cast(SurveyID, action_id)
            action_response = SurveyResponse(survey_id, response)
            session.add(action_response)
    else:
        raise UnsupportedActionException(
            'Action does not support returning Respose.')
    return action_response
예제 #25
0
def _clear_databases() -> None:
    logger.info('Removing all data from PostgreSQL.')
    with db_session() as sess:
        for table in reversed(Base.metadata.sorted_tables):
            sess.execute(
                'TRUNCATE TABLE "{}" RESTART IDENTITY CASCADE;'.format(
                    table.name))
    session.close_all()

    logger.info('Removing all data from Cassandra.')
    storage_session = storage.create_session()
    storage_session.set_keyspace(storage.MEDTAGGER_KEYSPACE)
    for model_name in dir(models):
        model = getattr(models, model_name)
        if issubclass(model.__class__,
                      ModelMetaClass) and model.__table_name__:
            storage_session.execute('TRUNCATE {}'.format(model.__table_name__))
예제 #26
0
    def add_new_rectangular_label_element(
            label_id: LabelID, position: LabelPosition, shape: LabelShape,
            label_tag: LabelTag) -> LabelElementID:
        """Add new Rectangular Element for given Label.

        :param label_id: Label's ID
        :param position: position (x, y, slice_index) of the Label
        :param shape: shape (width, height) of the Label
        :param label_tag: Label Tag object
        :return: ID of a Element
        """
        with db_session() as session:
            rectangular_label_element = RectangularLabelElement(
                position, shape, label_tag)
            rectangular_label_element.label_id = label_id
            session.add(rectangular_label_element)

        return rectangular_label_element.id
예제 #27
0
def add_task(key: str, name: str, image_path: str, datasets_keys: List[str],
             tags: List[LabelTag]) -> Task:
    """Add new Task to the database.

    :param key: key that will identify such Task
    :param name: name that will be used in the Use Interface for such Task
    :param image_path: path to the image that represents such Task (used in User Interface)
    :param datasets_keys: Keys of Datasets that Task takes Scans from
    :param tags: Label Tags that will be created and assigned to Task
    :return: Task object
    """
    with db_session() as session:
        task = Task(key, name, image_path)
        datasets = Dataset.query.filter(
            Dataset.key.in_(datasets_keys)).all()  # type: ignore
        task.datasets = datasets
        task.available_tags = tags
        session.add(task)
    return task
예제 #28
0
    def add_new_brush_label_element(label_id: LabelID, slice_index: int,
                                    width: int, height: int, image: bytes,
                                    label_tag: LabelTag) -> LabelElementID:
        """Add new Brush Element for given Label.

        :param label_id: Label's ID
        :param width: width of the Label's image
        :param height: height of the Label's image
        :param image: bytes with image representation of a binary mask
        :param label_tag: Label Tag object
        :return: ID of a Element
        """  # pylint: disable=too-many-arguments
        with db_session() as session:
            brush_label_element = BrushLabelElement(slice_index, width, height,
                                                    label_tag)
            brush_label_element.label_id = label_id
            session.add(brush_label_element)
        BrushLabelElementStorage.create(id=brush_label_element.id, image=image)
        return brush_label_element.id
예제 #29
0
def insert_labels_tags() -> None:
    """Insert all default Label Tags if they don't exist and assign them to category."""
    with db_session() as session:
        for row in TAGS:
            tag_key = row.get('key', '')
            tag_exists = session.query(
                exists().where(LabelTag.key == tag_key)).scalar()
            if tag_exists:
                logger.info('Label Tag exists with key "%s"', tag_key)
                continue

            tag = LabelTag(row.get('key', ''), row.get('name', ''))
            tag_category_key = row.get('category_key', '')
            category = session.query(ScanCategory).filter(
                ScanCategory.key == tag_category_key).one()
            tag.scan_category_id = category.id
            session.add(tag)
            logger.info(
                'Label Tag added for key "%s" and assigned to category for key "%s"',
                tag_key, tag_category_key)
예제 #30
0
def update(key: str,
           name: str = None,
           tools: List[LabelTool] = None,
           task_id: TaskID = None) -> LabelTag:
    """Update Tools that are available in Label Tag.

    :param key: key that will identify such Label Tag
    :param name: (optional) new name for such Label Tag
    :param tools: (optional) list of tools for given LabelTag that will be available on labeling page
    :param task_id: (optional) Task ID for another Task which should be linked to this Label Tag
    :return: Label Tag object
    """
    with db_session() as session:
        label_tag = get_label_tag_by_key(key)
        if name:
            label_tag.name = name
        if tools:
            label_tag.tools = tools
        if task_id:
            label_tag.task_id = task_id
        session.add(label_tag)
    return label_tag