Skip to content

Commit

Permalink
Merge pull request #53 from seeq12/bugfix/ao/correlation-errors-out-d…
Browse files Browse the repository at this point in the history
…ue-to-lack-of-crosscorrelation-UDF-CRAB-45677

CRAB 45677: Fix for correlation errors out due to lack of crosscorrelation udf
  • Loading branch information
wizzle3d authored Nov 18, 2024
2 parents 8b2ab78 + f182804 commit 651d8bb
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 74 deletions.
4 changes: 2 additions & 2 deletions seeq/addons/correlation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ._version import __version__
from ._seeq_formulas import correlation_udfs, signals_from_formula
from ._seeq_formulas import correlation_udfs, signals_from_formula, create_udfs
from ._preprocessor import _validate_df, default_preprocessing_wrapper
from ._cross_correlations import cross_corr_matrix_raw, cross_corr_matrix_lagged, lags_coeffs
from ._heatmap import _heatmap_plot, heatmap
Expand All @@ -13,4 +13,4 @@
'_heatmap', 'create_worksheet', '_create_worksheet', 'get_workbook', 'worksheet_with_lagged_signals',
'create_lagged_signals', 'pairplot', 'worksheet_corrs_and_time_shifts',
'correlation_udfs', 'signals_from_formula', 'CorrelationHeatmap', '_validate_df', 'lags_coeffs',
'default_preprocessing_wrapper', 'utils']
'default_preprocessing_wrapper', 'utils', 'create_udfs']
52 changes: 2 additions & 50 deletions seeq/addons/correlation/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
# noinspection PyProtectedMember
from seeq.spy import _url
from ._copy import copy
from .utils import get_user, get_user_group, get_seeq_url
from . import correlation_udfs
from .utils import get_seeq_url, DEFAULT_GROUP, DEFAULT_USERS
from . import create_udfs

NB_EXTENSIONS = ['widgetsnbextension', 'ipyvuetify', 'ipyvue']
DEPLOYMENT_FOLDER = 'deployment'
CORRELATION_NOTEBOOK = "correlation_analysis_master.ipynb"
DEFAULT_GROUP = ['Everyone']
DEFAULT_USERS = []


def install_app(sdl_url_, *, sort_key='a', permissions_group: list = None, permissions_users: list = None):
Expand Down Expand Up @@ -121,52 +119,6 @@ def logging_attempts(_user):
if count > allowed_attempts:
raise RuntimeError("Number of login attempts exceeded")


def create_udfs(api_client, *, permissions_groups: list = None, permissions_users: list = None):
"""
Creates the required Formula UDFs for the Correlation app
Parameters
----------
api_client: seeq.sdk.api_client.ApiClient
The seeq.sdk API client that handles the client-server
communication
permissions_groups: list
Names of the Seeq groups that will have access to each tool
permissions_users: list
Names of Seeq users that will have access to each tool
Returns
--------
-: None
The Correlation UDFs will be available in Seeq Workbench
"""

permissions_groups = permissions_groups if permissions_groups else DEFAULT_GROUP
permissions_users = permissions_users if permissions_users else DEFAULT_USERS
print("\n\nCreating CrossCorrelation UDFs...")
user_groups_api = sdk.UserGroupsApi(api_client)
users_api = sdk.UsersApi(spy.client)
items_api = sdk.ItemsApi(api_client)
pkg_id = correlation_udfs(api_client)

# assign group permissions
for group_name in permissions_groups:
group = get_user_group(group_name, user_groups_api)
if group:
ace_input = sdk.AceInputV1(identity_id=group.items[0].id, permissions=sdk.PermissionsV1(read=True))
items_api.add_access_control_entry(id=pkg_id, body=ace_input)

# assign user permissions
for user_name in permissions_users:
current_user = get_user(user_name, users_api)
if current_user:
ace_input = sdk.AceInputV1(identity_id=current_user.users[0].id,
permissions=sdk.PermissionsV1(read=True))
items_api.add_access_control_entry(id=pkg_id, body=ace_input)

print("DONE")


def cli_interface():
""" Command line utility to install the Correlation Add-on Tool """
parser = argparse.ArgumentParser(description='Install Correlation as a Seeq Add-on Tool')
Expand Down
62 changes: 51 additions & 11 deletions seeq/addons/correlation/_seeq_formulas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import textwrap
from seeq import sdk
from seeq import sdk, spy
from seeq.sdk.rest import ApiException
from .utils import check_udf_package, get_user_group, get_user, DEFAULT_USERS, DEFAULT_GROUP

pearson_formula = textwrap.dedent(
"""
Expand Down Expand Up @@ -163,16 +164,10 @@ def correlation_udfs(api_client):
creator_name = 'Alberto Rivas'
creator_contact_info = 'applied.research@seeq.com'
formulas_api = sdk.FormulasApi(api_client)
try:
pkg = formulas_api.get_package(package_name=package_name)
if pkg.name == package_name:
print(f"Overwriting CrossCorrelation package")
formulas_api.delete_package(package_name=package_name)
except ApiException as e:
if 'not found' in e.reason.lower():
pass
else:
raise e
found = check_udf_package(package_name, api_client)
if found:
print(f"Overwriting CrossCorrelation package")
formulas_api.delete_package(package_name=package_name)

# Create the Formula Package
package_input = sdk.FormulaPackageInputV1(creator_name=creator_name, creator_contact_info=creator_contact_info)
Expand Down Expand Up @@ -290,3 +285,48 @@ def signals_from_formula(signal1_id, signal_ref_id, workbook_id, formula_type=No
)
r = signals_api.create_signal_with_http_info(body=payload)[0]
return r


def create_udfs(api_client, *, permissions_groups: list = None, permissions_users: list = None):
"""
Creates the required Formula UDFs for the Correlation app
Parameters
----------
api_client: seeq.sdk.api_client.ApiClient
The seeq.sdk API client that handles the client-server
communication
permissions_groups: list
Names of the Seeq groups that will have access to each tool
permissions_users: list
Names of Seeq users that will have access to each tool
Returns
--------
-: None
The Correlation UDFs will be available in Seeq Workbench
"""

permissions_groups = permissions_groups if permissions_groups else DEFAULT_GROUP
permissions_users = permissions_users if permissions_users else DEFAULT_USERS
print("\n\nCreating CrossCorrelation UDFs...")
user_groups_api = sdk.UserGroupsApi(api_client)
users_api = sdk.UsersApi(spy.client)
items_api = sdk.ItemsApi(api_client)
pkg_id = correlation_udfs(api_client)

# assign group permissions
for group_name in permissions_groups:
group = get_user_group(group_name, user_groups_api)
if group:
ace_input = sdk.AceInputV1(identity_id=group.items[0].id, permissions=sdk.PermissionsV1(read=True))
items_api.add_access_control_entry(id=pkg_id, body=ace_input)

# assign user permissions
for user_name in permissions_users:
current_user = get_user(user_name, users_api)
if current_user:
ace_input = sdk.AceInputV1(identity_id=current_user.users[0].id,
permissions=sdk.PermissionsV1(read=True))
items_api.add_access_control_entry(id=pkg_id, body=ace_input)

print("DONE")
2 changes: 1 addition & 1 deletion seeq/addons/correlation/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.98'
__version__ = '0.1.99'
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,35 @@
},
"outputs": [],
"source": [
"from seeq.addons import correlation\n"
"from seeq.addons import correlation\n",
"from seeq import spy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C = correlation.CorrelationHeatmap(sdl_notebook_url=jupyter_notebook_url)\n",
"C.run()\n"
],
"found = correlation.utils.check_udf_package()\n",
"if not found:\n",
" correlation.create_udfs(spy.client)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
"outputs": [],
"source": [
"C = correlation.CorrelationHeatmap(sdl_notebook_url=jupyter_notebook_url)\n",
"C.run()\n"
]
}
],
"metadata": {
Expand All @@ -48,4 +60,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
8 changes: 5 additions & 3 deletions seeq/addons/correlation/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from ._common import validate_argument_types, print_red, path_list_to_string, path_string_to_list
from ._cache_management import clear_cache_all
from ._permissions import get_user, get_user_group
from ._sdl import pull_only_signals, get_worksheet_url, get_workbook_worksheet_workstep_ids, get_seeq_url
from ._permissions import get_user, get_user_group, DEFAULT_USERS, DEFAULT_GROUP
from ._sdl import (pull_only_signals, get_worksheet_url, get_workbook_worksheet_workstep_ids, get_seeq_url,
check_udf_package)
from ._seeq_new_content import create_condition, create_workstep_signals


__all__ = ['validate_argument_types', 'print_red', 'create_condition', 'create_workstep_signals', 'get_user',
'get_user_group', 'pull_only_signals', 'get_worksheet_url', 'get_workbook_worksheet_workstep_ids',
'clear_cache_all', 'get_seeq_url', 'path_list_to_string', 'path_string_to_list']
'clear_cache_all', 'get_seeq_url', 'path_list_to_string', 'path_string_to_list', 'check_udf_package',
'DEFAULT_USERS', 'DEFAULT_GROUP']
4 changes: 4 additions & 0 deletions seeq/addons/correlation/utils/_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from . import print_red


DEFAULT_GROUP = ['Everyone']
DEFAULT_USERS = []


def get_user_group(group_name, user_groups_api):
try:
group = user_groups_api.get_user_groups(name_search=group_name)
Expand Down
17 changes: 16 additions & 1 deletion seeq/addons/correlation/utils/_sdl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pandas as pd
from seeq import spy
from seeq import spy, sdk
from seeq.sdk.rest import ApiException
from urllib.parse import urlparse, unquote, parse_qs
import ipaddress

Expand Down Expand Up @@ -95,3 +96,17 @@ def get_seeq_url():
return spy.session.private_url

return None

def check_udf_package(name="CrossCorrelations", api_client=None):
if api_client:
formulas_api = sdk.FormulasApi(api_client)
else:
formulas_api = sdk.FormulasApi(spy.client)
try:
formulas_api.get_package(package_name=name)
return True
except ApiException as e:
if 'not found' in e.reason.lower():
return False
else:
raise e

0 comments on commit 651d8bb

Please sign in to comment.