Skip to content

Commit

Permalink
more importlib hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
slobentanzer committed Jan 30, 2025
1 parent 78f6699 commit 16ad69d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
21 changes: 20 additions & 1 deletion docs/scripts/_stats.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
"""Statistical analysis of the results."""

import sys
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from ._plotting import melt_and_process
# Import plotting module using full path
CURRENT_DIR = Path(__file__).parent
plotting_path = CURRENT_DIR / "_plotting.py"

if "_plotting" not in sys.modules:
spec = spec_from_file_location("_plotting", plotting_path)
if spec is None or spec.loader is None:
msg = "Could not load _plotting module"
raise ImportError(msg)
plotting = module_from_spec(spec)
sys.modules["_plotting"] = plotting
spec.loader.exec_module(plotting)
else:
plotting = sys.modules["_plotting"]

melt_and_process = plotting.melt_and_process


def calculate_stats(overview: pd.DataFrame) -> None:
Expand Down
18 changes: 13 additions & 5 deletions docs/scripts/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,34 @@

import pandas as pd

# Get the current directory
CURRENT_DIR = Path(__file__).parent

# Dynamically import local modules
# Dynamically import local modules with full paths
def import_local_module(module_name: str) -> None:
"""Import a local module."""
module_path = Path(__file__).parent / f"_{module_name}.py"
module_path = CURRENT_DIR / f"_{module_name}.py"
module = None

# If module is already loaded, return it
if f"_{module_name}" in sys.modules:
return sys.modules[f"_{module_name}"]

spec = spec_from_file_location(f"_{module_name}", module_path)
if spec is None or spec.loader is None:
msg = f"Could not load module {module_name}"
raise ImportError(msg)
module = module_from_spec(spec)
sys.modules[spec.name] = module # This makes the module available for other imports
sys.modules[module.__name__] = module
spec.loader.exec_module(module)
return module

# Import the local modules
# Import the modules
plotting = import_local_module("plotting")
preprocess = import_local_module("preprocess")
stats = import_local_module("stats")

# Extract the needed functions from the modules
# Extract the needed functions
plot_accuracy_per_model = plotting.plot_accuracy_per_model
plot_accuracy_per_quantisation = plotting.plot_accuracy_per_quantisation
plot_accuracy_per_task = plotting.plot_accuracy_per_task
Expand Down

0 comments on commit 16ad69d

Please sign in to comment.