diff --git a/umbra/controller.py b/umbra/controller.py index abc64dc6417b6d38e459f6f6d25e52dcec01c129..dfaded878f0ef46ed1747e34a2f3650cf6a9f339 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 a74e0c063349a6f6804c2c1bc815ae16d78a7178..784b491f91aaf04b8f514354cc34b469d85d199b 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 1b1a75990a0e68237f13d479a9d321d0fd0f729c..0fec6e7ca0c5bbf9bde91664f8ccc8a4b67e71af 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 b8937f3f96906f6b4d50342c60b27653a98e64c8..964a17947b7d2cdf4a66ea5c39ec5759692f8a2f 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 fe045e84ca5e3947d8679bcd37d10dfe3d255d74..c1fada4a21de1d809d136743d5a326b23a38377c 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