mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-08 10:22:09 +00:00
Hotfix/squeeze import (#5358)
* remove commit_hash and branch references * lazy load stats, scipy, pandas_ta * black & ruff * adding commithash back to log prefix * fix test --------- Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
This commit is contained in:
@@ -22,7 +22,6 @@ import numpy as np
|
||||
import pandas as pd
|
||||
import plotly.graph_objects as go
|
||||
import plotly.io as pio
|
||||
import statsmodels.api as sm
|
||||
from openbb_charting.core.backend import PLOTLYJS_PATH, get_backend
|
||||
from openbb_charting.core.chart_style import ChartStyle
|
||||
from openbb_charting.core.config.openbb_styles import (
|
||||
@@ -30,7 +29,6 @@ from openbb_charting.core.config.openbb_styles import (
|
||||
)
|
||||
from openbb_core.app.model.charts.charting_settings import ChartingSettings
|
||||
from plotly.subplots import make_subplots
|
||||
from scipy import stats
|
||||
|
||||
if TYPE_CHECKING:
|
||||
try: # noqa: SIM105
|
||||
@@ -308,6 +306,8 @@ class OpenBBFigure(go.Figure):
|
||||
Column of the subplot, by default 1
|
||||
"""
|
||||
|
||||
from scipy import stats
|
||||
|
||||
callback = stats.norm if curve == "normal" else stats.gaussian_kde
|
||||
|
||||
def _validate_x(data: Union[np.ndarray, pd.Series, type[TimeSeriesT]]):
|
||||
@@ -1606,6 +1606,8 @@ class OpenBBFigure(go.Figure):
|
||||
pacf : bool, optional
|
||||
Flag to indicate whether to use partial autocorrelation or not, by default False
|
||||
"""
|
||||
import statsmodels.api as sm
|
||||
|
||||
mode = "markers+lines" if marker else "lines"
|
||||
line = kwargs.pop("line", None)
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@ from typing import List, Literal
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pandas_ta as ta
|
||||
import statsmodels.api as sm
|
||||
from openbb_core.app.model.obbject import OBBject
|
||||
from openbb_core.app.model.results.empty import Empty
|
||||
from openbb_core.app.router import Router
|
||||
@@ -15,8 +13,6 @@ from openbb_core.app.utils import (
|
||||
)
|
||||
from openbb_provider.abstract.data import Data
|
||||
from pydantic import NonNegativeFloat, PositiveInt
|
||||
from scipy import stats
|
||||
from statsmodels.tsa import stattools
|
||||
|
||||
from openbb_qa.qa_helpers import get_fama_raw
|
||||
from openbb_qa.qa_models import (
|
||||
@@ -57,6 +53,8 @@ def normality(data: List[Data], target: str) -> OBBject[NormalityModel]:
|
||||
Normality tests summary. See qa_models.NormalityModel for details.
|
||||
"""
|
||||
|
||||
from scipy import stats
|
||||
|
||||
df = basemodel_to_df(data)
|
||||
series_target = get_target_column(df, target)
|
||||
|
||||
@@ -81,6 +79,8 @@ def normality(data: List[Data], target: str) -> OBBject[NormalityModel]:
|
||||
def capm(data: List[Data], target: str) -> OBBject[CAPMModel]:
|
||||
"""Capital Asset Pricing Model."""
|
||||
|
||||
import statsmodels.api as sm
|
||||
|
||||
df = basemodel_to_df(data)
|
||||
|
||||
df_target = get_target_columns(df, ["date", target])
|
||||
@@ -177,6 +177,9 @@ def kurtosis(data: List[Data], target: str, window: PositiveInt) -> OBBject[List
|
||||
OBBject[List[Data]]
|
||||
Kurtosis.
|
||||
"""
|
||||
|
||||
import pandas_ta as ta
|
||||
|
||||
df = basemodel_to_df(data)
|
||||
series_target = get_target_column(df, target)
|
||||
results = ta.kurtosis(close=series_target, length=window).dropna()
|
||||
@@ -250,6 +253,8 @@ def unitroot(
|
||||
Unit root tests summary.
|
||||
"""
|
||||
|
||||
from statsmodels.tsa import stattools
|
||||
|
||||
df = basemodel_to_df(data)
|
||||
series_target = get_target_column(df, target)
|
||||
|
||||
@@ -404,6 +409,9 @@ def skew(data: List[Data], target: str, window: PositiveInt) -> OBBject[List[Dat
|
||||
OBBject[List[Data]]
|
||||
Skewness.
|
||||
"""
|
||||
|
||||
import pandas_ta as ta
|
||||
|
||||
df = basemodel_to_df(data)
|
||||
series_target = get_target_column(df, target)
|
||||
results = ta.skew(close=series_target, length=window).dropna()
|
||||
@@ -421,6 +429,8 @@ def quantile(
|
||||
) -> OBBject[List[Data]]:
|
||||
"""Quantile."""
|
||||
|
||||
import pandas_ta as ta
|
||||
|
||||
df = basemodel_to_df(data)
|
||||
series_target = get_target_column(df, target)
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ from typing import Any, Literal, Optional, Tuple
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
_warn = warnings.warn
|
||||
|
||||
@@ -454,6 +453,9 @@ def clenow_momentum(
|
||||
pd.Series:
|
||||
Values for best fit line
|
||||
"""
|
||||
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
if len(values) < window:
|
||||
raise ValueError(f"Calculation asks for at least last {window} days of data")
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ class FormatterWithExceptions(logging.Formatter):
|
||||
"levelname": level_name,
|
||||
"appId": self.settings.app_id,
|
||||
"sessionId": self.settings.session_id,
|
||||
"commitHash": self.settings.commit_hash,
|
||||
"commitHash": "unknown-commit",
|
||||
"userId": self.settings.user_id,
|
||||
}
|
||||
|
||||
|
||||
@@ -113,11 +113,9 @@ class PosthogHandler(logging.Handler):
|
||||
"appName": self._settings.app_name,
|
||||
"appId": self._settings.app_id,
|
||||
"sessionId": self._settings.session_id,
|
||||
"commitHash": self._settings.commit_hash,
|
||||
"platform": self._settings.platform,
|
||||
"pythonVersion": self._settings.python_version,
|
||||
"terminalVersion": self._settings.terminal_version,
|
||||
"branch": self._settings.branch,
|
||||
}
|
||||
|
||||
if self._settings.user_id:
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
from openbb_core.app.logs.utils.system_utils import get_branch
|
||||
from openbb_core.app.logs.utils.utils import get_app_id, get_log_dir, get_session_id
|
||||
from openbb_core.app.model.system_settings import SystemSettings
|
||||
from openbb_core.app.model.user_settings import UserSettings
|
||||
@@ -33,11 +32,6 @@ class LoggingSettings:
|
||||
user_id, user_email, user_primary_usage = None, None, None
|
||||
|
||||
# System
|
||||
self.commit_hash: Optional[str] = system_settings.logging_commit_hash
|
||||
# TODO: If we can avoid get_branch making a web request we can save up to 1s of import time.
|
||||
self.branch: Optional[str] = (
|
||||
get_branch(self.commit_hash) if self.commit_hash else None
|
||||
)
|
||||
self.app_name: str = system_settings.logging_app_name
|
||||
self.app_id: str = get_app_id(user_data_directory)
|
||||
self.session_id: str = get_session_id()
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
# IMPORT STANDARD
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
|
||||
# IMPORT THIRD-PARTY
|
||||
# IMPORT INTERNAL
|
||||
|
||||
try:
|
||||
import git
|
||||
except ImportError:
|
||||
WITH_GIT = False
|
||||
else:
|
||||
WITH_GIT = True
|
||||
|
||||
GITHUB_PROJECT_URL = (
|
||||
"https://api.github.com/repos/openbb-finance/openbbterminal/branches"
|
||||
)
|
||||
|
||||
UNKNOWN_COMMIT_PLACEHOLDER = "unknown-commit"
|
||||
UNKNOWN_BRANCH_PLACEHOLDER = "unknown-branch"
|
||||
REPOSITORY_DIRECTORY = Path(
|
||||
__file__
|
||||
).parent.parent.parent.parent.parent.parent.parent.parent
|
||||
|
||||
|
||||
def get_commit_hash() -> str:
|
||||
"""Get Commit Short Hash"""
|
||||
|
||||
git_dir = REPOSITORY_DIRECTORY.joinpath(".git")
|
||||
|
||||
if WITH_GIT and git_dir.is_dir():
|
||||
repo = git.Repo(path=git_dir)
|
||||
sha = repo.head.object.hexsha
|
||||
short_sha = repo.git.rev_parse(sha, short=8)
|
||||
commit_hash = f"sha:{short_sha}"
|
||||
else:
|
||||
commit_hash = UNKNOWN_COMMIT_PLACEHOLDER
|
||||
|
||||
return commit_hash
|
||||
|
||||
|
||||
def get_branch(commit_hash: str, request_timeout: int = 5) -> str:
|
||||
"""Get Branch Name"""
|
||||
|
||||
if commit_hash == UNKNOWN_COMMIT_PLACEHOLDER:
|
||||
return UNKNOWN_BRANCH_PLACEHOLDER
|
||||
|
||||
def get_branch_commit_hash(branch: str) -> str:
|
||||
response = requests.get(
|
||||
f"{GITHUB_PROJECT_URL}/{branch}", timeout=request_timeout
|
||||
)
|
||||
return "sha:" + response.json()["commit"]["sha"][:8]
|
||||
|
||||
for branch in ["main", "develop"]:
|
||||
try:
|
||||
if get_branch_commit_hash(branch) == commit_hash:
|
||||
return branch
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
git_dir = REPOSITORY_DIRECTORY.joinpath(".git")
|
||||
if WITH_GIT and git_dir.is_dir():
|
||||
try:
|
||||
repo = git.Repo(path=git_dir)
|
||||
branch = repo.active_branch.name
|
||||
return branch
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return UNKNOWN_BRANCH_PLACEHOLDER
|
||||
@@ -13,7 +13,6 @@ from openbb_core.app.constants import (
|
||||
SYSTEM_SETTINGS_PATH,
|
||||
USER_SETTINGS_PATH,
|
||||
)
|
||||
from openbb_core.app.logs.utils.system_utils import get_commit_hash
|
||||
from openbb_core.app.model.abstract.tagged import Tagged
|
||||
|
||||
FrozenField = partial(Field, allow_mutation=False)
|
||||
@@ -104,8 +103,3 @@ class SystemSettings(Tagged):
|
||||
if value not in ["stdout", "stderr", "noop", "file", "posthog"]:
|
||||
raise ValueError("Invalid logging handler")
|
||||
return v
|
||||
|
||||
@validator("logging_commit_hash", allow_reuse=True, always=True)
|
||||
@classmethod
|
||||
def validate_commit_hash(cls, v):
|
||||
return v or get_commit_hash()
|
||||
|
||||
@@ -11,7 +11,6 @@ logging_settings = Mock()
|
||||
logging_settings.app_name = "test_app_name"
|
||||
logging_settings.app_id = "test_app_id"
|
||||
logging_settings.session_id = "test_session_id"
|
||||
logging_settings.commit_hash = "test_commit_hash"
|
||||
logging_settings.user_id = "test_user_id"
|
||||
|
||||
|
||||
@@ -296,7 +295,7 @@ def test_format(formatter):
|
||||
formatted_log = formatter.format(log_record)
|
||||
|
||||
# Assertions
|
||||
expected_log = "INFO|test_app_name|test_commit_hash|test_app_id|test_session_id|test_user_id|Filtered log message"
|
||||
expected_log = "INFO|test_app_name|unknown-commit|test_app_id|test_session_id|test_user_id|Filtered log message"
|
||||
|
||||
print(expected_log)
|
||||
print(formatted_log)
|
||||
|
||||
@@ -16,11 +16,9 @@ class MockLoggingSettings:
|
||||
session_id,
|
||||
frequency,
|
||||
appid,
|
||||
commit_hash,
|
||||
platform,
|
||||
python_version,
|
||||
terminal_version,
|
||||
branch,
|
||||
userid,
|
||||
):
|
||||
self.app_name = app_name
|
||||
@@ -28,11 +26,9 @@ class MockLoggingSettings:
|
||||
self.session_id = session_id
|
||||
self.frequency = frequency
|
||||
self.app_id = appid
|
||||
self.commit_hash = commit_hash
|
||||
self.platform = platform
|
||||
self.python_version = python_version
|
||||
self.terminal_version = terminal_version
|
||||
self.branch = branch
|
||||
self.user_id = userid
|
||||
|
||||
|
||||
@@ -45,11 +41,9 @@ logging_settings.user_logs_directory.absolute.return_value = Path(
|
||||
logging_settings.session_id = "session123"
|
||||
logging_settings.frequency = "H"
|
||||
logging_settings.app_id = "test123"
|
||||
logging_settings.commit_hash = "commit123"
|
||||
logging_settings.platform = "Windows"
|
||||
logging_settings.python_version = "3.9"
|
||||
logging_settings.terminal_version = "1.2.3"
|
||||
logging_settings.branch = "main"
|
||||
logging_settings.user_id = "user123"
|
||||
|
||||
|
||||
@@ -203,11 +197,9 @@ def test_log_to_dict(handler, log_info, expected_dict):
|
||||
"appName": "TestApp",
|
||||
"appId": "test123",
|
||||
"sessionId": "session123",
|
||||
"commitHash": "commit123",
|
||||
"platform": "Windows",
|
||||
"pythonVersion": "3.9",
|
||||
"terminalVersion": "1.2.3",
|
||||
"branch": "main",
|
||||
"userId": "user123",
|
||||
},
|
||||
),
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
# TODO : add tests once the code is fixed
|
||||
|
||||
|
||||
def test_get_commit_hash():
|
||||
pass
|
||||
|
||||
|
||||
def test_get_branch():
|
||||
pass
|
||||
@@ -1,5 +1,4 @@
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from openbb_core.app.model.system_settings import SystemSettings
|
||||
@@ -146,24 +145,3 @@ def test_validate_logging_handlers(handlers, valid):
|
||||
else:
|
||||
with pytest.raises(ValueError, match="Invalid logging handler"):
|
||||
SystemSettings.validate_logging_handlers(handlers)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"commit_hash, expected_result",
|
||||
[
|
||||
# Test case: Valid commit hash provided
|
||||
("1234567890", "1234567890"),
|
||||
# Test case: Empty commit hash, mocked get_commit_hash returns "abcdef1234"
|
||||
("", "abcdef1234"),
|
||||
],
|
||||
)
|
||||
def test_validate_commit_hash(commit_hash, expected_result):
|
||||
with patch(
|
||||
"openbb_core.app.model.system_settings.get_commit_hash",
|
||||
return_value="abcdef1234",
|
||||
):
|
||||
# Act
|
||||
result = SystemSettings.validate_commit_hash(commit_hash)
|
||||
|
||||
# Assert
|
||||
assert result == expected_result
|
||||
|
||||
Reference in New Issue
Block a user