|
| 1 | +# Copyright 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 | +"""This module contains code to test SageMaker ``LineageQueryResult.visualize()``""" |
| 14 | +from __future__ import absolute_import |
| 15 | +import time |
| 16 | +import os |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +import sagemaker.lineage.query |
| 21 | +from sagemaker.lineage.query import LineageQueryDirectionEnum |
| 22 | +from tests.integ.sagemaker.lineage.helpers import name, LineageResourceHelper |
| 23 | + |
| 24 | + |
| 25 | +def test_LineageResourceHelper(sagemaker_session): |
| 26 | + # check if LineageResourceHelper works properly |
| 27 | + lineage_resource_helper = LineageResourceHelper(sagemaker_session=sagemaker_session) |
| 28 | + try: |
| 29 | + art1 = lineage_resource_helper.create_artifact(artifact_name=name()) |
| 30 | + art2 = lineage_resource_helper.create_artifact(artifact_name=name()) |
| 31 | + lineage_resource_helper.create_association(source_arn=art1, dest_arn=art2) |
| 32 | + except Exception as e: |
| 33 | + print(e) |
| 34 | + assert False |
| 35 | + finally: |
| 36 | + lineage_resource_helper.clean_all() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.skip("visualizer load test") |
| 40 | +def test_wide_graph_visualize(sagemaker_session): |
| 41 | + lineage_resource_helper = LineageResourceHelper(sagemaker_session=sagemaker_session) |
| 42 | + wide_graph_root_arn = lineage_resource_helper.create_artifact(artifact_name=name()) |
| 43 | + |
| 44 | + # create wide graph |
| 45 | + # Artifact ----> Artifact |
| 46 | + # \ \ \-> Artifact |
| 47 | + # \ \--> Artifact |
| 48 | + # \---> ... |
| 49 | + try: |
| 50 | + for i in range(150): |
| 51 | + artifact_arn = lineage_resource_helper.create_artifact(artifact_name=name()) |
| 52 | + lineage_resource_helper.create_association( |
| 53 | + source_arn=wide_graph_root_arn, dest_arn=artifact_arn |
| 54 | + ) |
| 55 | + |
| 56 | + lq = sagemaker.lineage.query.LineageQuery(sagemaker_session) |
| 57 | + lq_result = lq.query(start_arns=[wide_graph_root_arn]) |
| 58 | + lq_result.visualize(path="wideGraph.html") |
| 59 | + |
| 60 | + print("vertex len = ") |
| 61 | + print(len(lq_result.vertices)) |
| 62 | + assert False |
| 63 | + |
| 64 | + except Exception as e: |
| 65 | + print(e) |
| 66 | + assert False |
| 67 | + |
| 68 | + finally: |
| 69 | + lineage_resource_helper.clean_all() |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.skip("visualizer load test") |
| 73 | +def test_long_graph_visualize(sagemaker_session): |
| 74 | + lineage_resource_helper = LineageResourceHelper(sagemaker_session=sagemaker_session) |
| 75 | + long_graph_root_arn = lineage_resource_helper.create_artifact(artifact_name=name()) |
| 76 | + last_arn = long_graph_root_arn |
| 77 | + |
| 78 | + # create long graph |
| 79 | + # Artifact -> Artifact -> ... -> Artifact |
| 80 | + try: |
| 81 | + for i in range(10): |
| 82 | + new_artifact_arn = lineage_resource_helper.create_artifact(artifact_name=name()) |
| 83 | + lineage_resource_helper.create_association( |
| 84 | + source_arn=last_arn, dest_arn=new_artifact_arn |
| 85 | + ) |
| 86 | + last_arn = new_artifact_arn |
| 87 | + |
| 88 | + lq = sagemaker.lineage.query.LineageQuery(sagemaker_session) |
| 89 | + lq_result = lq.query( |
| 90 | + start_arns=[long_graph_root_arn], direction=LineageQueryDirectionEnum.DESCENDANTS |
| 91 | + ) |
| 92 | + # max depth = 10 -> graph rendered only has length of ten (in DESCENDANTS direction) |
| 93 | + lq_result.visualize(path="longGraph.html") |
| 94 | + |
| 95 | + except Exception as e: |
| 96 | + print(e) |
| 97 | + assert False |
| 98 | + |
| 99 | + finally: |
| 100 | + lineage_resource_helper.clean_all() |
| 101 | + |
| 102 | + |
| 103 | +def test_graph_visualize(sagemaker_session, extract_data_from_html): |
| 104 | + lineage_resource_helper = LineageResourceHelper(sagemaker_session=sagemaker_session) |
| 105 | + |
| 106 | + # create lineage data |
| 107 | + # image artifact ------> model artifact(startarn) -> model deploy action -> endpoint context |
| 108 | + # /-> |
| 109 | + # dataset artifact -/ |
| 110 | + try: |
| 111 | + graph_startarn = lineage_resource_helper.create_artifact( |
| 112 | + artifact_name=name(), artifact_type="Model" |
| 113 | + ) |
| 114 | + image_artifact = lineage_resource_helper.create_artifact( |
| 115 | + artifact_name=name(), artifact_type="Image" |
| 116 | + ) |
| 117 | + lineage_resource_helper.create_association( |
| 118 | + source_arn=image_artifact, dest_arn=graph_startarn, association_type="ContributedTo" |
| 119 | + ) |
| 120 | + dataset_artifact = lineage_resource_helper.create_artifact( |
| 121 | + artifact_name=name(), artifact_type="DataSet" |
| 122 | + ) |
| 123 | + lineage_resource_helper.create_association( |
| 124 | + source_arn=dataset_artifact, dest_arn=graph_startarn, association_type="AssociatedWith" |
| 125 | + ) |
| 126 | + modeldeploy_action = lineage_resource_helper.create_action( |
| 127 | + action_name=name(), action_type="ModelDeploy" |
| 128 | + ) |
| 129 | + lineage_resource_helper.create_association( |
| 130 | + source_arn=graph_startarn, dest_arn=modeldeploy_action, association_type="ContributedTo" |
| 131 | + ) |
| 132 | + endpoint_context = lineage_resource_helper.create_context( |
| 133 | + context_name=name(), context_type="Endpoint" |
| 134 | + ) |
| 135 | + lineage_resource_helper.create_association( |
| 136 | + source_arn=modeldeploy_action, |
| 137 | + dest_arn=endpoint_context, |
| 138 | + association_type="AssociatedWith", |
| 139 | + ) |
| 140 | + time.sleep(3) |
| 141 | + |
| 142 | + # visualize |
| 143 | + lq = sagemaker.lineage.query.LineageQuery(sagemaker_session) |
| 144 | + lq_result = lq.query(start_arns=[graph_startarn]) |
| 145 | + lq_result.visualize(path="testGraph.html") |
| 146 | + |
| 147 | + # check generated graph info |
| 148 | + fo = open("testGraph.html", "r") |
| 149 | + lines = fo.readlines() |
| 150 | + for line in lines: |
| 151 | + if "nodes = " in line: |
| 152 | + node = line |
| 153 | + if "edges = " in line: |
| 154 | + edge = line |
| 155 | + |
| 156 | + node_dict = extract_data_from_html(node) |
| 157 | + edge_dict = extract_data_from_html(edge) |
| 158 | + |
| 159 | + # check node number |
| 160 | + assert len(node_dict) == 5 |
| 161 | + |
| 162 | + expected_nodes = { |
| 163 | + graph_startarn: { |
| 164 | + "color": "#146eb4", |
| 165 | + "label": "Model", |
| 166 | + "shape": "star", |
| 167 | + "title": "Artifact", |
| 168 | + }, |
| 169 | + image_artifact: { |
| 170 | + "color": "#146eb4", |
| 171 | + "label": "Image", |
| 172 | + "shape": "dot", |
| 173 | + "title": "Artifact", |
| 174 | + }, |
| 175 | + dataset_artifact: { |
| 176 | + "color": "#146eb4", |
| 177 | + "label": "DataSet", |
| 178 | + "shape": "dot", |
| 179 | + "title": "Artifact", |
| 180 | + }, |
| 181 | + modeldeploy_action: { |
| 182 | + "color": "#88c396", |
| 183 | + "label": "ModelDeploy", |
| 184 | + "shape": "dot", |
| 185 | + "title": "Action", |
| 186 | + }, |
| 187 | + endpoint_context: { |
| 188 | + "color": "#ff9900", |
| 189 | + "label": "Endpoint", |
| 190 | + "shape": "dot", |
| 191 | + "title": "Context", |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + # check node properties |
| 196 | + for node in node_dict: |
| 197 | + for label, val in expected_nodes[node["id"]].items(): |
| 198 | + assert node[label] == val |
| 199 | + |
| 200 | + # check edge number |
| 201 | + assert len(edge_dict) == 4 |
| 202 | + |
| 203 | + expected_edges = { |
| 204 | + image_artifact: { |
| 205 | + "from": image_artifact, |
| 206 | + "to": graph_startarn, |
| 207 | + "title": "ContributedTo", |
| 208 | + }, |
| 209 | + dataset_artifact: { |
| 210 | + "from": dataset_artifact, |
| 211 | + "to": graph_startarn, |
| 212 | + "title": "AssociatedWith", |
| 213 | + }, |
| 214 | + graph_startarn: { |
| 215 | + "from": graph_startarn, |
| 216 | + "to": modeldeploy_action, |
| 217 | + "title": "ContributedTo", |
| 218 | + }, |
| 219 | + modeldeploy_action: { |
| 220 | + "from": modeldeploy_action, |
| 221 | + "to": endpoint_context, |
| 222 | + "title": "AssociatedWith", |
| 223 | + }, |
| 224 | + } |
| 225 | + |
| 226 | + # check edge properties |
| 227 | + for edge in edge_dict: |
| 228 | + for label, val in expected_edges[edge["from"]].items(): |
| 229 | + assert edge[label] == val |
| 230 | + |
| 231 | + except Exception as e: |
| 232 | + print(e) |
| 233 | + assert False |
| 234 | + |
| 235 | + finally: |
| 236 | + lineage_resource_helper.clean_all() |
| 237 | + os.remove("testGraph.html") |
0 commit comments