Annotation parameters manager.
Parameters of a set of annotations.
Annotation parameters manager.
Parameters of a set of annotations.
Create a new sppasParam instance with default values.
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]
Load the annotation configuration files.
Load from a list of given file names (without path) or from the default sppas ui configuration file.
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 the sppasui.json file.
Parse the file to get the list of annotations and parse the corresponding "json" file.
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']))
Return the workspace.
def get_workspace(self):
"""Return the workspace."""
return self._workspace
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 a list of files or directories into the workspace.
The state of all the added files is set to CHECKED.
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)
Return the list of entries to annotate.
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]
Fix the name of the file to save the report of the annotations.
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
Return the name of the file for the Procedure Outcome Report.
def get_report_filename(self):
"""Return the name of the file for the Procedure Outcome Report."""
return self._report
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)
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()
def get_langresource(self, step):
return self.annotations[step].get_langresource()
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
def activate_step(self, step):
step = self.__check_step(step)
self.annotations[step].set_activate(True)
def disable_step(self, step):
step = self.__check_step(step)
self.annotations[step].set_activate(False)
def get_step_status(self, step):
step = self.__check_step(step)
return self.annotations[step].get_activate()
def get_step_name(self, step):
step = self.__check_step(step)
return self.annotations[step].get_name()
def get_step_types(self, step):
step = self.__check_step(step)
return self.annotations[step].get_types()
def get_step_descr(self, step):
step = self.__check_step(step)
return self.annotations[step].get_descr()
Get the annotation step index from an annotation key.
KeyError
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))
def get_step_key(self, step):
step = self.__check_step(step)
return self.annotations[step].get_key()
def get_step_numbers(self):
return len(self.annotations)
def get_steplist(self):
steps = []
for i in range(len(self.annotations)):
steps.append(self.annotations[i].get_name())
return steps
def get_langlist(self, step=2):
step = self.__check_step(step)
return self.annotations[step].get_langlist()
Return the 'sppasParam' instance of the annotation.
def get_step(self, step):
"""Return the 'sppasParam' instance of the annotation."""
step = self.__check_step(step)
return self.annotations[step]
def get_options(self, step):
step = self.__check_step(step)
return self.annotations[step].get_options()
def set_option_value(self, step, key, value):
step = self.__check_step(step)
self.annotations[step].set_option_value(key, value)
Return the output extension defined for the given out_format.
def get_output_extension(self, out_format):
"""Return the output extension defined for the given out_format."""
return self._out_extensions[out_format]
Fix the output extension of all the annotations of a given out_format.
ValueError
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))
Return a list of identifiers of the reference publications.
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()
Return the URL of the reference publication.
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)
Return a percentage of development.
It is supposed that the annotations which have references are done, they're not under development anymore!
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()
Load parameters of an annotation from its configuration file.
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
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