Skip to content

Commit

Permalink
Organized files for less clutering
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Louviot committed Nov 4, 2024
1 parent b42d9d9 commit 416a5cf
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
)
from eeg_research.preprocessing.tools.utils import read_raw_eeg
from eeg_research.simulators.decorators import pipe
from eeg_research.simulators.simulate_data import simulate_eeg_data
from eeg_research.simulators.eeg_simulator import simulate_eeg_data


class CleanerPipelines:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import bids

from eeg_research.simulators.cleaner_pipelines import CleanerPipelines
from eeg_research.preprocessing.pipelines.cleaner_pipelines import CleanerPipelines


def run_cbin_cleaner(cleaner: CleanerPipelines) -> CleanerPipelines:
Expand Down

This file was deleted.

91 changes: 88 additions & 3 deletions src/eeg_research/simulators/bids_simulator.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,106 @@
"""Create a temporary BIDS dataset to test data handling scripts."""

import json
import os
import shutil
import tempfile
from pathlib import Path
from typing import Generator, Any

import mne
import numpy as np
import pandas as pd

from eeg_research.simulators.path_handler import DirectoryTree
from eeg_research.simulators.simulate_data import (
from eeg_research.simulators.eeg_simulator import (
simulate_eeg_data,
simulate_light_eeg_data,
)


class DirectoryTree:
"""The class to handle the directory tree."""

def __init__(self, root_dir: str | os.PathLike) -> None:
"""Initialize the class with the root directory."""
self.root_dir = os.path.abspath(root_dir)
self.current_dir = self.root_dir

def generate_tree(
self: "DirectoryTree",
) -> Generator[tuple[str, list[str], list[str]], None, None]:
"""A generator method to walk through the directory tree.
It starts from the root directory. Yields tuples of (path, dirs, files)
for each directory in the tree.
"""
for dirpath, dirnames, filenames in os.walk(self.root_dir):
yield dirpath, dirnames, filenames

def list_directory_contents(
self: "DirectoryTree",
) -> tuple[list[str] | str, list[str]]:
"""List the contents of the current directory."""
try:
with os.scandir(self.current_dir) as it:
dirs = [entry.name for entry in it if entry.is_dir()]
files = [entry.name for entry in it if entry.is_file()]
return dirs, files
except PermissionError as e:
return f"Permission denied: {e}", []

def change_directory(self, target_dir: str | os.PathLike) -> str | os.PathLike:
"""Change the current directory to target_dir.
Change the current directory if it's a subdirectory
of current_dir or a valid path.
Args:
target_dir (str | os.PathLike): The target directory path.
"""
new_dir = os.path.abspath(os.path.join(self.current_dir, target_dir))
if os.path.commonpath([self.root_dir, new_dir]) != self.root_dir:
return f"Error: {new_dir} is not a subdirectory of the root directory."
if os.path.isdir(new_dir):
self.current_dir = new_dir
return f"Changed current directory to {self.current_dir}"
else:
return f"Error: Directory {new_dir} does not exist."

def print_tree(
self: "DirectoryTree",
start_path: str | os.PathLike = "",
prefix: str | os.PathLike = "",
) -> None:
"""Print the tree in a formated way.
Recursively prints the directory tree starting f
rom start_path with proper formatting.
Args:
start_path (str | os.PathLike, optional): The starting path.
Defaults to ''.
prefix (str | os.PathLike, optional): The prefix to be added.
Defaults to ''.
"""
if start_path == "":
start_path = self.root_dir
print(start_path + "/")

entries = [entry for entry in os.scandir(str(start_path))]
entries = sorted(entries, key=lambda e: (e.is_file(), e.name))
last_index = len(entries) - 1

for index, entry in enumerate(entries):
connector = "├──" if index != last_index else "└──"
if entry.is_dir():
print(f"{prefix}{connector} {entry.name}/")
extension = "│ " if index != last_index else " "
self.print_tree(
os.path.join(start_path, entry.name), str(prefix) + str(extension)
)
else:
print(f"{prefix}{connector} {entry.name}")

class DummyDataset:
"""A class to create a dummy BIDS dataset for EEG data.
Expand Down Expand Up @@ -322,7 +407,7 @@ def flush(self, check: bool = True) -> None:
print("Content being removed:")
tree.print_tree()

shutil.rmtree(self.root, ignore_errors=True, onerror=None)
shutil.rmtree(str(self.root))
post_removal_checker = self.root.exists()
if post_removal_checker:
print("The tree was not removed.")
Expand Down
2 changes: 1 addition & 1 deletion src/eeg_research/simulators/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import bids

from eeg_research.simulators.simulate_data import DummyDataset
from eeg_research.simulators.eeg_simulator import DummyDataset

FunctionType = TypeVar("FunctionType", bound=Callable[..., Any])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def simulate_light_eeg_data(

return raw


def simulate_eeg_data(
n_channels: int = 16,
duration: int = 2,
Expand Down
88 changes: 0 additions & 88 deletions src/eeg_research/simulators/path_handler.py
Original file line number Diff line number Diff line change
@@ -1,89 +1 @@
"""The module to handle the directory tree."""

import os
from typing import Generator


class DirectoryTree:
"""The class to handle the directory tree."""

def __init__(self, root_dir: str | os.PathLike) -> None:
"""Initialize the class with the root directory."""
self.root_dir = os.path.abspath(root_dir)
self.current_dir = self.root_dir

def generate_tree(
self: "DirectoryTree",
) -> Generator[tuple[str, list[str], list[str]], None, None]:
"""A generator method to walk through the directory tree.
It starts from the root directory. Yields tuples of (path, dirs, files)
for each directory in the tree.
"""
for dirpath, dirnames, filenames in os.walk(self.root_dir):
yield dirpath, dirnames, filenames

def list_directory_contents(
self: "DirectoryTree",
) -> tuple[list[str] | str, list[str]]:
"""List the contents of the current directory."""
try:
with os.scandir(self.current_dir) as it:
dirs = [entry.name for entry in it if entry.is_dir()]
files = [entry.name for entry in it if entry.is_file()]
return dirs, files
except PermissionError as e:
return f"Permission denied: {e}", []

def change_directory(self, target_dir: str | os.PathLike) -> str | os.PathLike:
"""Change the current directory to target_dir.
Change the current directory if it's a subdirectory
of current_dir or a valid path.
Args:
target_dir (str | os.PathLike): The target directory path.
"""
new_dir = os.path.abspath(os.path.join(self.current_dir, target_dir))
if os.path.commonpath([self.root_dir, new_dir]) != self.root_dir:
return f"Error: {new_dir} is not a subdirectory of the root directory."
if os.path.isdir(new_dir):
self.current_dir = new_dir
return f"Changed current directory to {self.current_dir}"
else:
return f"Error: Directory {new_dir} does not exist."

def print_tree(
self: "DirectoryTree",
start_path: str | os.PathLike = "",
prefix: str | os.PathLike = "",
) -> None:
"""Print the tree in a formated way.
Recursively prints the directory tree starting f
rom start_path with proper formatting.
Args:
start_path (str | os.PathLike, optional): The starting path.
Defaults to ''.
prefix (str | os.PathLike, optional): The prefix to be added.
Defaults to ''.
"""
if start_path == "":
start_path = self.root_dir
print(start_path + "/")

entries = [entry for entry in os.scandir(str(start_path))]
entries = sorted(entries, key=lambda e: (e.is_file(), e.name))
last_index = len(entries) - 1

for index, entry in enumerate(entries):
connector = "├──" if index != last_index else "└──"
if entry.is_dir():
print(f"{prefix}{connector} {entry.name}/")
extension = "│ " if index != last_index else " "
self.print_tree(
os.path.join(start_path, entry.name), str(prefix) + str(extension)
)
else:
print(f"{prefix}{connector} {entry.name}")
1 change: 0 additions & 1 deletion src/eeg_research/simulators/signals/__init__.py

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion tests/analysis/tools/test_freq_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from mne.io import RawArray

import eeg_research.analysis.tools.freq_analysis as script
from eeg_research.simulators.simulate_data import simulate_light_eeg_data
from eeg_research.simulators.eeg_simulator import simulate_light_eeg_data


@pytest.fixture
Expand Down
Loading

0 comments on commit 416a5cf

Please sign in to comment.