Skip to content

Commit

Permalink
Prototype implementation of lazy loading for check/detection_kit.py
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriPavela committed Sep 30, 2024
1 parent 3fb27d9 commit 8d537d3
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 44 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
recursive-include perun *.pyi
include perun/py.typed
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ perun_files = files(
'LICENSE',
'pyproject.toml',
'tox.ini',
'MANIFEST.in',
)

perun_dir = 'perun'
Expand Down
4 changes: 4 additions & 0 deletions perun/check/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
Contains the actual methods in isolate modules, and then a factory module,
containing helper and generic stuff."""

import lazy_loader as lazy

__getattr__, __dir__, __all__ = lazy.attach_stub(__name__, __file__)
1 change: 1 addition & 0 deletions perun/check/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .detection_kit import get_filtered_best_models_of as get_filtered_best_models_of, general_detection as general_detection, create_model_record as create_model_record, get_function_values as get_function_values, create_filter_by_model as create_filter_by_model
1 change: 1 addition & 0 deletions perun/check/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ perun_check_dir = perun_dir / 'check'

perun_check_files = files(
'__init__.py',
'__init__.pyi',
'factory.py',
'detection_kit.py',
'nonparam_kit.py',
Expand Down
8 changes: 3 additions & 5 deletions perun/check/methods/best_model_order_equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
# Perun Imports
from perun.check.methods.abstract_base_checker import AbstractBaseChecker
from perun.utils.structs.common_structs import DegradationInfo, PerformanceChange
import perun.check.detection_kit as detection
import perun.check as check

if TYPE_CHECKING:
from perun.profile.factory import Profile
Expand Down Expand Up @@ -81,10 +81,8 @@ def check(
:param _: unification with other detection methods (unused in this method)
:returns: tuple (degradation result, degradation location, degradation rate)
"""
best_baseline_models = detection.get_filtered_best_models_of(
baseline_profile, group="param"
)
best_target_models = detection.get_filtered_best_models_of(target_profile, group="param")
best_baseline_models = check.get_filtered_best_models_of(baseline_profile, group="param")
best_target_models = check.get_filtered_best_models_of(target_profile, group="param")

for uid, best_model in best_target_models.items():
best_baseline_model = best_baseline_models.get(uid)
Expand Down
4 changes: 2 additions & 2 deletions perun/check/methods/fast_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from perun.check.methods.abstract_base_checker import AbstractBaseChecker
from perun.logic import runner
from perun.utils.structs.common_structs import DegradationInfo, ClassificationMethod
import perun.check.detection_kit as detect
import perun.check as check

if TYPE_CHECKING:
from perun.profile.factory import Profile
Expand All @@ -35,7 +35,7 @@ def check(
:param _: unification with other detection methods (unused in this method)
:returns: tuple (degradation result, degradation location, degradation rate, confidence)
"""
return detect.general_detection(
return check.general_detection(
baseline_profile, target_profile, ClassificationMethod.FastCheck
)

Expand Down
14 changes: 7 additions & 7 deletions perun/check/methods/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from scipy import stats

# Perun Imports
from perun.check import detection_kit as detect
import perun.check as check
from perun.check.methods import fast_check
from perun.check.methods.abstract_base_checker import AbstractBaseChecker
from perun.utils.common import common_kit
Expand All @@ -40,7 +40,7 @@ def check(
:returns: tuple (degradation result, degradation location, degradation rate, confidence)
"""

return detect.general_detection(
return check.general_detection(
baseline_profile, target_profile, ClassificationMethod.LinearRegression
)

Expand Down Expand Up @@ -107,15 +107,15 @@ def exec_linear_regression(
uid, baseline_profile, baseline_x_pts, lin_abs_error
)
# obtaining the models (linear and quadratic) from the new regressed profile
quad_err_model = detect.get_filtered_best_models_of(
quad_err_model = check.get_filtered_best_models_of(
std_err_profile,
group="param",
model_filter=detect.create_filter_by_model("quadratic"),
model_filter=check.create_filter_by_model("quadratic"),
)
linear_err_model = detect.get_filtered_best_models_of(
linear_err_model = check.get_filtered_best_models_of(
std_err_profile,
group="param",
model_filter=detect.create_filter_by_model("linear"),
model_filter=check.create_filter_by_model("linear"),
)

# check the last quadratic type of change
Expand All @@ -127,7 +127,7 @@ def exec_linear_regression(

# We did not classify the change
if not change_type:
std_err_model = detect.get_filtered_best_models_of(std_err_profile, group="param")
std_err_model = check.get_filtered_best_models_of(std_err_profile, group="param")
change_type = std_err_model[uid].type

return change_type
4 changes: 2 additions & 2 deletions perun/check/methods/polynomial_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Perun Imports
from perun.check.methods.abstract_base_checker import AbstractBaseChecker
from perun.utils.structs.common_structs import DegradationInfo, ClassificationMethod
import perun.check.detection_kit as detect
import perun.check as check

if TYPE_CHECKING:
import numpy.typing as npt
Expand All @@ -37,7 +37,7 @@ def check(
:param _: unification with other detection methods (unused in this method)
:returns: tuple (degradation result, degradation location, degradation rate, confidence)
"""
return detect.general_detection(
return check.general_detection(
baseline_profile,
target_profile,
ClassificationMethod.PolynomialRegression,
Expand Down
6 changes: 3 additions & 3 deletions perun/check/nonparam_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from perun.postprocess.regression_analysis import data_provider
from perun.utils import log
from perun.utils.structs.common_structs import PerformanceChange, ModelRecord
import perun.check.detection_kit as methods
import perun.check as check
import perun.postprocess.regressogram.methods as rg_methods

if TYPE_CHECKING:
Expand Down Expand Up @@ -107,7 +107,7 @@ def unify_buckets_in_regressogram(

# match the regressogram model with the right 'uid'
model = [model for model in new_regressogram_models if model["uid"] == uid][0]
return methods.create_model_record(model)
return check.create_model_record(model)


def preprocess_nonparam_models(
Expand Down Expand Up @@ -146,7 +146,7 @@ def get_model_coordinates(model: ModelRecord) -> tuple[list[float], list[float]]
:return: obtained x and y coordinates - x-points, y-points
"""
if model.b1 is not None:
x_pts, y_pts = methods.get_function_values(model)
x_pts, y_pts = check.get_function_values(model)
else:
x_pts = np.linspace(model.x_start, model.x_end, num=len(model.b0))
y_pts = model.b0
Expand Down
27 changes: 5 additions & 22 deletions perun/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,8 @@

from __future__ import annotations

import cProfile, pstats, io
import atexit

pr = cProfile.Profile()
pr.enable()


def terminate_profiling():
pr.disable()
s = io.StringIO()
sortby = pstats.SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats("perun/collect")
# ps.print_stats(100)
print(s.getvalue())


atexit.register(terminate_profiling)


# Standard Imports
from typing import Optional, Any
from typing import Optional, Any, TYPE_CHECKING
import os
import sys

Expand All @@ -76,7 +56,6 @@ def terminate_profiling():
from perun.utils import exceptions, log as perun_log
from perun.utils.common import cli_kit, common_kit
from perun.utils.external import commands as external_commands
from perun.profile.factory import Profile
from perun.utils.exceptions import (
UnsupportedModuleException,
NotPerunRepositoryException,
Expand All @@ -93,6 +72,10 @@ def terminate_profiling():
import perun.deltadebugging.factory as delta


if TYPE_CHECKING:
from perun.profile.factory import Profile


DEV_MODE = False


Expand Down
1 change: 1 addition & 0 deletions perun/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
perun_files = files(
'cli.py',
'__init__.py',
'py.typed',
)

py3.install_sources(
Expand Down
6 changes: 3 additions & 3 deletions perun/profile/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from perun.profile import convert, query, stats, helpers
from perun.utils import log
from perun.utils.common import common_kit
import perun.check.detection_kit as detection
import perun.check as check
import perun.postprocess.regressogram.methods as nparam_methods

if TYPE_CHECKING:
Expand Down Expand Up @@ -374,9 +374,9 @@ def all_filtered_models(self, models_strategy: str) -> dict[str, ModelRecord]:
"""
group = models_strategy.rsplit("-")[1]
if models_strategy in ("all-param", "all-nonparam"):
return detection.get_filtered_best_models_of(self, group=group, model_filter=None)
return check.get_filtered_best_models_of(self, group=group, model_filter=None)
elif models_strategy in ("best-nonparam", "best-model", "best-param"):
return detection.get_filtered_best_models_of(self, group=group)
return check.get_filtered_best_models_of(self, group=group)
else:
return {}

Expand Down
Empty file added perun/py.typed
Empty file.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies = [
"ninja>=1.11",
"wheel>=0.43.0",

# Lazy loading
"lazy-loader>=0.4",

# Other
"psutil>=6.0.0",

Expand Down Expand Up @@ -140,6 +143,7 @@ module = [
"statsmodels.*",
"holoviews.*",
"bcc.*",
"lazy_loader.*",
]
ignore_missing_imports = true

Expand Down

0 comments on commit 8d537d3

Please sign in to comment.