-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
100 lines (79 loc) · 4.49 KB
/
models.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
from datetime import datetime
from dateutil.relativedelta import relativedelta
class Forecasts:
def __init__(self):
with open('./model/furniture_forecast.json') as f:
self.dataFurniture = json.load(f)
with open('./model/office_forecast.json') as f:
self.dataOffice = json.load(f)
with open('./model/tech_forecast.json') as f:
self.dataTech = json.load(f)
def verify(self):
va = list(self.dataFurniture.keys())[0]
print(datetime.strptime(va,'%Y-%m-%d').date())
def predict(self,category,date):
date = datetime.strptime(date,'%Y-%m-%d').date()
date_before_month = date + relativedelta(months=-1)
date_after_month = date+ relativedelta(months=1)
date = datetime.strftime(date,'%Y-%m-%d')
date_before_month = datetime.strftime(date_before_month,'%Y-%m-%d')
date_after_month = datetime.strftime(date_after_month,'%Y-%m-%d')
if category == 'Furniture':
dic = {}
dic['present'] = {}
dic['next']= {}
dic['previous'] = {}
dic['present']['qty'] = round(self.dataFurniture.get(date))
dic['next']['qty'] = round(self.dataFurniture.get(date_after_month))
dic['previous']['qty'] = round(self.dataFurniture.get(date_before_month))
dic['present']['date'] = datetime.strftime(datetime.strptime(date,'%Y-%m-%d').date(),'%b-%Y')
dic['next']['date'] = datetime.strftime(datetime.strptime(date_after_month,'%Y-%m-%d').date(),'%b-%Y')
dic['previous']['date'] = datetime.strftime(datetime.strptime(date_before_month,'%Y-%m-%d').date(),'%b-%Y')
return json.dumps(dic)
elif category == 'Office Supplies':
dic = {}
dic['present'] = {}
dic['next']= {}
dic['previous'] = {}
dic['present']['qty'] = round(self.dataOffice.get(date))
dic['next']['qty'] = round(self.dataOffice.get(date_after_month))
dic['previous']['qty'] = round(self.dataOffice.get(date_before_month))
dic['present']['date'] = datetime.strftime(datetime.strptime(date,'%Y-%m-%d').date(),'%b-%Y')
dic['next']['date'] = datetime.strftime(datetime.strptime(date_after_month,'%Y-%m-%d').date(),'%b-%Y')
dic['previous']['date'] = datetime.strftime(datetime.strptime(date_before_month,'%Y-%m-%d').date(),'%b-%Y')
return json.dumps(dic)
elif category == 'Technology':
dic = {}
dic['present'] = {}
dic['next']= {}
dic['previous'] = {}
dic['present']['qty'] = round(self.dataTech.get(date))
dic['next']['qty'] = round(self.dataTech.get(date_after_month))
dic['previous']['qty'] = round(self.dataTech.get(date_before_month))
dic['present']['date'] = datetime.strftime(datetime.strptime(date,'%Y-%m-%d').date(),'%b-%Y')
dic['next']['date'] = datetime.strftime(datetime.strptime(date_after_month,'%Y-%m-%d').date(),'%b-%Y')
dic['previous']['date'] = datetime.strftime(datetime.strptime(date_before_month,'%Y-%m-%d').date(),'%b-%Y')
return json.dumps(dic)
else:
return 'Invalid Input'
def subCatPredict(self,subcate,date):
date = datetime.strptime(date,'%Y-%m-%d').date()
date_before_month = date + relativedelta(months=-1)
date_after_month = date+ relativedelta(months=1)
date = datetime.strftime(date,'%Y-%m-%d')
date_before_month = datetime.strftime(date_before_month,'%Y-%m-%d')
date_after_month = datetime.strftime(date_after_month,'%Y-%m-%d')
with open('./model/Sub-Categorical-JSON/{}_forecast.json'.format(subcate)) as f:
self.dataSub = json.load(f)
dic = {}
dic['present'] = {}
dic['next']= {}
dic['previous'] = {}
dic['present']['qty'] = round(self.dataSub.get(date))
dic['next']['qty'] = round(self.dataSub.get(date_after_month))
dic['previous']['qty'] = round(self.dataSub.get(date_before_month))
dic['present']['date'] = datetime.strftime(datetime.strptime(date,'%Y-%m-%d').date(),'%b-%Y')
dic['next']['date'] = datetime.strftime(datetime.strptime(date_after_month,'%Y-%m-%d').date(),'%b-%Y')
dic['previous']['date'] = datetime.strftime(datetime.strptime(date_before_month,'%Y-%m-%d').date(),'%b-%Y')
return json.dumps(dic)