SPPAS integration of the Activity generation.
Module sppas.src.annotations
Class sppasActivity
Description
Constructor
Create a new sppasActivity instance.
Parameters
- log: (sppasLog) Human-readable logs.
View Source
def __init__(self, log=None):
"""Create a new sppasActivity instance.
:param log: (sppasLog) Human-readable logs.
"""
sppasBaseAnnotation.__init__(self, 'activity.json', log)
self.__activity = Activity()
Public functions
fix_options
Fix all options.
Available options are:
- duration
Parameters
- options: (sppasOption)
View Source
def fix_options(self, options):
"""Fix all options.
Available options are:
- duration
:param options: (sppasOption)
"""
for opt in options:
key = opt.get_key()
if 'duration' == key:
self.set_duration_tier(opt.get_value())
elif 'pattern' in key:
self._options[key] = opt.get_value()
else:
raise AnnotationOptionError(key)
set_duration_tier
Fix the activity duration option.
Parameters
- value: (bool) Activity tier generation.
View Source
def set_duration_tier(self, value):
"""Fix the activity duration option.
:param value: (bool) Activity tier generation.
"""
self._options['duration'] = bool(value)
convert
Create an Activity and ActivityDuration tier.
Parameters
- tier: (sppasTier)
- tmin: (sppasPoint)
- tmax: (sppasPoint)
Returns
- (sppasTier, sppasTier)
View Source
def convert(self, tier, tmin, tmax):
"""Create an Activity and ActivityDuration tier.
:param tier: (sppasTier)
:param tmin: (sppasPoint)
:param tmax: (sppasPoint)
:returns: (sppasTier, sppasTier)
"""
try:
activity = self.__activity.get_tier(tier, tmin, tmax)
except Exception as e:
self.logfile.print_message(MSG_EXTRA_TIER.format(tiername='Activity', message=str(e)), indent=2, status=annots.warning)
return (None, None)
duration = None
if self._options['duration'] is True:
duration = sppasTier('ActivityDuration')
for a in activity:
interval = a.get_location().get_best()
dur = round(interval.duration().get_value(), 6)
duration.create_annotation(sppasLocation(interval.copy()), sppasLabel(sppasTag(dur, tag_type='float')))
return (activity, duration)
get_inputs
Return the the tier with aligned tokens.
Parameters
- input_files: (list)
Raises
NoTierInputError
Returns
- (sppasTier)
View Source
def get_inputs(self, input_files):
"""Return the the tier with aligned tokens.
:param input_files: (list)
:raise: NoTierInputError
:return: (sppasTier)
"""
tier = None
annot_ext = self.get_input_extensions()
for filename in input_files:
fn, fe = os.path.splitext(filename)
if tier is None and fe in annot_ext[0]:
parser = sppasTrsRW(filename)
trs_input = parser.read()
tier = sppasFindTier.aligned_tokens(trs_input)
if tier is not None:
tmin = trs_input.get_min_loc()
tmax = trs_input.get_max_loc()
return (tier, tmin, tmax)
logging.error('Tier with time-aligned tokens not found.')
raise NoTierInputError
run
Run the automatic annotation process on an input.
Important: options could be changed!
Parameters
- input_files: (list of str) Time-aligned tokens
- output: (str) the output name - either filename or basename
Returns
- (sppasTranscription)
View Source
def run(self, input_files, output=None):
"""Run the automatic annotation process on an input.
Important: options could be changed!
:param input_files: (list of str) Time-aligned tokens
:param output: (str) the output name - either filename or basename
:returns: (sppasTranscription)
"""
tok_tier, tmin, tmax = self.get_inputs(input_files)
activity, duration = self.convert(tok_tier, tmin, tmax)
trs_output = sppasTranscription(self.name)
trs_output.set_meta('annotation_result_of', input_files[0])
trs_output.append(activity)
if duration is not None:
trs_output.append(duration)
if output is not None:
output_file = self.fix_out_file_ext(output)
parser = sppasTrsRW(output_file)
parser.write(trs_output)
return [output_file]
return trs_output
get_input_patterns
List of patterns this annotation expects for its input filenames.
View Source
def get_input_patterns(self):
"""List of patterns this annotation expects for its input filenames."""
return [self._options.get('inputpattern', '-palign')]
get_output_pattern
Pattern this annotation uses in an output filename.
View Source
def get_output_pattern(self):
"""Pattern this annotation uses in an output filename."""
return self._options.get('outputpattern', '-activity')