def test_DeleteBox(self): DataAPI.AddFridge(conn, "F21", 0, 10, 10, 10) DataAPI.AddBox(conn, "B15", "F21", 1, 2, 10, 10, 10) result = DataAPI.DeleteBox(conn, "B15") print("UNIT TEST 9: " + str(result)) DataAPI.DeleteFridge(conn, "F21") self.assertEqual(result,"Box B15 successfully deleted")
def do_hybrid_recommendation(test): test_set = "Test" + str(test) users = DataAPI.read_users_as_id_list() movies = DataAPI.read_movies_as_id_list() ratings = DataAPI.read_ratings(test_set) # Do K-Nearest Neigbour #KNN(test) head_ratings = DataAPI.read_recommendation_matrix("v2.1NearestNeighbour", test_set) # Do Content Based #calculate_recommendation_matrix(test_set) tail_ratings = DataAPI.read_recommendation_matrix("Weighted Content Based", test_set) # Merge results head_movies, tail_movies = get_head_and_tail(41, users, movies, ratings) new_ratings = merge(head_movies, tail_movies, head_ratings, tail_ratings, users) # Generate recommendations recommendations = [] for user in range(0, len(users)): recommendations.insert( user, recommend(user, ratings, movies, new_ratings, 10, head_movies, tail_movies)) print(user, ":", recommendations[user - 1]) return recommendations
def UpdateTradeFactorCatch(begintime,endtime): print begintime stock_data=di.GetDalyTrd(BeginTime=begintime,EndTime=str(endtime)[0:10]) ff=di.GetFF3F(BeginTime=begintime,EndTime=str(endtime)[0:10]) stock_data=pd.merge(stock_data,ff) stock_data['c']=1 i=0 TradeFactor=pd.DataFrame() for code in stock_data['Stkcd'].unique(): try: mid=stock_data[stock_data['Stkcd']==code] (x1,res,rank,s)=np.linalg.lstsq(mid[['MKT','c']],mid['Dretwd']) mid['e1']=mid['Dretwd']-np.dot(mid[['MKT','c']],x1) r1=1-res/((mid['Dretwd']-mid['Dretwd'].mean())*(mid['Dretwd']-mid['Dretwd'].mean())).sum() (x2,res,rank,s)=np.linalg.lstsq(mid[['MKT','SMB','HML','c']],mid['Dretwd']) mid['e2']=mid['Dretwd']-np.dot(mid[['MKT','SMB','HML','c']],x2) r2=1-res/((mid['Dretwd']-mid['Dretwd'].mean())*(mid['Dretwd']-mid['Dretwd'].mean())).sum() record=pd.DataFrame({'Trdmnt':str(endtime+pd.to_timedelta(5,'D'))[0:7],'Stkcd':code,'BetaCapm':x1[0],\ 'AlphaCapm':x1[1],'RCapm':r1,'StdECapm':mid['e1'].std(),'BetaMKT':x2[0],'BetaSMB':x2[1],\ 'BetaHML':x2[2],'RFF':r2,'StdEFF':mid['e2'].std(),'AlphaFF':x2[3]},index=[i]) except: pass TradeFactor=pd.concat([TradeFactor,record]) i+=1 return TradeFactor
def test_AddSampleNonExistingBox(self): DataAPI.AddCollection(conn, "C10", "D1", "client1", "1", "1", "1", "1", "1", "1", "1") result=DataAPI.AddSample(conn, "S20", "B10", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") print("UNIT TEST 22: " + str(result)) DataAPI.DeleteSample(conn, "S20") DataAPI.DeleteCollection(conn, "C10") self.assertEqual(result,"Box B10 does not exist")
def AddCollection(): _collectionTitle = newCollectionTitle.get() _username = newCollectionTitle.get() _password = password.get() _donorID = donorID.get() _clientName = clientName.get() _clientPhone = clientPhone.get() _clientEmail = clientEmail.get() _clientOrganization = clientOrganization.get() _clientStreet = clientStreet.get() _clientCity = clientCity.get() _clientCountry = clientCountry.get() _clientPostalCode = clientPostalCode.get() if any([ _collectionTitle == "", _username == "", _password == "", _donorID == "", _clientName == "", _clientPhone == "", _clientEmail == "", _clientOrganization == "", _clientStreet == "", _clientCity == "", _clientCountry == "", _clientPostalCode == "" ]): MessagePopup("One or more fields are missing data", "ERROR") else: messageText1 = DataAPI.AddCollection( conn, _collectionTitle, _donorID, _clientName, _clientPhone, _clientEmail, _clientOrganization, _clientStreet, _clientCity, _clientCountry, _clientPostalCode) MessagePopup(messageText1, "COLLECTION RECORD STATUS") messageText2 = DataAPI.AddUser(conn, _username, _password, "Customer") MessagePopup(messageText2, "COLLECTION USER STATUS")
def test_AddSuccessfulBox(self): DataAPI.AddFridge(conn, "F30", 0, 10, 10, 10) result = DataAPI.AddBox(conn, "B30", "F30", 1, 2, 10, 10, 10) print("UNIT TEST 6: " + str(result)) DataAPI.DeleteBox(conn, "B30") DataAPI.DeleteFridge(conn, "F30") self.assertEqual(result,"Successfully added Box B30")
def KNN(x): directory = "Test" + str(x) users = DataAPI.read_users_as_id_list() movies = DataAPI.read_movies_as_id_list() ratings = DataAPI.read_ratings(directory) new_ratings = do_K_nearest_neighbour(users, movies, ratings, directory) output = open( "../v2.1NearestNeighbour/Output/Test" + str(x) + "/ratings.data", "w") output.write(" ID, ") for movie in movies: output.write("{:>5}".format(movie) + (", " if movie < len(movies) else "")) i = 0 output.write("\n") for user in users: i += 1 print("Writing", round((i / len(users)) * 100, 1), "%") output.write("{:>5}".format(user) + ", ") for movie in movies: output.write("{: .2f}".format(new_ratings[user - 1][movie - 1]) + (", " if movie < len(movies) else "")) output.write("\n") if not output.closed: output.close() return new_ratings
def test_MoveNonExistingSample(self): DataAPI.AddFridge(conn, "F22", 0, 10, 10, 10) DataAPI.AddBox(conn, "B16", "F22", 1, 1, 10, 10, 10) result = DataAPI.MoveSample(conn, "S20", "B16", 1,1,1) print("UNIT TEST 28: " + str(result)) DataAPI.DeleteBox(conn, "B16") DataAPI.DeleteFridge(conn, "F22") self.assertEqual(result,"Sample ID S20 does not exist")
def test_MoveBoxNonExistingFridge(self): DataAPI.AddFridge(conn, "F22", 0, 10, 10, 10) DataAPI.AddBox(conn, "B16", "F22", 1, 2, 10, 10, 10) result = DataAPI.MoveBox(conn, "B16", "F23", 1, 2) print("UNIT TEST 13: " + str(result)) DataAPI.DeleteBox(conn, "B16") DataAPI.DeleteFridge(conn, "F22") self.assertEqual(result,"Fridge ID F23 does not exist")
def test_MoveBoxInvalidYPos(self): DataAPI.AddFridge(conn, "F20", 0, 10, 10, 100) DataAPI.AddBox(conn, "B13", "F20", 1, 1, 1, 1, 1) result = DataAPI.MoveBox(conn, "B13", "F20", 1, 100) print("UNIT TEST 17: " + str(result)) DataAPI.DeleteBox(conn, "B13") DataAPI.DeleteFridge(conn, "F20") self.assertEqual(result,"Invalid Y location")
def test_AddBoxTakenPosition(self): DataAPI.AddFridge(conn, "F20", 0, 10, 10, 10) DataAPI.AddBox(conn, "B12", "F20", 1, 1, 1, 1, 1) result = DataAPI.AddBox(conn, "B11", "F20", 1, 1, 1, 1, 1) print("UNIT TEST 8: " + str(result)) DataAPI.DeleteBox(conn, "B12") DataAPI.DeleteFridge(conn, "F20") self.assertNotEqual(result,"Successfully added Box B11")
def test_AddExistingBox(self): DataAPI.AddFridge(conn, "F20", 0, 10, 10, 10) DataAPI.AddBox(conn, "B12", "F20", 1, 1, 1, 1, 1) result = DataAPI.AddBox(conn, "B12", "F20", 1, 2, 1, 1, 1) print("UNIT TEST 7: " + str(result)) DataAPI.DeleteBox(conn, "B12") DataAPI.DeleteFridge(conn, "F20") self.assertNotEqual(result, "Successfully added Box B12")
def KNN(x): directory = "Test" + str(x) users = DataAPI.read_users_as_id_list() movies = DataAPI.read_movies_as_id_list() ratings = DataAPI.read_ratings(directory) i = 0 rated = ratings weight_matrix = calculate_weight_matrix(movies, ratings, users, x) starting_time = time.time() for user in users: i += 1 current_time = time.time() elapsed_time = current_time - starting_time remaining_time = ((elapsed_time * len(users)) / i) - elapsed_time print(x, " Rater", round((i / len(users)) * 100, 1), "% tid brugt: ", format_time(elapsed_time), " tid tilbage: ", format_time(remaining_time)) for movie in movies: if ratings[user - 1][movie - 1] == 0.0: rated[user - 1][movie - 1] = (rate(movie, user, users, ratings, weight_matrix)) for user in users: for movie in movies: if rated[user - 1][movie - 1] > 5: rated[user - 1][movie - 1] = 5 elif rated[user - 1][movie - 1] < 1: rated[user - 1][movie - 1] = 1 output = open("Output/Test" + str(x) + "/ratings.data", "w") output.write(" ID, ") for movie in movies: output.write("{:>5}".format(movie) + (", " if movie < len(movies) else "")) i = 0 output.write("\n") for user in users: i += 1 print("Writing", round((i / len(users)) * 100, 1), "%") output.write("{:>5}".format(user) + ", ") for movie in movies: output.write("{: .2f}".format(rated[user - 1][movie - 1]) + (", " if movie < len(movies) else "")) output.write("\n") if not output.closed: output.close()
def test_DeleteSampleTest(self): DataAPI.AddFridge(conn, "F20", 0, 10, 10, 10) DataAPI.AddBox(conn, "B10", "F20", 1, 1, 10, 10, 10) DataAPI.AddCollection(conn, "C10", "D1", "client1", "1", "1", "1", "1", "1", "1", "1") DataAPI.AddSample(conn, "S20", "B10", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") DataAPI.AddSampleTest(conn, "S20", "testType", "testResult") result = DataAPI.DeleteSampleTest(conn, "S20") print("UNIT TEST 41: " + str(result)) DataAPI.DeleteSample(conn, "S20") DataAPI.DeleteCollection(conn, "C10") DataAPI.DeleteBox(conn, "B10") DataAPI.DeleteFridge(conn, "F20") self.assertEqual(result,"Successfully deleted sample test from sample ID S20")
def test_MoveSampleNonExistingBox(self): DataAPI.AddFridge(conn, "F22", 0, 10, 10, 10) DataAPI.AddBox(conn, "B16", "F22", 1, 1, 10, 10, 10) DataAPI.AddCollection(conn, "C10", "D1", "client1", "1", "1", "1", "1", "1", "1", "1") DataAPI.AddSample(conn, "S20", "B16", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") result = DataAPI.MoveSample(conn, "S20", "B17", 1,1,1) print("UNIT TEST 27: " + str(result)) DataAPI.DeleteSample(conn, "S20") DataAPI.DeleteCollection(conn, "C10") DataAPI.DeleteBox(conn, "B16") DataAPI.DeleteFridge(conn, "F22") self.assertEqual(result,"Box ID B17 does not exist")
def test_DeleteCollectionNotEmpty(self): DataAPI.AddFridge(conn, "F22", 0, 10, 10, 10) DataAPI.AddBox(conn, "B16", "F22", 1, 1, 10, 10, 10) DataAPI.AddCollection(conn, "C10", "D1", "client1", "1", "1", "1", "1", "1", "1", "1") DataAPI.AddSample(conn, "S20", "B16", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") result = DataAPI.DeleteCollection(conn, "C10") print("UNIT TEST 38: " + str(result)) DataAPI.DeleteSample(conn, "S20") DataAPI.DeleteCollection(conn, "C10") DataAPI.DeleteBox(conn, "B10") DataAPI.DeleteFridge(conn, "F20") self.assertEqual(result, "Collection C10 is not empty - cannot be deleted")
def test_MoveSampleInvalidZPosition(self): DataAPI.AddFridge(conn, "F20", 0, 10, 10, 10) DataAPI.AddBox(conn, "B10", "F20", 1, 1, 10, 10, 10) DataAPI.AddCollection(conn, "C10", "D1", "client1", "1", "1", "1", "1", "1", "1", "1") DataAPI.AddSample(conn, "S20", "B10", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") result = DataAPI.MoveSample(conn, "S20", "B10", 1, 1, 100) print("UNIT TEST 32: " + str(result)) DataAPI.DeleteSample(conn, "S20") DataAPI.DeleteCollection(conn, "C10") DataAPI.DeleteBox(conn, "B10") DataAPI.DeleteFridge(conn, "F20") self.assertEqual(result,"Invalid Z location in box B10")
def test_AddSampleTakenPosition(self): DataAPI.AddFridge(conn, "F20", 0, 10, 10, 10) DataAPI.AddBox(conn, "B10", "F20", 1, 1, 10, 10, 10) DataAPI.AddCollection(conn, "C10", "D1", "client1", "1", "1", "1", "1", "1", "1", "1") DataAPI.AddSample(conn, "S20", "B10", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") result=DataAPI.AddSample(conn, "S21", "B10", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") print("UNIT TEST 20: " + str(result)) DataAPI.DeleteSample(conn, "S20") DataAPI.DeleteCollection(conn, "C10") DataAPI.DeleteBox(conn, "B10") DataAPI.DeleteFridge(conn, "F20") self.assertNotEqual(result,"Successfully added sample S20")
def test_DeleteNonEmptyBox(self): DataAPI.AddFridge(conn, "F29", 0, 10, 10, 100) DataAPI.AddBox(conn, "B30", "F29", 1, 2, 10, 10, 10) DataAPI.AddCollection(conn, "C10", "D1", "client1", "1", "1", "1", "1", "1", "1", "1") DataAPI.AddSample(conn, "S20", "B30", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") result = DataAPI.DeleteBox(conn, "B30") print("UNIT TEST 11: " + str(result)) DataAPI.DeleteSample(conn, "S20") DataAPI.DeleteCollection(conn, "C10") DataAPI.DeleteBox(conn, "B30") DataAPI.DeleteFridge(conn, "F29") self.assertEqual(result, "Box does not exist or is not empty - cannot be deleted")
def Warning_Window(conn): def CloseWarningWindow(): window_Warning.destroy() sampleDateWarning = DataAPI.CheckAllSampleDates(conn) if (sampleDateWarning != ""): window_Warning = tk.Tk() window_Warning.title("SAMPLE DATE WARNING") window_Warning["bg"] = 'cadet blue' text = tk.Text(window_Warning) myFont = Font(family="fixedsys", size=12) text.configure(font=myFont) tk.Label(window_Warning, text=sampleDateWarning, bg="cadet blue", font=myFont).grid(row=1, column=1) tk.Button(window_Warning, text='Continue', command=CloseWarningWindow, font=myFont).grid(row=2, column=1) tk.Label(window_Warning, height=1, width=2, bg='cadet blue').grid(row=0, column=0) tk.Label(window_Warning, height=1, width=2, bg='cadet blue').grid(row=3, column=2) window_Warning.mainloop()
def CreateSample(): try: _sampleID = sampleID.get() _boxID = boxID.get() _boxX = int(boxX.get()) _boxY = int(boxY.get()) _boxZ = int(boxZ.get()) _sampleType = sampleType.get() _originCountry = originCountry.get() _collectionDate = collectionDate.get() _entryDate = entryDate _subjectAge = int(subjectAge.get()) _tubeRating = int(tubeRating.get()) _collectionTitle = collectionTitle.get() _returnType = returnType.get() _returnDate = returnDate.get() _phenotypeValue = phenotypeValue.get() _diseaseState = diseaseState.get() messagebox.showinfo( "Add Sample", DataAPI.AddSample(conn, _sampleID, _boxID, _boxX, _boxY, _boxZ, _sampleType, _originCountry, _collectionDate, _entryDate, _subjectAge, _tubeRating, _collectionTitle, _returnType, _returnDate, _phenotypeValue, _diseaseState)) except: messagebox.showinfo("Add Sample", "ERROR: Invalid data entered")
def get_factor_by_day(tdate): ''' 获取给定日期的因子信息 参数: tdate, 时间,格式%Y%m%d 返回: DataFrame, 返回给定日期的70个因子值 ''' cnt = 0 while True: try: x = DataAPI.MktStockFactorsOneDayProGet( tradeDate=tdate, secID=u"", ticker=u"", field=['ticker', 'tradeDate'] + factors, pandas="1") x['tradeDate'] = x['tradeDate'].apply(lambda x: x.replace("-", "")) return x except Exception as e: cnt += 1 if cnt >= 3: print('error get factor data: ', tdate) break
def CreateFridge(): _fridgeID = fridgeID.get() _temperature = temp.get() _numShelves = numShelves.get() _widthShelves = widthShelves.get() _rate = rate.get() try: _temperature = int(_temperature) _numShelves = int(_numShelves) _widthShelves = int(_widthShelves) _rate = float(_rate) intCheck = "TRUE" except: intCheck = "FALSE" if any([ _fridgeID == "", _temperature == "", _numShelves == "", _widthShelves == "", _rate == "" ]): MessagePopup("One or more fields are missing data", "ERROR") elif intCheck == "FALSE": MessagePopup("One or more fields are wrong data type", "ERROR") else: try: messageText = DataAPI.AddFridge(conn, _fridgeID, _temperature, _numShelves, _widthShelves, _rate) MessagePopup(messageText, "Add Fridge") except: MessagePopup("ERROR: Invalid data entered", "Add Fridge")
def CreateBox(): _boxID = boxID.get() _fridgeID = fridgeID.get() _fridgeX = fridgeX.get() _fridgeY = fridgeY.get() _boxX = boxX.get() _boxY = boxY.get() _boxZ = boxZ.get() try: _fridgeX = int(_fridgeX) _fridgeY = int(_fridgeY) _boxX = int(_boxX) _boxY = int(_boxY) _boxZ = int(_boxZ) intCheck = "TRUE" except: intCheck = "FALSE" if any([ _boxID == "", _fridgeID == "", _fridgeX == "", _fridgeY == "", _boxX == "", _boxY == "", _boxZ == "" ]): MessagePopup("One or more fields are missing data", "ERROR") elif intCheck == "FALSE": MessagePopup("One or more fields are wrong data type", "ERROR") else: try: messageText = DataAPI.AddBox(conn, _boxID, _fridgeID, _fridgeX, _fridgeY, _boxX, _boxY, _boxZ) MessagePopup(messageText, "Add Box") except: MessagePopup("ERROR: Invalid data entered", "Add Box")
def get_factor_data(ticker, begin_date, end_date, factor_name): data = DataAPI.MktIdxdEvalGet(ticker=ticker, beginDate=begin_date, endDate=end_date, field=[u"ticker", u"tradeDate", factor_name], pandas="1") return data
def AddUser(): try: _username = username.get() _password = password.get() _accessLevel = accessLevel.get() messagebox.showinfo("Add User", DataAPI.AddUser(conn, _username, _password, _accessLevel)) except: messagebox.showinfo("Add User", "ERROR: Invalid data entered")
def get_index_data(ticker, begin_date, end_date): data = DataAPI.MktIdxdGet(ticker=ticker, beginDate=begin_date, endDate=end_date, exchangeCD=u"XSHE,XSHG", field=u"", pandas="1") data.to_csv(ticker + '.csv') return data
def test_DeleteSample(self): DataAPI.AddFridge(conn, "F22", 0, 10, 10, 10) DataAPI.AddBox(conn, "B16", "F22", 1, 1, 10, 10, 10) DataAPI.AddCollection(conn, "C10", "D1", "client1", "1", "1", "1", "1", "1", "1", "1") DataAPI.AddSample(conn, "S20", "B16", 1, 1, 1, "BLOOD", "SA", "2020/04/20", "2020/04/20", 20, 0, "C10", "Destroy", "2020/06/20", "Male", "YES") result = DataAPI.DeleteSample(conn, "S20") print("UNIT TEST 33: " + str(result)) DataAPI.DeleteCollection(conn, "C10") DataAPI.DeleteBox(conn, "B16") DataAPI.DeleteFridge(conn, "F22") self.assertEqual(result,"Sample S20 successfully deleted")
def AutoAdd(): _fileName = fileName.get() if _fileName == "": MessagePopup("Please enter valid file", "ERROR") else: result = DataAPI.CommitAuto(conn, _fileName) if result == "TRUE": MessagePopup("Successfully added all samples!", "Success") else: MessagePopup(result, "ERROR")
def CreateBox(): try: _boxID = boxID.get() _fridgeID = fridgeID.get() _boxX = int(boxX.get()) _boxY = int(boxY.get()) _boxZ = int(boxZ.get()) print(DataAPI.AddBox(conn, _boxID, _fridgeID, _boxX, _boxY, _boxZ)) except: print("ERROR: Invalid data entered")