def test_u2iAction(self): client = predictionio.Client(APP_KEY, 1, API_URL) client.identify("u101") # required param client.record_action_on_item("like", "i1") client.record_action_on_item("dislike", "i2") client.record_action_on_item("view", "i3") client.record_action_on_item("rate", "i4", {"pio_rate": 1}) client.record_action_on_item("conversion", "i5") client.identify("u102") # with optional param client.record_action_on_item("like", "i1", {"pio_latlng": [1.23, 4.56]}) client.record_action_on_item("dislike", "i2", {"pio_t": 1234567689}) client.record_action_on_item("view", "i3", { "pio_latlng": [4.67, 1.44], "pio_t": 3445566778 }) client.record_action_on_item("rate", "i4", { "pio_rate": 1, "pio_latlng": [66.78, 9.10] }) client.record_action_on_item("conversion", "i5", {"pio_price": 12.5}) # uid and iid with special characters client.identify("*****@*****.**") client.record_action_on_item("view", "*****@*****.**") client.record_action_on_item("view", "http://www.yahoo.com") client.close()
def test_get_itemrec_exception(self): client = predictionio.Client(APP_KEY, 1, API_URL) client.identify("uidwithoutrec") try: itemrec = client.get_itemrec_topn("python-itemrec-engine", 10) except predictionio.ItemRecNotFoundError as e: pass # expected exception except: raise try: itemrec = client.get_itemrec_topn( "python-itemrec-engine", 10, { "pio_itypes": ("t1", ), "pio_latlng": [1.34, 5.67], "pio_within": 5.0, "pio_unit": "km", "pio_attributes": ["custom1", "custom2"] }) except predictionio.ItemRecNotFoundError as e: pass # expected exception except: raise client.close()
def test_u2iAction_deprecated(self): client = predictionio.Client(APP_KEY, 1, API_URL) client.user_like_item("u1", "i1") client.user_dislike_item("u2", "i2") client.user_view_item("u3", "i3") client.user_rate_item("u4", "i4", 4) client.user_conversion_item("u5", "i5") client.close()
def test_get_itemrec_exception_deprecated(self): client = predictionio.Client(APP_KEY, 1, API_URL) try: itemrec = client.get_itemrec("uidwithoutrec", 10, "python-itemrec-engine") except predictionio.ItemRecNotFoundError as e: pass # expected exception except: raise client.close()
def get_sim(brandcat, query): client = predictionio.Client("c9E6ehRTIIRUs3ZLgqXjsXWyr6CSGKGiHRoWINzk2POfzcjWSyeoglEeQVmWYfkK",apiurl ='http://sugarqapio02.sugarops.com:8000') # stable uid-brandcat brandcat_sim = [] try: if query['cat'] == '109': rec = client.get_itemsim_topn("women-shoes",brandcat , 5 , {"pio_itypes":("cat109",)}) elif query['cat'] == '219': rec = client.get_itemsim_topn("men-shoes",brandcat , 5 , {"pio_itypes":("cat219",)}) except: rec = [] if rec: brandcat_sim = rec['pio_iids'] return brandcat_sim
def test_qsize(self): client = predictionio.Client(APP_KEY, 1, API_URL, qsize=10) client.identify("u222") for i in range(100): client.arecord_action_on_item("like", str(i)) n = 1 while n > 0: n = client.pending_requests() time.sleep(0.1) print n client.close()
def test_get_itemrec_deprecated(self): client = predictionio.Client(APP_KEY, 1, API_URL) # request more try: itemrec = client.get_itemrec("u0", 10, "python-itemrec-engine") except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": ["i2", "i3", "i1", "i0"]}) try: itemrec = client.get_itemrec("u1", 10, "python-itemrec-engine") except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertTrue( (itemrec == {"pio_iids": ["i2", "i1", "i0", "i3"]}) or (itemrec == {"pio_iids": ["i2", "i0", "i1", "i3"]}) ) try: itemrec = client.get_itemrec("u2", 10, "python-itemrec-engine") except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertTrue( (itemrec == {"pio_iids": ["i3", "i0", "i1", "i2"]}) or (itemrec == {"pio_iids": ["i3", "i1", "i0", "i2"]}) ) try: itemrec = client.get_itemrec("u3", 6, "python-itemrec-engine") except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertTrue( (itemrec == {"pio_iids": ["i0", "i1", "i2", "i3"]}) or (itemrec == {"pio_iids": ["i0", "i2", "i1", "i3"]}) ) # request less try: itemrec = client.get_itemrec("u0", 1, "python-itemrec-engine") except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": ["i2"]}) try: itemrec = client.get_itemrec("u0", 2, "python-itemrec-engine") except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": ["i2", "i3"]}) # request with optional attributes # pio_itypes try: itemrec = client.get_itemrec("u0", 10, "python-itemrec-engine", pio_itypes=("t1","t2")) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": ["i2", "i3", "i1", "i0"]}) # subset itypes try: itemrec = client.get_itemrec("u0", 10, "python-itemrec-engine", pio_itypes=("t2",)) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": ["i2", "i1"]}) # nonexisting itypes try: itemrec = client.get_itemrec("u0", 10, "python-itemrec-engine", pio_itypes=("other-itype",)) except predictionio.ItemRecNotFoundError as e: pass # expected no recommendation except: raise # pio_attributes try: itemrec = client.get_itemrec("u0", 10, "python-itemrec-engine", pio_itypes=("t1",), pio_attributes=["custom1", "custom2"]) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": ["i2", "i3", "i1", "i0"], "custom1": [None, None, "i1c1", "i0c1"], "custom2": ["i2c2", None, "i1c2", None]}) client.close()
""" Import simple test data for testing getting itemrec """ import predictionio APP_KEY = "7zwXYnroz52gemLdHEU20Nn8c2SyobFpnrzoTGOolCe8ZRH2zmGyhVknj9Sa7P6x" API_URL = "http://localhost:8000" MIN_VERSION = '0.5.0' if predictionio.__version__ < MIN_VERSION: err = "Require PredictionIO Python SDK version >= %s" % MIN_VERSION raise Exception(err) if __name__ == "__main__": client = predictionio.Client(APP_KEY, 1, API_URL) client.create_user("u0") client.create_user("u1") client.create_user("u2") client.create_user("u3") client.create_item("i0", ("t1", ), {"custom1": "i0c1"}) client.create_item("i1", ("t1", "t2"), { "custom1": "i1c1", "custom2": "i1c2" }) client.create_item("i2", ("t1", "t2"), {"custom2": "i2c2"}) client.create_item("i3", ("t1", )) client.identify("u0x") client.record_action_on_item("rate", "i0", {"pio_rate": 2})
def __init__(self): self._app_data = AppData() self._client = predictionio.Client(APP_KEY, 1, API_URL)
def test_get_itemrec_deprecated(self): client = predictionio.Client(APP_KEY, 1, API_URL) uid0 = "[email protected]" uid1 = "[email protected]" uid2 = "http://u2.com" uid3 = "[email protected]" iid0 = "http://i0.com" iid1 = "i1@i1" iid2 = "i2.org" iid3 = "i3" engine_name = "itemrec" # request more try: itemrec = client.get_itemrec(uid0, 10, engine_name) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": [iid2, iid3, iid1, iid0]}) try: itemrec = client.get_itemrec(uid1, 10, engine_name) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertTrue((itemrec == { "pio_iids": [iid2, iid1, iid0, iid3] }) or (itemrec == { "pio_iids": [iid2, iid0, iid1, iid3] })) try: itemrec = client.get_itemrec(uid2, 10, engine_name) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertTrue((itemrec == { "pio_iids": [iid3, iid0, iid1, iid2] }) or (itemrec == { "pio_iids": [iid3, iid1, iid0, iid2] })) try: itemrec = client.get_itemrec(uid3, 6, engine_name) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertTrue((itemrec == { "pio_iids": [iid0, iid1, iid2, iid3] }) or (itemrec == { "pio_iids": [iid0, iid2, iid1, iid3] })) # request less try: itemrec = client.get_itemrec(uid0, 1, engine_name) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": [iid2]}) try: itemrec = client.get_itemrec(uid0, 2, engine_name) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": [iid2, iid3]}) # request with optional attributes # pio_itypes try: itemrec = client.get_itemrec(uid0, 10, engine_name, pio_itypes=("t1", "t2")) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": [iid2, iid3, iid1, iid0]}) # subset itypes try: itemrec = client.get_itemrec(uid0, 10, engine_name, pio_itypes=("t2", )) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual(itemrec, {"pio_iids": [iid2, iid1]}) # nonexisting itypes try: itemrec = client.get_itemrec(uid0, 10, engine_name, pio_itypes=("other-itype", )) except predictionio.ItemRecNotFoundError as e: pass # expected no recommendation except: raise # pio_attributes try: itemrec = client.get_itemrec(uid0, 10, engine_name, pio_itypes=("t1", ), pio_attributes=["custom1", "custom2"]) except predictionio.ItemRecNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemrec self.assertEqual( itemrec, { "pio_iids": [iid2, iid3, iid1, iid0], "custom1": [None, None, "i1c1", "i0c1"], "custom2": ["i2c2", None, "i1c2", None] }) client.close()
def _test_item(self, iids): client = predictionio.Client(APP_KEY, 1, API_URL) iid1 = iids[0] iid2 = iids[1] iid3 = iids[2] iid4 = iids[3] iid5 = iids[4] # create items and read back client.create_item(iid1, ("t1", "t2", "t3")) client.create_item(iid2, ("t1", )) client.create_item( iid3, ("t2", ), { "pio_price": 4.99, "pio_profit": 2.0, "pio_startT": 12345667, "pio_endT": 4567788, "pio_latlng": [1.345, 9.876], "pio_inactive": True }) client.create_item(iid4, ("t2", ), { "pio_latlng": [1.2345, 10.11], "custom1": "value1" }) client.create_item(iid5, ("t1", "t2"), { "custom1": "i5value1", "custom2": "i5value2" }) item1 = client.get_item(iid1) item2 = client.get_item(iid2) item3 = client.get_item(iid3) item4 = client.get_item(iid4) item5 = client.get_item(iid5) del item1[ "pio_startT"] # pio_startT is automatically inserted, don't compare self.assertEqual(item1, { "pio_iid": iid1, "pio_itypes": ("t1", "t2", "t3") }) del item2["pio_startT"] self.assertEqual(item2, {"pio_iid": iid2, "pio_itypes": ("t1", )}) self.assertEqual( item3, { "pio_iid": iid3, "pio_itypes": ("t2", ), "pio_price": 4.99, "pio_profit": 2.0, "pio_startT": 12345667, "pio_endT": 4567788, "pio_latlng": [1.345, 9.876], "pio_inactive": True }) del item4["pio_startT"] self.assertEqual( item4, { "pio_iid": iid4, "pio_itypes": ("t2", ), "pio_latlng": [1.2345, 10.11], "custom1": "value1" }) del item5["pio_startT"] self.assertEqual( item5, { "pio_iid": iid5, "pio_itypes": ("t1", "t2"), "custom1": "i5value1", "custom2": "i5value2" }) # delete and then try to get it client.delete_item(iid2) try: item2 = client.get_item(iid2) except predictionio.ItemNotFoundError as e: pass # expected exception except: raise # others still exist item3 = client.get_item(iid3) self.assertEqual( item3, { "pio_iid": iid3, "pio_itypes": ("t2", ), "pio_price": 4.99, "pio_profit": 2.0, "pio_startT": 12345667, "pio_endT": 4567788, "pio_latlng": [1.345, 9.876], "pio_inactive": True }) # read, modify, write del item3["pio_iid"] item3_itypes = item3.pop("pio_itypes") item3["pio_price"] = 6.99 item3["custom1"] = "some value" client.create_item(iid3, item3_itypes, item3) updated_item3 = client.get_item(iid3) self.assertEqual( updated_item3, { "pio_iid": iid3, "pio_itypes": ("t2", ), "pio_price": 6.99, "pio_profit": 2.0, "pio_startT": 12345667, "pio_endT": 4567788, "pio_latlng": [1.345, 9.876], "pio_inactive": True, "custom1": "some value" }) client.close()
def test_get_itemsim(self): client = predictionio.Client(APP_KEY, 1, API_URL) # request more than what is available try: itemsim = client.get_itemsim_topn("python-itemsim-engine", "i0", 10) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertTrue((itemsim == { "pio_iids": ["i1", "i2", "i3"] }) or (itemsim == { "pio_iids": ["i1", "i3", "i2"] })) try: itemsim = client.get_itemsim_topn("python-itemsim-engine", "i1", 10) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertTrue((itemsim == {"pio_iids": ["i2", "i3", "i0"]})) try: itemsim = client.get_itemsim_topn("python-itemsim-engine", "i2", 10) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertTrue((itemsim == {"pio_iids": ["i1", "i3", "i0"]})) try: itemsim = client.get_itemsim_topn("python-itemsim-engine", "i3", 10) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertTrue((itemsim == {"pio_iids": ["i1", "i2", "i0"]})) # request less try: itemsim = client.get_itemsim_topn("python-itemsim-engine", "i1", 1) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertEqual(itemsim, {"pio_iids": ["i2"]}) try: itemsim = client.get_itemsim_topn("python-itemsim-engine", "i1", 2) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertEqual(itemsim, {"pio_iids": ["i2", "i3"]}) # request with optional attributes # pio_itypes try: itemsim = client.get_itemsim_topn("python-itemsim-engine", "i1", 10, {"pio_itypes": ("t1", "t2")}) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertEqual(itemsim, {"pio_iids": ["i2", "i3", "i0"]}) # subset itypes try: itemsim = client.get_itemsim_topn("python-itemsim-engine", "i1", 10, {"pio_itypes": ("t2", )}) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertEqual(itemsim, {"pio_iids": ["i2"]}) # nonexisting itypes try: itemsim = client.get_itemsim_topn( "python-itemsim-engine", "i0", 10, {"pio_itypes": ("other-itype", )}) except predictionio.ItemSimNotFoundError as e: pass # expected no recommendation except: raise # pio_attributes try: itemsim = client.get_itemsim_topn( "python-itemsim-engine", "i1", 10, { "pio_itypes": ("t1", ), "pio_attributes": ["custom1", "custom2"] }) except predictionio.ItemSimNotFoundError as e: print "ERROR: have you run import_testdata.py and then wait for the algorithm training completion?" raise except: raise if DEBUG: print itemsim self.assertEqual( itemsim, { "pio_iids": ["i2", "i3", "i0"], "custom1": [None, None, "i0c1"], "custom2": ["i2c2", None, None] }) # TODO pio_latlng # TODO pio_within # TODO pio_unit client.close()
def _test_user(self, uids): client = predictionio.Client(APP_KEY, 1, API_URL) uid1 = uids[0] uid2 = uids[1] uid3 = uids[2] uid4 = uids[3] uid5 = uids[4] # create users and get them back client.create_user(uid1) # create user with optional attributes client.create_user(uid2, {"pio_latlng": [1.2, 33.3]}) client.create_user(uid3, { "pio_latlng": [4.5, 67.8], "pio_inactive": True }) # create user with custom attributes client.create_user(uid4, { "pio_latlng": [1.2, 33.3], "custom1": "value1", "custom2": "value2" }) client.create_user(uid5, {"custom1": "u5c1", "custom2": "u5c2"}) user1 = client.get_user(uid1) user2 = client.get_user(uid2) user3 = client.get_user(uid3) user4 = client.get_user(uid4) user5 = client.get_user(uid5) self.assertEqual(user1, {"pio_uid": uid1}) self.assertEqual(user2, {"pio_uid": uid2, "pio_latlng": [1.2, 33.3]}) self.assertEqual(user3, { "pio_uid": uid3, "pio_latlng": [4.5, 67.8], "pio_inactive": True }) self.assertEqual( user4, { "pio_uid": uid4, "pio_latlng": [1.2, 33.3], "custom1": "value1", "custom2": "value2" }) self.assertEqual(user5, { "pio_uid": uid5, "custom1": "u5c1", "custom2": "u5c2" }) # delete user and then try to get it client.delete_user(uid1) try: user = client.get_user(uid1) except predictionio.UserNotFoundError as e: pass # expected exception except: raise # other users still exist user2 = client.get_user(uid2) self.assertEqual(user2, {"pio_uid": uid2, "pio_latlng": [1.2, 33.3]}) # read, modify, write user3 = client.get_user(uid3) self.assertEqual(user3, { "pio_uid": uid3, "pio_latlng": [4.5, 67.8], "pio_inactive": True }) del user3["pio_uid"] user3["pio_latlng"] = [5.6, 10.11] user3["pio_inactive"] = False user3["custom1"] = "food" client.create_user(uid3, user3) updated_user3 = client.get_user(uid3) self.assertEqual( updated_user3, { "pio_uid": uid3, "pio_latlng": [5.6, 10.11], "pio_inactive": False, "custom1": "food" }) user4 = client.get_user(uid4) self.assertEqual( user4, { "pio_uid": uid4, "pio_latlng": [1.2, 33.3], "custom1": "value1", "custom2": "value2" }) del user4["pio_uid"] user4["custom1"] = "new value" client.create_user(uid4, user4) updated_user4 = client.get_user(uid4) self.assertEqual( updated_user4, { "pio_uid": uid4, "pio_latlng": [1.2, 33.3], "custom1": "new value", "custom2": "value2" }) client.close()
def test_status(self): client = predictionio.Client(APP_KEY, 1, API_URL) status = client.get_status() self.assertEqual(status, "PredictionIO Output API is online.") client.close()
print "[Info] Importing rate actions to PredictionIO..." count = 0 for v in app_data.get_rate_actions(): count += 1 if all_info: print "[Info] Importing %s..." % v else: if (count % 32 == 0): sys.stdout.write('\r[Info] %s' % count) sys.stdout.flush() client.identify(v.uid) client.arecord_action_on_item("rate", v.iid, { "pio_rate": v.rating, "pio_t": v.t }) sys.stdout.write('\r[Info] %s rate actions were imported.\n' % count) sys.stdout.flush() if __name__ == '__main__': app_data = AppData() client = predictionio.Client(APP_KEY, THREADS, API_URL, qsize=REQUEST_QSIZE) batch_import_task(app_data, client) client.close()
import predictionio import random ######################################################## # initialize things random.seed() client = predictionio.Client(appkey="hkp4eZXspTjIJUhk7RJpEaO1YWO2V4t8QRHG4nZTjHzIJpNvX4K4r5AwanRnoAKv") ######################################################## # generate 10 users with user ids 1,2,...,10 user_ids = [str(i) for i in range(1, 11)] for user_id in user_ids: print "Add user " + user_id client.create_user(user_id) ######################################################## # generate 50 items with item ids 1,2,...,50 # assign type id 1 to all of them item_ids = [str(i) for i in range(1, 51)] for item_id in item_ids: print "Add item " + item_id client.create_item(item_id, ('1',)) ######################################################## # each user randomly views 10 items for user_id in user_ids: for viewed_item in random.sample(item_ids, 10): print "User " + user_id + " views item " + viewed_item client.identify(user_id) client.record_action_on_item("view", viewed_item) ######################################################## client.close()
import predictionio import sys import json # arguments: pid_file, u2ifile, brand-cat-option, label key = "c9E6ehRTIIRUs3ZLgqXjsXWyr6CSGKGiHRoWINzk2POfzcjWSyeoglEeQVmWYfkK" client = predictionio.Client(key, qsize=1000) pid_file = sys.argv[1] u2i_file = sys.argv[2] brand_cat_option = sys.argv[3] label = sys.argv[4] pid_file_brand_idx = 6 pid_file_pid_idx = 0 u2i_file_pid_idx = 3 u2i_file_uid_idx = 4 if brand_cat_option == '1': print "use brand_cat_option 1" cat_idx = 4 elif brand_cat_option == '2': print "use brand_cat_option 2" cat_idx = 3 elif brand_cat_option == '3': print "use brand_cat_option 3" cat_idx = 2 pid_list = [] brand_cat = [] pid_f = open(pid_file) for p in pid_f: