|
| 1 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Classes to modify Predictor code to be compatible |
| 14 | +with version 2.0 and later of the SageMaker Python SDK. |
| 15 | +""" |
| 16 | +from __future__ import absolute_import |
| 17 | + |
| 18 | +import ast |
| 19 | + |
| 20 | +from sagemaker.cli.compatibility.v2.modifiers.modifier import Modifier |
| 21 | + |
| 22 | +BASE_PREDICTOR = "RealTimePredictor" |
| 23 | +PREDICTORS = { |
| 24 | + "FactorizationMachinesPredictor": ("sagemaker", "sagemaker.amazon.factorization_machines"), |
| 25 | + "IPInsightsPredictor": ("sagemaker", "sagemaker.amazon.ipinsights"), |
| 26 | + "KMeansPredictor": ("sagemaker", "sagemaker.amazon.kmeans"), |
| 27 | + "KNNPredictor": ("sagemaker", "sagemaker.amazon.knn"), |
| 28 | + "LDAPredictor": ("sagemaker", "sagemaker.amazon.lda"), |
| 29 | + "LinearLearnerPredictor": ("sagemaker", "sagemaker.amazon.linear_learner"), |
| 30 | + "NTMPredictor": ("sagemaker", "sagemaker.amazon.ntm"), |
| 31 | + "PCAPredictor": ("sagemaker", "sagemaker.amazon.pca"), |
| 32 | + "RandomCutForestPredictor": ("sagemaker", "sagemaker.amazon.randomcutforest"), |
| 33 | + "RealTimePredictor": ("sagemaker", "sagemaker.predictor"), |
| 34 | + "SparkMLPredictor": ("sagemaker.sparkml", "sagemaker.sparkml.model"), |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +class PredictorConstructorRefactor(Modifier): |
| 39 | + """A class to refactor *Predictor class and refactor endpoint attribute.""" |
| 40 | + |
| 41 | + def node_should_be_modified(self, node): |
| 42 | + """Checks if the ``ast.Call`` node instantiates a class of interest. |
| 43 | +
|
| 44 | + This looks for the following calls: |
| 45 | +
|
| 46 | + - ``sagemaker.<my>.<namespace>.<MyPredictor>`` |
| 47 | + - ``sagemaker.<namespace>.<MyPredictor>`` |
| 48 | + - ``<MyPredictor>`` |
| 49 | +
|
| 50 | + Args: |
| 51 | + node (ast.Call): a node that represents a function call. For more, |
| 52 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + bool: If the ``ast.Call`` instantiates a class of interest. |
| 56 | + """ |
| 57 | + return any(_matching(node, name, namespaces) for name, namespaces in PREDICTORS.items()) |
| 58 | + |
| 59 | + def modify_node(self, node): |
| 60 | + """Modifies the ``ast.Call`` node to call ``Predictor`` instead. |
| 61 | +
|
| 62 | + Also renames ``endpoint`` attribute to ``endpoint_name``. |
| 63 | +
|
| 64 | + Args: |
| 65 | + node (ast.Call): a node that represents a *Predictor constructor. |
| 66 | + """ |
| 67 | + _rename_class(node) |
| 68 | + _rename_endpoint(node) |
| 69 | + |
| 70 | + |
| 71 | +def _matching(node, name, namespaces): |
| 72 | + """Determines if the node matches the constructor name in the right namespace""" |
| 73 | + if _matching_name(node, name): |
| 74 | + return True |
| 75 | + |
| 76 | + if not _matching_attr(node, name): |
| 77 | + return False |
| 78 | + |
| 79 | + return any(_matching_namespace(node, namespace) for namespace in namespaces) |
| 80 | + |
| 81 | + |
| 82 | +def _matching_name(node, name): |
| 83 | + """Determines if the node is an ast.Name node with a matching name""" |
| 84 | + return isinstance(node.func, ast.Name) and node.func.id == name |
| 85 | + |
| 86 | + |
| 87 | +def _matching_attr(node, name): |
| 88 | + """Determines if the node is an ast.Attribute node with a matching name""" |
| 89 | + return isinstance(node.func, ast.Attribute) and node.func.attr == name |
| 90 | + |
| 91 | + |
| 92 | +def _matching_namespace(node, namespace): |
| 93 | + """Determines if the node corresponds to a matching namespace""" |
| 94 | + names = namespace.split(".") |
| 95 | + name, value = names.pop(), node.func.value |
| 96 | + while isinstance(value, ast.Attribute) and len(names) > 0: |
| 97 | + if value.attr != name: |
| 98 | + return False |
| 99 | + name, value = names.pop(), value.value |
| 100 | + |
| 101 | + return isinstance(value, ast.Name) and value.id == name |
| 102 | + |
| 103 | + |
| 104 | +def _rename_class(node): |
| 105 | + """Renames the RealTimePredictor base class to Predictor""" |
| 106 | + if _matching_name(node, BASE_PREDICTOR): |
| 107 | + node.func.id = "Predictor" |
| 108 | + elif _matching_attr(node, BASE_PREDICTOR): |
| 109 | + node.func.attr = "Predictor" |
| 110 | + |
| 111 | + |
| 112 | +def _rename_endpoint(node): |
| 113 | + """Renames keyword endpoint argument to endpoint_name""" |
| 114 | + for keyword in node.keywords: |
| 115 | + if keyword.arg == "endpoint": |
| 116 | + keyword.arg = "endpoint_name" |
| 117 | + break |
| 118 | + |
| 119 | + |
| 120 | +class PredictorImportFromRenamer(Modifier): |
| 121 | + """A class to update import statements of ``RealTimePredictor``.""" |
| 122 | + |
| 123 | + def node_should_be_modified(self, node): |
| 124 | + """Checks if the import statement imports ``RealTimePredictor`` from the correct module. |
| 125 | +
|
| 126 | + Args: |
| 127 | + node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement. |
| 128 | + For more, see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 129 | +
|
| 130 | + Returns: |
| 131 | + bool: If the import statement imports ``RealTimePredictor`` from the correct module. |
| 132 | + """ |
| 133 | + return node.module in PREDICTORS[BASE_PREDICTOR] and any( |
| 134 | + name.name == BASE_PREDICTOR for name in node.names |
| 135 | + ) |
| 136 | + |
| 137 | + def modify_node(self, node): |
| 138 | + """Changes the ``ast.ImportFrom`` node's name from ``RealTimePredictor`` to ``Predictor``. |
| 139 | +
|
| 140 | + Args: |
| 141 | + node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement. |
| 142 | + For more, see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 143 | + """ |
| 144 | + for name in node.names: |
| 145 | + if name.name == BASE_PREDICTOR: |
| 146 | + name.name = "Predictor" |
0 commit comments