Skip to content
Snippets Groups Projects
model.py 3.30 KiB
from statistics import Statistics
from shadow_task import ShadowTask
import pandas as pd


class Model:
    """Internal data representation and processing."""
    def __init__(self):
        self._stats = Statistics(None)
        self._analysed = False
        self._shadow_tasks = []

    @property
    def shadow_tasks(self):
        """ Getter for the shadow tasks.

        Returns:
             the shadow tasks
        """
        return self._shadow_tasks

    @shadow_tasks.setter
    def shadow_tasks(self, tasks):
        """ Set the shadow tasks with the given tasks.

        Args:
            tasks: the shadow tasks there are
        """
        self._shadow_tasks = tasks

    def analysis_complete(self):
        """Check whether self._analysis_results has a value."""
        return self._analysed

    def compare(self):
        """Run the analyses"""
        for trial in self._shadow_tasks:
            self._stats.analyze(trial)
        self._analysed = True

    def add_task(self, pnr, vnr, condition, source, shadow):
        """ Add a task specifying all the necessary information

            Args:
                pnr: the participant number
                vnr: the video number
                condition: the task of the shadowing trial
                source: the source file
                shadow: the shadow file
        """
        self._shadow_tasks.append(
            ShadowTask(pnr, vnr, condition, source, shadow))

    def create_delay_frame(self):
        """Create the frame for displaying delays.

        Returns:
             The dataframe with delays
        """
        delay_frame = ({'participant': [],
              'assignment': [],
              'video': [],
              'words': [],
              'delays': []})
        for trial in self._shadow_tasks:
            for result in trial.delays:
                delay_frame['participant'].append(result[0])
                delay_frame['assignment'].append(result[1])
                delay_frame['video'].append(result[2])
                delay_frame['words'].append(result[3])
                delay_frame['delays'].append(result[4])

        return pd.DataFrame(data=delay_frame)

    def create_mistake_frame(self):
        """ Create the frame for the output

        Returns:
             The filled data frame
        """
        d = ({'participant': [],
              'assignment': [],
              'video': [],
              'accuracy': [],
              '#mistakes': [],
              '#phonetic': [],
              '#repetition': [],
              '#form': [],
              '#semantic': [],
              '#skipped': [],
              '#random': []})
        for trial in self._shadow_tasks:
            d['participant'].append(trial.participant)
            d['assignment'].append(trial.condition)
            d['video'].append(trial.video)
            d['accuracy'].append(trial.results['accuracy'])
            d['#mistakes'].append(trial.results['#mistakes'])
            d['#phonetic'].append(trial.results['#phonetic'])
            d['#repetition'].append(trial.results['#repetition'])
            d['#form'].append(trial.results['#form'])
            d['#semantic'].append(trial.results['#semantic'])
            d['#skipped'].append(trial.results['#skipped'])
            d['#random'].append(trial.results['#random'])

        return pd.DataFrame(data=d)