SPPAS 4.22

https://sppas.org/

Module sppas.src.annotations

Class sppasParam

Description

Annotation parameters manager.

Parameters of a set of annotations.

Constructor

Create a new sppasParam instance with default values.

Parameters
  • annotation_keys: (list) List of annotations to load. None=ALL.
View Source
def __init__(self, annotation_keys=None):
    """Create a new sppasParam instance with default values.

    :param annotation_keys: (list) List of annotations to load. None=ALL.

    """
    self._report = None
    self._workspace = sppasWorkspace()
    self.annotations = []
    self.load_annotations(annotation_keys)
    self._out_extensions = dict()
    for filetype in sppasFiles.OUT_FORMATS:
        self._out_extensions[filetype] = sppasFiles.DEFAULT_EXTENSIONS[filetype]

Public functions

load_annotations

Load the annotation configuration files.

Load from a list of given file names (without path) or from the default sppas ui configuration file.

Parameters
  • annotation_files: (list) List of annotations to load. None=ALL.
View Source
def load_annotations(self, annotation_files=None):
    """Load the annotation configuration files.

        Load from a list of given file names (without path) or from the
        default sppas ui configuration file.

        :param annotation_files: (list) List of annotations to load. None=ALL.

        """
    if not annotation_files or len(annotation_files) == 0:
        self.parse_config_file()
    else:
        for cfg_file in annotation_files:
            self.__load(os.path.join(paths.etc, cfg_file))

parse_config_file

Parse the sppasui.json file.

Parse the file to get the list of annotations and parse the corresponding "json" file.

View Source
def parse_config_file(self):
    """Parse the sppasui.json file.

        Parse the file to get the list of annotations and parse the
        corresponding "json" file.

        """
    config = os.path.join(paths.etc, 'sppasui.json')
    if os.path.exists(config) is False:
        raise IOError('Installation error: the file to configure the automatic annotations does not exist.')
    with open(config) as cfg:
        dict_cfg = json.load(cfg)
    for ann in dict_cfg['annotate']:
        self.__load(os.path.join(paths.etc, ann['config']))

get_workspace

Return the workspace.

View Source
def get_workspace(self):
    """Return the workspace."""
    return self._workspace

set_workspace

View Source
def set_workspace(self, wkp):
    if isinstance(wkp, sppasWorkspace) is False:
        raise sppasTypeError('sppasWorkspace', type(wkp))
    logging.debug('New data to set in sppasParam. Id={:s}'.format(wkp.id))
    self._workspace = wkp

add_to_workspace

Add a list of files or directories into the workspace.

The state of all the added files is set to CHECKED.

Parameters
  • files: (str or list of str)
View Source
def add_to_workspace(self, files):
    """Add a list of files or directories into the workspace.

        The state of all the added files is set to CHECKED.

        :param files: (str or list of str)

        """
    if isinstance(files, list) is False:
        try:
            if os.path.isfile(files):
                objs = self._workspace.add_file(files)
                if objs is not None:
                    for obj in objs:
                        self._workspace.set_object_state(States().CHECKED, obj)
                obj = self._workspace.get_object(files)
                self._workspace.set_object_state(States().CHECKED, obj)
            elif os.path.isdir(files):
                for f in os.listdir(files):
                    self.add_to_workspace(os.path.join(files, f))
            else:
                raise sppasIOError(files)
        except Exception as e:
            logging.error('File {!s:s} not added into the workspace: {:s}'.format(files, str(e)))
    else:
        for f in files:
            self.add_to_workspace(f)

get_checked_roots

Return the list of entries to annotate.

View Source
def get_checked_roots(self):
    """Return the list of entries to annotate."""
    roots = self._workspace.get_fileroot_from_state(States().CHECKED) + self._workspace.get_fileroot_from_state(States().AT_LEAST_ONE_CHECKED)
    return [r.id for r in roots]

set_report_filename

Fix the name of the file to save the report of the annotations.

Parameters
  • filename: (str) Filename for the Procedure Outcome Report
View Source
def set_report_filename(self, filename):
    """Fix the name of the file to save the report of the annotations.

        :param filename: (str) Filename for the Procedure Outcome Report

        """
    self._report = filename

get_report_filename

Return the name of the file for the Procedure Outcome Report.

View Source
def get_report_filename(self):
    """Return the name of the file for the Procedure Outcome Report."""
    return self._report

set_lang

View Source
def set_lang(self, language, step=None):
    if step is not None:
        self.annotations[step].set_lang(language)
    else:
        for a in self.annotations:
            a.set_lang(language)

get_lang

View Source
def get_lang(self, step=None):
    if step is None:
        for a in self.annotations:
            lang = a.get_lang()
            if lang is not None and lang != annots.UNDETERMINED:
                return a.get_lang()
        return annots.UNDETERMINED
    return self.annotations[step].get_lang()

get_langresource

View Source
def get_langresource(self, step):
    return self.annotations[step].get_langresource()

activate_annotation

View Source
def activate_annotation(self, stepname):
    for i, a in enumerate(self.annotations):
        if a.get_key() == stepname:
            a.set_activate(True)
            return i
    return -1

activate_step

View Source
def activate_step(self, step):
    step = self.__check_step(step)
    self.annotations[step].set_activate(True)

disable_step

View Source
def disable_step(self, step):
    step = self.__check_step(step)
    self.annotations[step].set_activate(False)

get_step_status

View Source
def get_step_status(self, step):
    step = self.__check_step(step)
    return self.annotations[step].get_activate()

get_step_name

View Source
def get_step_name(self, step):
    step = self.__check_step(step)
    return self.annotations[step].get_name()

get_step_types

View Source
def get_step_types(self, step):
    step = self.__check_step(step)
    return self.annotations[step].get_types()

get_step_descr

View Source
def get_step_descr(self, step):
    step = self.__check_step(step)
    return self.annotations[step].get_descr()

get_step_idx

Get the annotation step index from an annotation key.

Parameters
  • annotation_key: (str)
Raises

KeyError

View Source
def get_step_idx(self, annotation_key):
    """Get the annotation step index from an annotation key.

        :param annotation_key: (str)
        :raises: KeyError

        """
    for i, a in enumerate(self.annotations):
        if a.get_key() == annotation_key:
            return i
    raise KeyError('No configuration file is available for an annotationwith key {:s}'.format(annotation_key))

get_step_key

View Source
def get_step_key(self, step):
    step = self.__check_step(step)
    return self.annotations[step].get_key()

get_step_numbers

View Source
def get_step_numbers(self):
    return len(self.annotations)

get_steplist

View Source
def get_steplist(self):
    steps = []
    for i in range(len(self.annotations)):
        steps.append(self.annotations[i].get_name())
    return steps

get_langlist

View Source
def get_langlist(self, step=2):
    step = self.__check_step(step)
    return self.annotations[step].get_langlist()

get_step

Return the 'sppasParam' instance of the annotation.

Parameters
  • step
View Source
def get_step(self, step):
    """Return the 'sppasParam' instance of the annotation."""
    step = self.__check_step(step)
    return self.annotations[step]

get_options

View Source
def get_options(self, step):
    step = self.__check_step(step)
    return self.annotations[step].get_options()

set_option_value

View Source
def set_option_value(self, step, key, value):
    step = self.__check_step(step)
    self.annotations[step].set_option_value(key, value)

get_output_extension

Return the output extension defined for the given out_format.

Parameters
  • out_format
View Source
def get_output_extension(self, out_format):
    """Return the output extension defined for the given out_format."""
    return self._out_extensions[out_format]

set_output_extension

Fix the output extension of all the annotations of a given out_format.

Parameters
  • output_ext: (str) File extension (with or without a dot)
  • output_format: (str) Either ANNOT, AUDIO, VIDEO OR IMAGE
Returns
  • (str) the assigned extension
Raises

ValueError

View Source
def set_output_extension(self, output_ext, output_format):
    """Fix the output extension of all the annotations of a given out_format.

        :param output_ext: (str) File extension (with or without a dot)
        :param output_format: (str) Either ANNOT, AUDIO, VIDEO OR IMAGE
        :return: (str) the assigned extension
        :raise: ValueError

        """
    if not output_ext.startswith('.'):
        output_ext = '.' + output_ext
    for e in sppasFiles.get_outformat_extensions(output_format):
        if e.startswith('.') is False:
            e = '.' + e
        if output_ext.lower() == e.lower():
            self._out_extensions[output_format] = e
            return e
    raise ValueError('{} not in supported extensions of {}'.format(output_ext, output_format))

get_ref_ids

Return a list of identifiers of the reference publications.

Parameters
  • step: (int) Annotation index
View Source
def get_ref_ids(self, step):
    """Return a list of identifiers of the reference publications.

        :param step: (int) Annotation index

        """
    step = self.__check_step(step)
    return self.annotations[step].get_reference_identifiers()

get_ref_url

Return the URL of the reference publication.

Parameters
  • step: (int) Annotation index
  • ref_id: (str) Identifier of a reference
View Source
def get_ref_url(self, step, ref_id):
    """Return the URL of the reference publication.

        :param step: (int) Annotation index
        :param ref_id: (str) Identifier of a reference

        """
    step = self.__check_step(step)
    return self.annotations[step].get_reference_url(ref_id)

get_devel

Return a percentage of development.

It is supposed that the annotations which have references are done, they're not under development anymore!

Parameters
  • step: (int) Annotation index
Returns
  • (int) 0=unknown. 1-99=in progress value. 100=done.
View Source
def get_devel(self, step):
    """Return a percentage of development.

        It is supposed that the annotations which have references are done,
        they're not under development anymore!

        :param step: (int) Annotation index
        :return: (int) 0=unknown. 1-99=in progress value. 100=done.

        """
    step = self.__check_step(step)
    return self.annotations[step].get_devel()

Protected functions

__load

Load parameters of an annotation from its configuration file.

Parameters
  • cfg_file
View Source
def __load(self, cfg_file):
    """Load parameters of an annotation from its configuration file."""
    try:
        a = annotationParam(cfg_file)
        self.annotations.append(a)
        logging.info('Configuration file {:s} loaded.'.format(cfg_file))
    except Exception as e:
        a = None
        logging.error('Configuration file {:s} not loaded: {}'.format(cfg_file, str(e)))
    return a

__check_step

View Source
def __check_step(self, step):
    try:
        step = int(step)
    except TypeError:
        raise sppasTypeError(step, 'int')
    if step < 0:
        raise sppasTypeError(step, 'unsigned int')
    if len(self.annotations) <= step:
        raise ValueError('Annotation step index {:d} out of range.'.format(step))
    return step