def __init__(self, model_path): self._gateway = create_gateway() self._pipeline = ExportedPipeline() self._pipeline.load(model_path) self._features_spec = """ { "event": [{ "group": "medication", "operation": "count", "window": 365 }, { "group": "diagnostic", "operation": "count", "window": 365 }, { "group": "medication", "operation": "avrgdu", "window": 365 }, { "group": "encounter", "operation": "avrgdu", "window": 365 }], "attribute": [{ "attribute": "DOB", "operation": "default" },{ "attribute": "gender" }] } """ self._feature_mapping = ssf.load_feature_mapping(model_path + "mapping.txt") self._feature_names = [k for k, __ in sorted(self._feature_mapping.items(), key=lambda x: x[1])] self._metrics = load_json(model_path + "metric") self._top_features = None for step in self._pipeline._steps: if hasattr(step._model, "scores_"): scores = step._model.scores_ top_features_index = np.argsort(scores)[::-1][:200] self._top_features = [self._feature_names[i] for i in top_features_index] break
class SpaceshipAdapter(object): def __init__(self, model_path): self._gateway = create_gateway() self._pipeline = ExportedPipeline() self._pipeline.load(model_path) self._features_spec = """ { "event": [{ "group": "medication", "operation": "count", "window": 365 }, { "group": "diagnostic", "operation": "count", "window": 365 }, { "group": "medication", "operation": "avrgdu", "window": 365 }, { "group": "encounter", "operation": "avrgdu", "window": 365 }], "attribute": [{ "attribute": "DOB", "operation": "default" },{ "attribute": "gender" }] } """ self._feature_mapping = ssf.load_feature_mapping(model_path + "mapping.txt") self._feature_names = [k for k, __ in sorted(self._feature_mapping.items(), key=lambda x: x[1])] self._metrics = load_json(model_path + "metric") self._top_features = None for step in self._pipeline._steps: if hasattr(step._model, "scores_"): scores = step._model.scores_ top_features_index = np.argsort(scores)[::-1][:200] self._top_features = [self._feature_names[i] for i in top_features_index] break def score(self, patient): if isinstance(patient, dict): import json patient = json.dumps(patient) # feature construction named_features = ssf.extract_named_features(self._gateway.jvm, patient, self._features_spec) feature_vector = ssf.to_sparse_feature_vector(self._feature_mapping, named_features) return self._pipeline.calibrated_score(feature_vector) def model_statistics(self): return self._metrics def top_features(self, patient): if isinstance(patient, dict): import json patient = json.dumps(patient) named_features = ssf.extract_named_features(self._gateway.jvm, patient, self._features_spec) return [k for k, _ in named_features.items() if k in self._top_features]