def test_create_model(): with requests_mock.mock() as m: rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.AGENT) model_id = "model_5906255e-0a3d-4fef-8653-8d41911264fb" m.get(rh.url_get_uuid("model"), json={"id": model_id}) ion = ION() ion.id = "bdc2ee10-767c-4524-ba72-8268a3894bff" mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) model_data = "MODEL_DATA" model = mh.create_model(name="my model", model_format=ModelFormat.TEXT, description="test model") model_file = os.path.join(os.path.sep, "tmp", str(uuid.uuid4())) f = open(model_file, 'w') f.write(model_data) f.close() model.set_model_path(model_file) assert model.get_id() == model_id os.remove(model_file) rh.done()
def test_publish_model_rest(): with requests_mock.mock() as m: rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.AGENT, mlops_server="localhost", mlops_port="4567") model_id = "model_5906255e-0a3d-4fef-8653-8d41911264fb" m.post('http://localhost:4567/models', json=model_id) m.get(rh.url_get_uuid("model"), json={"id": model_id}) ion = ION() ion.id = "bdc2ee10-767c-4524-ba72-8268a3894bff" mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) model_data = "MODEL_DATA" model = mh.create_model(name="my model", model_format=ModelFormat.TEXT, description="test model", user_defined="whatever I want goes here") model_file = os.path.join(os.path.sep, "tmp", str(uuid.uuid4())) f = open(model_file, 'w') f.write(model_data) f.close() model.set_model_path(model_file) my_id = mh.publish_model(model, None) os.remove(model_file) assert (model_id == my_id) rh.done()
def test_convert_models_json_dict_to_dataframe(): rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.AGENT) ion = ION() ion.id = "bdc2ee10-767c-4524-ba72-8268a3894bff" mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) df = mh.convert_models_json_dict_to_dataframe(models_list_json_dict) assert len(df) == 2 rh.done()
def test_model_list_dict_from_json(): with requests_mock.mock() as m: m.get('http://localhost:3456/v1/models', json=models_list_json_dict) rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.AGENT) ion = ION() ion.id = "bdc2ee10-767c-4524-ba72-8268a3894bff" mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) result_model_list = mh.fetch_all_models_json_dict() print("Type is: {}".format(type(result_model_list))) print("result_model_list: {}".format(result_model_list)) json_str_orig = json.dumps(models_list_json_dict, sort_keys=True, indent=2) json_str_got = json.dumps(result_model_list, sort_keys=True, indent=2) assert json_str_orig == json_str_got rh.done()
def test_get_models_with_filter(): with requests_mock.mock() as m: m.get('http://localhost:3456/v1/models', json=models_list_json_dict) rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.AGENT) ion = ION() ion.id = "bdc2ee10-767c-4524-ba72-8268a3894bff" mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) mf = ModelFilter() mf.time_window_start = datetime.utcfromtimestamp(1518460571573 / 1000) mf.time_window_end = datetime.utcfromtimestamp(1518460577573 / 1000) filtered_models = mh.get_models_dataframe(model_filter=mf, download=False) assert len(filtered_models) == 1 print(filtered_models[[models.json_fields.MODEL_NAME_FIELD, models.json_fields.MODEL_CREATED_ON_FIELD]]) rh.done()
def test_get_models_with_filter_2(): with requests_mock.mock() as m: m.get('http://localhost:3456/v1/models', json=models_list_json_dict) rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.AGENT) ion = ION() ion.id = "13445bb4-535a-4d45-b2f2-77293026e3da" mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) model_id_to_filter = '8c95deaf-87e4-4c21-bc92-e5b1a0454f9a' mf = ModelFilter() mf.id = model_id_to_filter filtered_models = mh.get_models_dataframe(model_filter=mf, download=False) print(filtered_models[[models.json_fields.MODEL_ID_FIELD, models.json_fields.MODEL_FORMAT_FIELD]]) assert len(filtered_models) == 1 assert filtered_models.iloc[0][models.json_fields.MODEL_FORMAT_FIELD] == 'TEXT' assert filtered_models.iloc[0][models.json_fields.MODEL_ID_FIELD] == model_id_to_filter rh.done()
def test_publish_model(): expected_models_list_json_dict = [ { models.json_fields.MODEL_ID_FIELD: '', models.json_fields.MODEL_NAME_FIELD: 'my model name', models.json_fields.MODEL_FORMAT_FIELD: 'Text', models.json_fields.MODEL_VERSION_FIELD: '', models.json_fields.MODEL_DESCRIPTION_FIELD: 'test model', models.json_fields.MODEL_TRAIN_VERSION_FIELD: '', models.json_fields.MODEL_SIZE_FIELD: 10, models.json_fields.MODEL_OWNER_FIELD: '', models.json_fields.MODEL_CREATED_ON_FIELD: None, models.json_fields.MODEL_FLAG_VALUES_FIELD: [], models.json_fields.MODEL_ANNOTATIONS_FIELD: {"custom_data": "my content"}, models.json_fields.MODEL_ACTIVE_FIELD: False } ] rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.STAND_ALONE) ion = ION() mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) model_data = "MODEL_DATA" model = mh.create_model(name="my model name", model_format=ModelFormat.TEXT, description="test model") model.set_annotations({"custom_data": "my content"}) model_file = os.path.join(os.path.sep, "tmp", str(uuid.uuid4())) f = open(model_file, 'w') f.write(model_data) f.close() model.set_model_path(model_file) my_id = mh.publish_model(model, None) os.remove(model_file) assert my_id == model.get_id() expected_models_list_json_dict[0][models.json_fields.MODEL_ID_FIELD] = my_id ret_data = mh.download_model(my_id) assert ret_data == model_data result_model_list = mh.fetch_all_models_json_dict() actual_json_dumps = json.dumps(result_model_list, sort_keys=True, indent=2) local_json_dump = json.dumps(expected_models_list_json_dict, sort_keys=True, indent=2) print("Expected_Dumps: {}".format(local_json_dump)) print("Actual_Dumps: {}".format(actual_json_dumps)) assert expected_models_list_json_dict == result_model_list with pytest.raises(MLOpsException): mh.publish_model("Not a model", None) rh.done()
def test_get_models_with_filter_3(): with requests_mock.mock() as m: m.get('http://localhost:3456/models', json=models_list_json_dict) rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.AGENT) ion = ION() ion.id = "bdc2ee10-767c-4524-ba72-8268a3894bff" mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) mf = ModelFilter() mf.time_window_start = datetime.utcfromtimestamp(1518460571573 / 1000) mf.time_window_end = datetime.utcfromtimestamp(1518460577573 / 1000) mf.pipeline_instance_id = ['94bf382b-47d5-4b80-b76c-3bca862e6e23', 'asdf'] filtered_models = mh.get_models_dataframe(model_filter=mf, download=False) assert len(filtered_models) == 1 print(filtered_models[["name", "createdTimestamp", "pipelineInstanceId"]]) # No model found mf.id = "111111111111111" filtered_models = mh.get_models_dataframe(model_filter=mf, download=False) assert len(filtered_models) == 0 rh.done()
def test_publish_model(): rh = MlOpsRestFactory().get_rest_helper(MLOpsMode.STAND_ALONE) ion = ION() ion.id = "bdc2ee10-767c-4524-ba72-8268a3894bff" local_models_list_json_dict[0]["workflowRunId"] = ion.id mh = ModelHelper(rest_helper=rh, ion=ion, stats_helper=None) model_data = "MODEL_DATA" model = mh.create_model(name="my model", model_format=ModelFormat.TEXT, description="test model", user_defined="whatever I want goes here") model_file = os.path.join(os.path.sep, "tmp", str(uuid.uuid4())) f = open(model_file, 'w') f.write(model_data) f.close() model.set_model_path(model_file) my_id = mh.publish_model(model, None) os.remove(model_file) assert my_id == model.get_id() local_models_list_json_dict[0]["modelId"] = my_id ret_data = mh.download_model(my_id) assert ret_data == model_data result_model_list = mh.fetch_all_models_json_dict() actual_json_dumps = json.dumps(result_model_list, sort_keys=True, indent=2) local_json_dump = json.dumps(local_models_list_json_dict, sort_keys=True, indent=2) print("Expected_Dumps: {}".format(local_json_dump)) print("Actual_Dumps: {}".format(actual_json_dumps)) assert local_models_list_json_dict == result_model_list with pytest.raises(MLOpsException): mh.publish_model("Not a model", None) rh.done()
def build_from_dict(self, json_dict): self._debug("Building {} from dict".format(Constants.ION_LITERAL)) self._debug(mask_passwords(json_dict)) ion = ION() ion.id = json_dict['id'] ion.name = json_dict[IONJsonConstants.ION_INFO_SECTION][ IONJsonConstants.ION_NAME_TAG] self._info("{} id [{}]".format(Constants.ION_LITERAL, json_dict['id'])) ion_comps = json_dict[IONJsonConstants.ION_INFO_SECTION][ IONJsonConstants.ION_COMP_SECTION] if len(ion_comps) < 1: msg = "{} components sections does not contain any component".format( Constants.ION_LITERAL) self._error(msg) raise MLOpsException(msg) for comp_dict in ion_comps: agent_set = {} wf_id = comp_dict["id"] pipeline_ee_tuple = comp_dict["pipelineEETuple"] ee = pipeline_ee_tuple["executionEnvironment"] agent_set[ee['agentId']] = pipeline_ee_tuple['pipelineProfileId'] comp = MLAppNode( name=str(wf_id), id=wf_id, pipeline_pattern_id=str( comp_dict[IONJsonConstants.PIPELINE_PATTERN_ID_TAG]), pipeline_agent_set=agent_set, ee_id=str(ee["id"])) ion.nodes.append(comp) ion.node_by_id[comp.id] = comp ion.node_by_name[comp.name] = comp pipeline_instances = json_dict[ IONJsonConstants.PIPELINE_INSTANCES_SECTION] self._debug("pipelines number: {}".format(len(pipeline_instances))) for instance_data in pipeline_instances: comp_id = instance_data[ IONJsonConstants.PIPELINE_WORKFLOW_NODE_ID_TAG] pipeline_instance_id = instance_data[ IONJsonConstants.PIPELINE_INSTANCE_ID_TAG] if comp_id not in ion.node_by_id: raise MLOpsException( "Error: could not find {} node id {} in pipeline " "instances".format(Constants.ION_LITERAL, comp_id)) ion.node_by_id[comp_id].pipeline_instance_id = pipeline_instance_id pipeline = Pipeline( pipeline_name=instance_data["pipelineName"], pipeline_id=instance_data["pipelineId"], pipeline_type=instance_data["pipelineType"].upper()) ion.pipelines.append(pipeline) # TODO maybe need to append? ion.pipeline_by_name[pipeline.pipeline_name] = pipeline ion.pipeline_by_id[pipeline.pipeline_id] = pipeline pipeline_id = instance_data['pipelineId'] if pipeline_id not in ion.pipeline_by_id: raise MLOpsException( "Error: could not find pipeline by id {}".format( pipeline_id)) pipeline = ion.pipeline_by_id[pipeline_id] ion.pipelineInstance_to_pipeline[pipeline_instance_id] = pipeline if pipeline.pipeline_name not in ion.pipeline_to_pipelineInstances: ion.pipeline_to_pipelineInstances[pipeline.pipeline_name] = [] ion.pipeline_to_pipelineInstances[pipeline.pipeline_name].append( (comp_id, pipeline_instance_id)) ion.policy = Policy(health_threshold=None, canary_threshold=None) ion._finalize() return ion
def _set_stand_alone_values(self): self._ion = ION() self._ion.id = 1 self._ion.name = "ION_1"