-
Notifications
You must be signed in to change notification settings - Fork 0
data_reader refactor #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MichalSzandar
wants to merge
9
commits into
main
Choose a base branch
from
data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
95a65de
data_reader refactor
MichalSzandar 75d3f49
Merge branch 'main' into data
MichalSzandar 7695bd2
remove circular dependency
MichalSzandar f4fdedd
Merge branch 'data' of github.com:KN-Neuron/NeuroSpeller into data
MichalSzandar 51222f3
run black on changed files
MichalSzandar 316db97
Apply suggestion from @Copilot
MichalSzandar f6ed6e9
Update data/src/main.py
MichalSzandar d7446e3
fix data_reader
MichalSzandar ee84240
remove circular dependency
MichalSzandar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,130 @@ | ||
| from collections import defaultdict | ||
| import scipy.io | ||
| from data_classes import * | ||
| from consts import * | ||
|
|
||
|
|
||
| def load_data(filename: str) -> tuple[np.ndarray, np.ndarray, list[int]]: | ||
| mat_data = scipy.io.loadmat(filename) | ||
| eeg_data = mat_data[EEG_KEY] | ||
| din_data = mat_data[DIN_KEY] | ||
|
|
||
| latencies_ms = [int(din_data[1, i].item()) for i in range(din_data.shape[1])] | ||
| latencies = [int(round(l * SAMPLING_RATE / 1000)) for l in latencies_ms] | ||
|
|
||
| return eeg_data, latencies_ms, latencies | ||
|
|
||
|
|
||
| def group_din_markers_into_trials( | ||
| latencies: list[int], gap_threshold_samples: int | ||
| ) -> list[trial_group]: | ||
| trial_groups = [] | ||
| current_start_idx = 0 | ||
| current_group_start = latencies[0] | ||
| current_group_end = latencies[0] | ||
|
|
||
| for i in range(1, len(latencies)): | ||
| if latencies[i] - latencies[i - 1] > gap_threshold_samples: | ||
| trial_groups.append( | ||
| trial_group( | ||
| start_sample=current_group_start, | ||
| end_sample=current_group_end, | ||
| start_idx=current_start_idx, | ||
| end_idx=i - 1, | ||
| from consts import ( | ||
| EEG_KEY, | ||
| DIN_KEY, | ||
| SAMPLING_RATE, | ||
| EXPECTED_FREQS, | ||
| ADAPT_TRIALS, | ||
| WINDOW_TIME_FRAME, | ||
| ) | ||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class DataReader(ABC): | ||
| @abstractmethod | ||
| def _load_data(self, filename: str) -> tuple[np.ndarray, np.ndarray, list[int]]: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def get_all_trials(self, filename: str) -> list[trial_info]: | ||
| pass | ||
|
|
||
|
|
||
| class MatDataReader(DataReader): | ||
| def _load_data( | ||
| self, filename: str, sampling_rate: int = SAMPLING_RATE | ||
| ) -> tuple[np.ndarray, np.ndarray, list[int]]: | ||
| mat_data = scipy.io.loadmat(filename) | ||
| eeg_data = mat_data[EEG_KEY] | ||
| din_data = mat_data[DIN_KEY] | ||
|
|
||
| latencies_ms = [int(din_data[1, i].item()) for i in range(din_data.shape[1])] | ||
| latencies = [int(round(l * sampling_rate / 1000)) for l in latencies_ms] | ||
|
|
||
| return eeg_data, latencies_ms, latencies | ||
|
|
||
| def _group_din_markers_into_trials( | ||
| self, latencies: list[int], gap_threshold_samples: int | ||
| ) -> list[trial_group]: | ||
|
MichalSzandar marked this conversation as resolved.
|
||
| trial_groups = [] | ||
| current_start_idx = 0 | ||
| current_group_start = latencies[0] | ||
| current_group_end = latencies[0] | ||
|
|
||
| for i in range(1, len(latencies)): | ||
| if latencies[i] - latencies[i - 1] > gap_threshold_samples: | ||
| trial_groups.append( | ||
| trial_group( | ||
| start_sample=current_group_start, | ||
| end_sample=current_group_end, | ||
| start_idx=current_start_idx, | ||
| end_idx=i - 1, | ||
| ) | ||
| ) | ||
| current_group_start = latencies[i] | ||
| current_start_idx = i | ||
| current_group_end = latencies[i] | ||
|
|
||
| trial_groups.append( | ||
| trial_group( | ||
| start_sample=current_group_start, | ||
| end_sample=current_group_end, | ||
| start_idx=current_start_idx, | ||
| end_idx=len(latencies) - 1, | ||
| ) | ||
| current_group_start = latencies[i] | ||
| current_start_idx = i | ||
| current_group_end = latencies[i] | ||
|
|
||
| trial_groups.append( | ||
| trial_group( | ||
| start_sample=current_group_start, | ||
| end_sample=current_group_end, | ||
| start_idx=current_start_idx, | ||
| end_idx=len(latencies) - 1, | ||
| ) | ||
| ) | ||
|
|
||
| return trial_groups | ||
|
|
||
| return trial_groups | ||
|
|
||
| def _extract_trials( | ||
| self, | ||
| eeg_data: np.ndarray, | ||
| latencies_ms: list[int], | ||
| trial_groups: list[trial_group], | ||
| adapt_trials: int = ADAPT_TRIALS, | ||
| trial_time_frame_sec: int = WINDOW_TIME_FRAME, | ||
| sampling_rate: int = SAMPLING_RATE, | ||
| ) -> list[trial_info]: | ||
|
MichalSzandar marked this conversation as resolved.
|
||
| all_trials = [] | ||
| samples_per_trial = sampling_rate * trial_time_frame_sec | ||
|
|
||
| for i, tg in enumerate(trial_groups): | ||
| start_sample = tg.start_sample | ||
| epoch = eeg_data[:, start_sample : start_sample + samples_per_trial] | ||
|
|
||
| if epoch.shape[1] != samples_per_trial: | ||
| continue | ||
|
|
||
| dins_ms = latencies_ms[tg.start_idx : tg.end_idx + 1] | ||
| n_dins = len(dins_ms) | ||
| true_freq = 0.0 | ||
| closest = 0.0 | ||
|
|
||
| if n_dins > 1: | ||
| intervals = [ | ||
| dins_ms[j] - dins_ms[j - 1] for j in range(1, len(dins_ms)) | ||
| ] | ||
| true_freq = (1000.0 / np.mean(intervals)) / 2.0 | ||
| closest = min(EXPECTED_FREQS, key=lambda f: abs(f - true_freq)) | ||
|
|
||
| trial_type = TrialType.ADAPT if i < adapt_trials else TrialType.TEST | ||
|
|
||
| trial_record = trial_info( | ||
| epoch=epoch, | ||
| trial=i, | ||
| true_freq=round(true_freq, 2), | ||
| closest_freq=closest, | ||
| n_dins=n_dins, | ||
| type=trial_type, | ||
| ) | ||
|
|
||
| def extract_trials( | ||
| eeg_data: np.ndarray, latencies_ms: list[int], trial_groups: list[trial_group] | ||
| ) -> list[trial_info]: | ||
| all_trials = [] | ||
|
|
||
| for i, tg in enumerate(trial_groups): | ||
| start_sample = tg.start_sample | ||
| epoch = eeg_data[:, start_sample : start_sample + SAMPLES_PER_5_SEC] | ||
|
|
||
| if epoch.shape[1] != SAMPLES_PER_5_SEC: | ||
| continue | ||
|
|
||
| dins_ms = latencies_ms[tg.start_idx : tg.end_idx + 1] | ||
| n_dins = len(dins_ms) | ||
| true_freq = 0.0 | ||
| closest = 0.0 | ||
|
|
||
| if n_dins > 1: | ||
| intervals = [dins_ms[j] - dins_ms[j - 1] for j in range(1, len(dins_ms))] | ||
| true_freq = (1000.0 / np.mean(intervals)) / 2.0 | ||
| closest = min(EXPECTED_FREQS, key=lambda f: abs(f - true_freq)) | ||
|
|
||
| trial_type = TrialType.ADAPT if i < ADAPT_TRIALS else TrialType.TEST | ||
| all_trials.append(trial_record) | ||
|
|
||
| trial_record = trial_info( | ||
| epoch=epoch, | ||
| trial=i, | ||
| true_freq=round(true_freq, 2), | ||
| closest_freq=closest, | ||
| n_dins=n_dins, | ||
| type=trial_type, | ||
| ) | ||
| return all_trials | ||
|
|
||
| all_trials.append(trial_record) | ||
| def get_all_trials(self, filename: str) -> list[trial_info]: | ||
| eeg_data, latencies_ms, latencies = self._load_data(filename) | ||
| trial_groups = self._group_din_markers_into_trials(latencies, 250) | ||
| all_trials = self._extract_trials(eeg_data, latencies_ms, trial_groups) | ||
|
|
||
| return all_trials | ||
| return all_trials | ||
|
|
||
|
|
||
| def main(filename: str) -> list[trial_info]: | ||
| eeg_data, latencies_ms, latencies = load_data(filename) | ||
| trial_groups = group_din_markers_into_trials(latencies, 250) | ||
| all_trials = extract_trials(eeg_data, latencies_ms, trial_groups) | ||
| class CSVDataReader(DataReader): | ||
| def _load_data(self, filename): | ||
| return super()._load_data(filename) | ||
|
|
||
| return all_trials | ||
| def get_all_trials(self, filename): | ||
| return super().get_all_trials(filename) | ||
|
MichalSzandar marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.