Skip to content

Commit

Permalink
chore: catch explicit model not found exception in mlflow (#133)
Browse files Browse the repository at this point in the history
Signed-off-by: Avik Basu <[email protected]>
  • Loading branch information
ab93 authored Jan 20, 2023
1 parent 8b37db8 commit 7fdd599
Show file tree
Hide file tree
Showing 28 changed files with 336 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ coverage:
project:
default:
target: auto
threshold: 1%
threshold: 5%
patch:
default:
target: auto
threshold: 1%
threshold: 10%
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# These owners will be the default owners for everything in
# the repo. Unless a later match takes precedence
* @ab93 @vigith @whynowy
* @ab93 @vigith @nkoppisetty
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ lint: format

# install all dependencies
setup:
poetry install --with dev --all-extras
poetry install --with dev,torch --all-extras --no-root

# test your application (tests in the tests/ directory)
test:
Expand Down
12 changes: 12 additions & 0 deletions numalogic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging


Expand Down
12 changes: 12 additions & 0 deletions numalogic/_constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import os

NUMALOGIC_DIR = os.path.dirname(__file__)
Expand Down
12 changes: 12 additions & 0 deletions numalogic/models/autoencoder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from numalogic.models.autoencoder.trainer import AutoencoderTrainer

__all__ = ["AutoencoderTrainer"]
12 changes: 12 additions & 0 deletions numalogic/models/autoencoder/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from abc import ABCMeta

import pytorch_lightning as pl
Expand Down
12 changes: 12 additions & 0 deletions numalogic/models/autoencoder/trainer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging

import pytorch_lightning as pl
Expand Down
12 changes: 12 additions & 0 deletions numalogic/models/autoencoder/variants/conv.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging
from typing import Tuple

Expand Down
12 changes: 12 additions & 0 deletions numalogic/models/autoencoder/variants/lstm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging
from typing import Tuple

Expand Down
12 changes: 12 additions & 0 deletions numalogic/models/autoencoder/variants/transformer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Tuple

import torch
Expand Down
12 changes: 12 additions & 0 deletions numalogic/models/autoencoder/variants/vanilla.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Tuple, Sequence

import torch
Expand Down
12 changes: 12 additions & 0 deletions numalogic/postprocess.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import numpy as np
from numpy.typing import ArrayLike

Expand Down
14 changes: 14 additions & 0 deletions numalogic/preprocess/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from numalogic.preprocess.transformer import LogTransformer, StaticPowerTransformer

__all__ = ["LogTransformer", "StaticPowerTransformer"]
12 changes: 12 additions & 0 deletions numalogic/preprocess/transformer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging

import numpy as np
Expand Down
12 changes: 12 additions & 0 deletions numalogic/registry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from numalogic.registry.artifact import ArtifactManager
from numalogic.registry.artifact import ArtifactData

Expand Down
14 changes: 14 additions & 0 deletions numalogic/registry/artifact.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
from typing import Sequence, Any, Dict
Expand All @@ -19,6 +31,8 @@ class ArtifactManager(metaclass=ABCMeta):
:param uri: server/connection uri
"""

__slots__ = ("uri",)

def __init__(self, uri: str):
self.uri = uri

Expand Down
30 changes: 26 additions & 4 deletions numalogic/registry/mlflow_registry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging
from enum import Enum
from typing import Optional, Sequence
Expand All @@ -6,6 +18,7 @@
import mlflow.pytorch
from mlflow.entities.model_registry import ModelVersion
from mlflow.exceptions import RestException
from mlflow.protos.databricks_pb2 import ErrorCode, RESOURCE_DOES_NOT_EXIST
from mlflow.tracking import MlflowClient

from numalogic.registry import ArtifactManager, ArtifactData
Expand Down Expand Up @@ -39,9 +52,9 @@ class MLflowRegistry(ArtifactManager):
Examples
--------
>>> from numalogic.models.autoencoder.variants.vanilla import VanillaAE
>>> from numalogic.registry.mlflow_registry import MLflowRegistry
>>> from sklearn.pipeline import make_pipeline
>>> from numalogic.models.autoencoder.variants import VanillaAE
>>> from numalogic.registry import MLflowRegistry
>>> from sklearn.preprocessing import StandardScaler
>>>
>>> data = [[0, 0], [0, 0], [1, 1], [1, 1]]
>>> scaler = StandardScaler.fit(data)
Expand All @@ -50,6 +63,7 @@ class MLflowRegistry(ArtifactManager):
>>> artifact_data = registry.load(skeys=["model"], dkeys=["AE"])
"""

__slots__ = ("client", "handler", "models_to_retain")
_TRACKING_URI = None

def __new__(
Expand Down Expand Up @@ -132,6 +146,14 @@ def load(
_LOGGER.info("Successfully loaded model metadata from Mlflow!")

return ArtifactData(artifact=model, metadata=metadata, extras=dict(version_info))
except RestException as mlflow_err:
if ErrorCode.Value(mlflow_err.error_code) == RESOURCE_DOES_NOT_EXIST:
_LOGGER.info("Model not found with key: %s", model_key)
else:
_LOGGER.exception(
"Mlflow error when loading a model with key: %s: %r", model_key, mlflow_err
)
return None
except Exception as ex:
_LOGGER.exception("Error when loading a model with key: %s: %r", model_key, ex)
return None
Expand Down Expand Up @@ -164,7 +186,7 @@ def save(
_LOGGER.info("Successfully inserted model %s to Mlflow", model_key)
return model_version
except Exception as ex:
_LOGGER.exception("Error when saving a model with key: %s: %r", model_key, ex)
_LOGGER.exception("Unhandled error when saving a model with key: %s: %r", model_key, ex)
return None
finally:
mlflow.end_run()
Expand Down
12 changes: 12 additions & 0 deletions numalogic/synthetic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from numalogic.synthetic.timeseries import SyntheticTSGenerator
from numalogic.synthetic.anomalies import AnomalyGenerator
from numalogic.synthetic.sparsity import SparsityGenerator
Expand Down
12 changes: 12 additions & 0 deletions numalogic/synthetic/anomalies.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Sequence, List

import numpy as np
Expand Down
12 changes: 12 additions & 0 deletions numalogic/synthetic/sparsity.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import random


Expand Down
12 changes: 12 additions & 0 deletions numalogic/synthetic/timeseries.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Copyright 2022 The Numaproj Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Tuple

import numpy as np
Expand Down
Loading

0 comments on commit 7fdd599

Please sign in to comment.