This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelUpdate.py
77 lines (70 loc) · 2.95 KB
/
ModelUpdate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import csv
import glob
import time
import shutil
import pandas as pd
from datetime import datetime
"""
Update current model function
"""
def updateModel():
# read the oldest file name
filePath = sorted(glob.glob(r"input/UP*"), reverse=False)[0]
fileName = os.path.basename(filePath)
# get the data from currentModel.csv, if it is not exist, the new one will be created
try:
with open('processed/currentModel.csv', 'r') as f:
reader = csv.reader(f)
currentModelList = list(reader)
except FileNotFoundError:
print("Current Model is not exist!")
print("Current Model created!")
open('processed/currentModel.csv', 'w+')
with open('processed/currentModel.csv', 'r') as f:
reader = csv.reader(f)
currentModelList = list(reader)
# get the upList from UP file and remove the null and repeat row
df = pd.read_csv(filePath, names=['uid','iid','score'])
df.drop_duplicates(keep='first', inplace=True) # dropping all repeat row
df = df.dropna() # remove all row which contain null
df['uid'] = df['uid'].astype('int')
df['iid'] = df['iid'].astype('int')
df = df.astype(str)
df['score'] = df['score'].str.replace('.0', '')
upList = df.values.tolist() # change the data from dataframe to list
# start updating model
timestamp = datetime.utcnow().strftime('%H:%M:%S.%f')[:-3]
print(timestamp + " Model updating start: " + fileName)
# combined 2 file
with open('processed/currentModel.csv', 'w', newline='') as rowFile:
rowFileWriter = csv.writer(rowFile)
combined = []
# if user change the score, update the score in current model
for cm_row in currentModelList:
combined_row = []
combined_row.extend(cm_row)
for up_row in upList:
if combined_row[0] == up_row[0] and combined_row[1] == up_row[1]:
combined_row[2] = up_row[2]
combined.append(combined_row)
# add the row to current if the row is not exist in current model
for up_row in upList:
found = False
for cm_row in currentModelList:
if cm_row[0] == up_row[0] and cm_row[1] == up_row[1]:
found = True
break
if found == False:
combined.append(up_row)
# sort the combined data and write in current model
combined = sorted(combined, key=lambda x: x[0], reverse=False)
for cb_row in combined:
rowFileWriter.writerow(cb_row)
# move the UP file to processed file
inputPath = "input/" + fileName
processedPath = "processed/" + fileName
shutil.move(inputPath, processedPath)
# end updating model
timestamp = datetime.utcnow().strftime('%H:%M:%S.%f')[:-3]
print(timestamp + " Model updating end: " + fileName)