From 2d1ff714e94540b9e318e7da2abbe264e7eeda4d Mon Sep 17 00:00:00 2001 From: "Verbeek, J.M. (Janneke)" <j.verbeek@student.ru.nl> Date: Sun, 1 Dec 2019 17:00:21 +0100 Subject: [PATCH] Worked on file selection. Reading in multiple files works fine, button still works, single file still works. Next up is making sure comparison happens between files that have the same identity/number, as well as improved usability. --- umbra/controller.py | 28 +++++++++++----------------- umbra/filereader.py | 30 +++++++++++++++++++++++------- umbra/model.py | 35 +++++++++++++++++++++++++++++++---- umbra/utils.py | 8 ++++++++ umbra/view.py | 4 ++-- 5 files changed, 75 insertions(+), 30 deletions(-) diff --git a/umbra/controller.py b/umbra/controller.py index abc64dc6..dfaded87 100644 --- a/umbra/controller.py +++ b/umbra/controller.py @@ -1,4 +1,5 @@ from filereader import * +from utils import Utils as ut import os import tkinter.filedialog as fd @@ -48,25 +49,19 @@ class Controller: self._select_folder() def _select_folder(self): - - shadow_codes = ["_AO"] folder_path = fd.askdirectory() file_names = os.listdir(folder_path) file_paths = [folder_path + "/" + x for x in file_names] - print(file_paths) + self._shadow_files = file_paths # For the record for file_path in file_paths: - self._filereader.path(file_path) - self._filereader.read() - # if file_path != "": # Do not assign an empty file path - # label_text = file_path.title().split("/")[-1] - # if any(code in label_text.upper() for code in shadow_codes): - # self.model.add_file(file_path, "shadow") - # self.view.add_file_entry(label_text, "shadow") - # else: - # self.model.add_file(file_path, "source") - # self.view.add_file_entry(label_text, "source") - - + if "AO" not in file_path: + pass # TODO: raise error + else: + dict = self._model._multi_data_shadow + key = ut.shadow_regex(file_path) + self._filereader.path(file_path) + data = self._filereader.read() + self._model.add_to_dict(key, data, dict) def _select_files(self, type): """Select files and add them to files list corresponding to type. @@ -127,10 +122,9 @@ class Controller: # TODO # Temporary 'solution' for testing below path = self._source_files[0] - print(path) self._filereader = CSVReader(path, type) data = self._filereader.read() - if type == "source": # Fix is dirtier than I can talk + if type == "source": self._model._data_source = data else: self._model._data_shadow = data diff --git a/umbra/filereader.py b/umbra/filereader.py index a74e0c06..784b491f 100644 --- a/umbra/filereader.py +++ b/umbra/filereader.py @@ -7,6 +7,8 @@ import csv class FileReader (ABC): """Read source and shadow files from given path.""" + def __init__(self, path): + self._path = path @abstractmethod def read(self): @@ -23,6 +25,7 @@ class FileReader (ABC): extraction: df of only the relevant data (timing/words) """ extraction = data.iloc[:, 5:8] + extraction = extraction.copy() extraction.columns = ["Onset", "Offset", "Word"] extraction[["Onset", "Offset"]] = extraction[["Onset", "Offset"]] \ .apply(pd.to_numeric) @@ -50,6 +53,16 @@ class FileReader (ABC): words = Sentence(ws) return words + def path(self): + return self._path + + def path(self, pth): + self._path = pth + + + + + class CSVReader(FileReader): @@ -58,16 +71,19 @@ class CSVReader(FileReader): Args: path: string, the internal path to the source file """ - self._path = path + super().__init__(path) self._type = type # Very dirty fix def read(self): """Method that is used to read the data into a workable format""" - df = pd.read_csv(self._path, header=None, sep='\n') - df = df[0].str.split('\t', expand=True) - data = self.extract_task_data(df) - words = self.df_to_words(data, self._type) - return words + if ".csv" not in self._path: + pass + else: + df = pd.read_csv(self._path, header=None, sep='\n') + df = df[0].str.split('\t', expand=True) + data = self.extract_task_data(df) + words = self.df_to_words(data, self._type) + return words class TxtReader(FileReader): @@ -78,7 +94,7 @@ class TxtReader(FileReader): Args: path: path to file """ - self._path = path + super().__init__(path) self._words = None def read(self): diff --git a/umbra/model.py b/umbra/model.py index 1b1a7599..0fec6e7c 100644 --- a/umbra/model.py +++ b/umbra/model.py @@ -6,8 +6,10 @@ class Model: def __init__(self): self._stats = Statistics(None) self._data_source = None + self._multi_data_shadow = {} # dict because fast and indexing self._data_shadow = None self._analysis_results = None + self._multi_results = {} self._save_pref = None @property @@ -26,6 +28,14 @@ class Model: def data_source(self, data): self._data_source = data + @property + def multi_data_shadow(self): + return self._multi_data_shadow + + @multi_data_shadow.setter + def multi_data_shadow(self, data): + self._multi_data_shadow = data + @property def data_shadow(self): return self._data_shadow @@ -44,7 +54,8 @@ class Model: def has_shadow(self): """Check whether self._data_shadow has a value.""" - return self._data_shadow is not None + return self._data_shadow is not None \ + or self._multi_data_shadow is not None def analysis_complete(self): """Check whether self._analysis_results has a value.""" @@ -53,7 +64,23 @@ class Model: def compare(self): """"Run the analyses and saves the results.""" if self.has_source() and self.has_shadow(): - self._analysis_results = self._stats.analyze(self._data_source, - self._data_shadow) - else: + print(self._multi_data_shadow, self.has_shadow()) + if self._multi_data_shadow: + for key, data in self._multi_data_shadow.items(): + # TODO: if same number then do this + result = self._stats.analyze(self._data_source, data) + self.add_to_dict(key+"_res", result, self._multi_results) + + # else pass + else: + self._analysis_results = \ + self._stats.analyze(self._data_source, self._data_shadow) + else: print("This needs fixing") + + # I needs it + def add_to_dict(self, key, item, dict): + if key in dict: + pass # TODO: raise error + else: + dict[key] = item diff --git a/umbra/utils.py b/umbra/utils.py index b8937f3f..964a1794 100644 --- a/umbra/utils.py +++ b/umbra/utils.py @@ -1,3 +1,6 @@ +import re + + class Utils: @staticmethod @@ -32,3 +35,8 @@ class Utils: m, s = divmod(s, 60) h, m = divmod(m, 60) return "%02i:%02i:%02i.%03i" % (h, m, s, ms) + + def shadow_regex(path): + match = re.search("AO\d", path) + if match: + return match.group() diff --git a/umbra/view.py b/umbra/view.py index fe045e84..c1fada4a 100644 --- a/umbra/view.py +++ b/umbra/view.py @@ -245,9 +245,9 @@ class View: self._add_to_dict(key, label, self._elements) @staticmethod - def _add_to_dict(key, value, dict): # TODO: Possibly better in utils + def _add_to_dict(key, value, dict): # TODO: Definitely better in utils """Checks if key already exists in a dictionary - before adding new key-value pair. + before adding new key-value pair Args: key: Key to add to dictionary -- GitLab