def predict(self, feature_id, feature_names): logging.info('feature_id = %s', feature_id) logging.info('feature_names = %s', feature_names) if any([i is None for i in [self.m, self.b, self.fs, self.serving_fs]]): with open('simple_model.dat', 'r') as f: model = json.load(f) self.m = np.array(model.get('m', [])) self.b = float(model.get('b', 0)) _FEAST_CORE_URL = model['FEAST_CORE_URL'] _FEAST_SERVING_URL = model['FEAST_SERVING_URL'] _ENTITY_ID = model['ENTITY_ID'] logging.info('FEAST_CORE_URL: %s', _FEAST_CORE_URL) logging.info('FEAST_SERVING_URL: %s', _FEAST_SERVING_URL) logging.info('ENTITY_ID: %s', _ENTITY_ID) logging.info('FEATURES_SET: %s', self.SERVING_FEATURE_SET) self.fs = Client(core_url=_FEAST_CORE_URL, serving_url=_FEAST_SERVING_URL, verbose=True) self.serving_fs = FeatureSet(entity=_ENTITY_ID, features=self.SERVING_FEATURE_SET) features = self.fs.get_serving_data(self.serving_fs, entity_keys=[feature_id]) X = features.to_numpy()[0][1:] logging.info('X: %s', str(X)) return [sum(self.m * X) + self.b]
class LabelPrediction(object): def __init__(self): self.m = None self.b = None self.fs = None self.serving_fs = None logging.basicConfig( level=logging.INFO, format=('%(levelname)s|%(asctime)s' '|%(pathname)s|%(lineno)d| %(message)s'), datefmt='%Y-%m-%dT%H:%M:%S', ) logging.getLogger().setLevel(logging.INFO) def predict(self, feature_id, feature_names): logging.info('feature_id = %s', feature_id) logging.info('feature_names = %s', feature_names) if any([i is None for i in [self.m, self.b, self.fs, self.serving_fs]]): with open('simple_model.dat', 'r') as f: model = json.load(f) self.m = np.array(model.get('m', [])) self.b = float(model.get('b', 0)) _FEAST_CORE_URL = model['FEAST_CORE_URL'] _FEAST_SERVING_URL = model['FEAST_SERVING_URL'] _ENTITY_ID = model['ENTITY_ID'] _FEATURES_SET = model['FEATURES_SET'] logging.info('FEAST_CORE_URL: %s', _FEAST_CORE_URL) logging.info('FEAST_SERVING_URL: %s', _FEAST_SERVING_URL) logging.info('ENTITY_ID: %s', _ENTITY_ID) logging.info('FEATURES_SET: %s', _FEATURES_SET) self.fs = Client(core_url=_FEAST_CORE_URL, serving_url=_FEAST_SERVING_URL, verbose=True) self.serving_fs = FeatureSet(entity=_ENTITY_ID, features=_FEATURES_SET) features = self.fs.get_serving_data(self.serving_fs, entity_keys=[feature_id]) X = features.to_numpy()[0][1:] logging.info('X: %s', str(X)) return sum(self.m * X) + self.b
def main(): parser = argparse.ArgumentParser() parser.add_argument("--entity_spec_file", required=True) parser.add_argument("--feature_spec_files", required=True) parser.add_argument("--expected-warehouse-values-file") parser.add_argument("--expected-serving-values-file") parser.add_argument("--bigquery-dataset-for-warehouse") parser.add_argument("--feast-serving-url") parser.add_argument("--project") args = parser.parse_args() feast_client = Client(serving_url=args.feast_serving_url) entity_name = get_entity_name(args.entity_spec_file) feature_infos = get_feature_infos(args.feature_spec_files) if args.expected_warehouse_values_file: if not args.bigquery_dataset_for_warehouse: raise RuntimeError("Missing option --bigquery-dataset-for-warehouse") validate_warehouse( entity_name, feature_infos, args.expected_warehouse_values_file, args.bigquery_dataset_for_warehouse, args.project, ) else: print("\n[Skipping validation of Feast Warehouse data]") print("because --expected-warehouse-values-file is not set") if args.expected_serving_values_file: validate_serving( entity_name, feature_infos, args.expected_serving_values_file, feast_client ) else: print("\n[Skipping validation of Feast Serving data]") print("because --expected-serving-values-file is not set")
class HousingModel(object): """Model class.""" SERVING_FEATURE_SET = [ 'usa_housing.avg_area_income', 'usa_housing.avg_area_house_age', 'usa_housing.avg_area_number_of_rooms', 'usa_housing.avg_area_number_of_bedrooms', 'usa_housing.area_population' ] def __init__(self): self.m = None self.b = None self.fs = None self.serving_fs = None logging.basicConfig( level=logging.INFO, format=('%(levelname)s|%(asctime)s' '|%(pathname)s|%(lineno)d| %(message)s'), datefmt='%Y-%m-%dT%H:%M:%S', ) logging.getLogger().setLevel(logging.INFO) # Train model def train(self, training_df): np.set_printoptions(precision=3) train_data = training_df[[ x.split('.')[1] for x in TRAINING_FEATURES_SET ]].to_numpy() train_data[:, len(train_data[0]) - 1] = 1 Y = training_df['price'].to_numpy() x = np.linalg.lstsq(train_data, Y, rcond=0)[0] m, b = x[:len(train_data[0]) - 1], x[len(train_data[0]) - 1] self.m = m self.b = b return m, b def predict(self, feature_id, feature_names): logging.info('feature_id = %s', feature_id) logging.info('feature_names = %s', feature_names) if any([i is None for i in [self.m, self.b, self.fs, self.serving_fs]]): with open('simple_model.dat', 'r') as f: model = json.load(f) self.m = np.array(model.get('m', [])) self.b = float(model.get('b', 0)) _FEAST_CORE_URL = model['FEAST_CORE_URL'] _FEAST_SERVING_URL = model['FEAST_SERVING_URL'] _ENTITY_ID = model['ENTITY_ID'] logging.info('FEAST_CORE_URL: %s', _FEAST_CORE_URL) logging.info('FEAST_SERVING_URL: %s', _FEAST_SERVING_URL) logging.info('ENTITY_ID: %s', _ENTITY_ID) logging.info('FEATURES_SET: %s', self.SERVING_FEATURE_SET) self.fs = Client(core_url=_FEAST_CORE_URL, serving_url=_FEAST_SERVING_URL, verbose=True) self.serving_fs = FeatureSet(entity=_ENTITY_ID, features=self.SERVING_FEATURE_SET) features = self.fs.get_serving_data(self.serving_fs, entity_keys=[feature_id]) X = features.to_numpy()[0][1:] logging.info('X: %s', str(X)) return [sum(self.m * X) + self.b] def save_model(self, model_path): """Save the model to a json file.""" MODEL_FILE = 'simple_model.dat' model = { 'm': self.m.tolist(), 'b': self.b, 'FEAST_CORE_URL': FEAST_CORE_URL, 'FEAST_SERVING_URL': FEAST_SERVING_URL, 'ENTITY_ID': ENTITY_ID, } logging.info('Saving model to %s', model_path) with open(model_path, 'w+') as f: json.dump(model, f)
def client(self, mocker): cli = Client(core_url="some.uri", serving_url="some.serving.uri") mocker.patch.object(cli, '_connect_core') mocker.patch.object(cli, '_connect_serving') return cli
def client(): return Client(verbose=True)
import test_feast from feast.sdk.client import Client # This main.py is for easier local debugging # because running pytest often hides useful debugging output if __name__ == "__main__": feast_client = Client(verbose=True) test_feast.TestFeastIntegration().test_end_to_end(client=feast_client)
def fs(self): if not self._fs: self._fs = Client(core_url=self.FEAST_CORE_URL,serving_url=self.FEAST_SERVING_URL, verbose=True) return self._fs