class TestPMFRecommendation(TestCase): """Test the core recommendations task.""" def setUp(self): """Instantiate the resources required for the tests.""" self.fs = LocalFileSystem(src_dir='tests/test_data') self.assertTrue(self.fs.get_name().endswith('tests/test_data')) self.pmf_rec = PMFRecommendation(2, data_store=self.fs, num_latent=5) def test__find_closest_user_in_training_set(self): """Test if we are getting correct "closest user" from the training set.""" # Full match closest = self.pmf_rec._find_closest_user_in_training_set( [17190, 14774, 15406, 16594, 29063]) self.assertIsNotNone(closest) # Partial closest = self.pmf_rec._find_closest_user_in_training_set( [17190, 14774, 15406]) self.assertIsNotNone(closest) # Negative closest = self.pmf_rec._find_closest_user_in_training_set([3, 4]) self.assertIsNone(closest) def test__sigmoid(self): """Test if the sigmoid function is behaving correctly.""" self.assertEqual(self.pmf_rec._sigmoid(0), 0.5) def test_predict(self): """Test the prediction flow.""" # Test for a new stack. missing, recommendation, ptm = self.pmf_rec.predict(['pon-logger']) self.assertFalse(missing) # Should have two recommendations here. self.assertEqual(len(recommendation), 2) # Tests for missing package. missing, recommendation, _ = self.pmf_rec.predict( ['pon-logger', 'missing']) self.assertTrue(missing) # Test if still getting recommendation as no. of missing = no. of known self.assertGreater(len(recommendation), 0) missing, _, package_tag_map = self.pmf_rec.predict(['missing']) self.assertDictEqual(package_tag_map, {}) # Test for precomputed stack. _, recommendation, _ = self.pmf_rec.predict( ['async', 'colors', 'request', 'underscore', 'pkginfo']) self.assertTrue(recommendation)
class TestPMFRecommendation(TestCase): """Test the core recommendations task.""" def setUp(self): """Instantiate the resources required for the tests.""" self.fs = LocalDataStore('tests/test_data') self.assertTrue(self.fs.get_name().endswith('tests/test_data')) self.pmf_rec = PMFRecommendation(2, data_store=self.fs, num_latent=50) def test__find_closest_user_in_training_set(self): """Test if we are getting correct "closest user" from the training set.""" # Full match closest = self.pmf_rec._find_closest_user_in_training_set( [21, 107, 89, 14]) self.assertIsNotNone(closest) # Negative closest = self.pmf_rec._find_closest_user_in_training_set( [17190, 14774, 15406]) self.assertIsNone(closest) # Partial closest = self.pmf_rec._find_closest_user_in_training_set([3, 4]) self.assertIsNotNone(closest) def test__sigmoid(self): """Test if the sigmoid function is behaving correctly.""" self.assertEqual(self.pmf_rec._sigmoid(0), 0.5) def test_predict(self): """Test the prediction flow.""" # Test for a new stack. missing, recommendation, ptm = self.pmf_rec.predict(["nopt"]) self.assertFalse(missing) # Should have two recommendations here. self.assertEqual(len(recommendation), 2) # Tests for missing package. missing, recommendation, _ = self.pmf_rec.predict(['iferr', 'missing']) self.assertTrue(missing) # Test if still getting recommendation as no. of missing = no. of known self.assertGreater(len(recommendation), 0) missing, _, package_tag_map = self.pmf_rec.predict(['missing']) self.assertDictEqual(package_tag_map, {}) # Test for precomputed stack. _, recommendation, _ = self.pmf_rec.predict( ["statuses", "debuglog", "unpipe"]) self.assertTrue(recommendation)
class TestPMFRecommendation(TestCase): """Test the core recommendations task.""" def setUp(self): """Instantiate the resources required for the tests.""" self.fs = LocalFileSystem(src_dir='tests/test_data') self.pmf_rec = PMFRecommendation(2, data_store=self.fs, num_latent=5) def test__find_closest_user_in_training_set(self): """Test if we are getting correct "closest user" from the training set.""" closest = self.pmf_rec._find_closest_user_in_training_set( [17190, 14774, 15406, 16594, 29063]) # Check that closest is not empty self.assertIsNotNone(closest) # Negative closest = self.pmf_rec._find_closest_user_in_training_set([3, 4]) # should be empty self.assertIsNone(closest) def test__sigmoid(self): """Test if the sigmoid function is behaving correctly.""" self.assertEqual(self.pmf_rec._sigmoid(0), 0.5) def test_predict(self): """Test the prediction flow.""" # Test for a new stack. missing, recommendation, ptm = self.pmf_rec.predict(['pon-logger']) self.assertFalse(missing) # Should have two recommendations here. self.assertEqual(len(recommendation), 2) # Tests for missing package. missing, _, _ = self.pmf_rec.predict(['pon-logger', 'missing']) self.assertTrue(missing) missing, _, package_tag_map = self.pmf_rec.predict(['missing']) self.assertDictEqual(package_tag_map, {})
print("INSIDE LOCAL ACCESS") s3 = AmazonS3(bucket_name=cloud_constants.S3_BUCKET_NAME, aws_access_key_id=cloud_constants.AWS_S3_ACCESS_KEY_ID, aws_secret_access_key=cloud_constants.AWS_S3_SECRET_KEY_ID, endpoint_url=cloud_constants.AWS_S3_ENDPOINT_URL, local_dev=True) s3.connect() else: from rudra.data_store.local_data_store import LocalDataStore # Change the source directory here for local file system testing. s3 = LocalDataStore('tests/test_data/') ScoringParams.num_latent_factors = 5 # This needs to be global as ~200MB of data is loaded from S3 every time an object of this class # is instantiated. recommender = PMFRecommendation(ScoringParams.recommendation_threshold, s3, ScoringParams.num_latent_factors) @app.get('/api/v1/liveness', status_code=200) def liveness(): """Define the linveness probe.""" return {} @app.get('/api/v1/readiness', status_code=200) def readiness(): """Define the readiness probe.""" return {} @app.post('/api/v1/companion_recommendation', status_code=200)
import flask from flask import Flask, request from recommendation_engine.predictor.online_recommendation import PMFRecommendation from recommendation_engine.data_store.s3_data_store import S3DataStore import recommendation_engine.config.cloud_constants as cloud_constants from recommendation_engine.config.params_scoring import ScoringParams app = Flask(__name__) s3 = S3DataStore(src_bucket_name=cloud_constants.S3_BUCKET_NAME, access_key=cloud_constants.AWS_S3_ACCESS_KEY_ID, secret_key=cloud_constants.AWS_S3_SECRET_KEY_ID) # This needs to be global as ~200MB of data is loaded from S3 every time an object of this class # is instantiated. recommender = PMFRecommendation(ScoringParams.recommendation_threshold, s3) @app.route('/api/v1/liveness', methods=['GET']) def liveness(): """Define the linveness probe.""" return flask.jsonify({}), 200 @app.route('/api/v1/readiness', methods=['GET']) def readiness(): """Define the readiness probe.""" return flask.jsonify({"status": "ready"}), 200 @app.route('/api/v1/companion_recommendation', methods=['POST'])
def setUp(self): """Instantiate the resources required for the tests.""" self.fs = LocalFileSystem(src_dir='tests/test_data') self.assertTrue(self.fs.get_name().endswith('tests/test_data')) self.pmf_rec = PMFRecommendation(2, data_store=self.fs, num_latent=5)