Alvianojs commited on
Commit
ddd31fd
·
1 Parent(s): d7aa6c5

Upload map_viz_pred.py

Browse files
Files changed (1) hide show
  1. src/map_viz_pred.py +99 -0
src/map_viz_pred.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import division, print_function
2
+ from six import StringIO
3
+ from svgpath2mpl import parse_path
4
+ from collections import defaultdict
5
+ import xml.etree.ElementTree as etree
6
+ import re
7
+ import matplotlib as mpl
8
+ import matplotlib.pyplot as plt
9
+ import numpy as np
10
+ import requests
11
+ import pandas as pd
12
+ import datetime
13
+
14
+ def calling_pred_map_viz():
15
+ r = "snazzy-image-01.svg"
16
+ tree = etree.parse(r)
17
+ root = tree.getroot()
18
+ path_elems = root.findall('.//{http://www.w3.org/2000/svg}path')
19
+
20
+ paths = [parse_path(elem.attrib['d']) for elem in path_elems]
21
+ facecolors = []
22
+ edgecolors = []
23
+ linewidths = []
24
+ for elem in path_elems:
25
+ facecolors.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("fill", "none"))
26
+ edgecolors.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("stroke", "none"))
27
+ linewidths.append(dict(item.split(":") for item in elem.attrib.get('style', 'none').split(";")).get("stroke-width", "none").replace("px", ""))
28
+
29
+ path_id = defaultdict(int)
30
+ for i, elem in enumerate(path_elems):
31
+ try:
32
+ #print(i, elem.attrib['id'])
33
+ path_id[elem.attrib['id']] = i
34
+ except:
35
+ continue
36
+
37
+
38
+ counts_df1 = merge_volumes(counts_df)
39
+ counts_df1.loc[:,'date_time'] = pd.to_datetime(counts_df1.loc[:,'date'] + " "+ counts_df1.loc[:,'time'], format='%Y-%m-%d %H:%M:%S')
40
+ counts_df1.loc[:,'hour'] = counts_df1.loc[:,'date_time'].apply(hour_rounder)
41
+ counts_df1.loc[:,'day_name'] = counts_df1.loc[:,'date_time'].dt.day_name()
42
+ filtered_date = counts_df1.iloc[-1]['date']
43
+ filtered_time = counts_df.iloc[-1]['time']
44
+ filtered_day = counts_df1.iloc[-1]['day_name']
45
+ filtered_hour = counts_df1.iloc[-1]['hour']
46
+
47
+
48
+ day_hour_view_group = counts_df1.groupby(by=['view', 'day_name', 'hour'])['total'].mean().reset_index()
49
+ count_max = day_hour_view_group['total'].max()
50
+ count_min = day_hour_view_group['total'].min()
51
+
52
+
53
+ count_dict = {"woodlands_to_sg" :date_view_group.loc[(date_view_group['view'] == 'Woodlands - to SG') & (date_view_group['day_name'] == filtered_day) & (date_view_group['hour'] == filtered_hour), "total" ].iloc[0],
54
+ "woodlands_to_jh" :date_view_group.loc[(date_view_group['view'] == 'Woodlands - to Johor') & (date_view_group['day_name'] == filtered_day) & (date_view_group['hour'] == filtered_hour), "total" ].iloc[0],
55
+ "tuas_to_sg" :date_view_group.loc[(date_view_group['view'] == 'Tuas - to SG') & (date_view_group['day_name'] == filtered_day) & (date_view_group['hour'] == filtered_hour), "total" ].iloc[0],
56
+ "tuas_to_jh" :date_view_group.loc[(date_view_group['view'] == 'Tuas - to Johor') & (date_view_group['day_name'] == filtered_day) & (date_view_group['hour'] == filtered_hour), "total" ].iloc[0]
57
+ }
58
+
59
+ values = np.array([0., 0.5, 1.])
60
+ values = np.sort(np.array(values))
61
+ values = np.interp(values, (values.min(), values.max()), (0., 1.))
62
+ colors = ["#539f6b", "#ffc835", "#bf0000"]
63
+ cmap = mpl.colors.LinearSegmentedColormap.from_list("custom", list(zip(values, colors)))
64
+
65
+ norm = mpl.colors.Normalize(vmin=count_min, vmax=count_max)
66
+
67
+
68
+
69
+ hex_dict = {k: mpl.colors.to_hex(cmap(norm(v))) for k, v in count_dict.items()}
70
+ color_dict = defaultdict(str)
71
+
72
+ for k, i in path_id.items():
73
+ #print(k, i)
74
+ color_dict[i] = hex_dict[k]
75
+
76
+ for k, i in color_dict.items():
77
+ #print(k,i)
78
+ facecolors[k] = i
79
+
80
+ collection = mpl.collections.PathCollection(paths,
81
+ edgecolors=edgecolors,
82
+ linewidths=[int(i)/100 for i in linewidths if i != 'none'],
83
+ facecolors=[i.strip() for i in facecolors])
84
+
85
+
86
+
87
+ fig = plt.figure(figsize=(10,10))
88
+ ax = fig.add_subplot(111)
89
+ # ax.close()
90
+ collection.set_transform(ax.transData)
91
+ ax.add_artist(collection)
92
+ ax.set_xlim([100, 1900])
93
+ ax.set_ylim([1800, 0])
94
+ ax.text(200, 100, '1,000,000', color='r', size='large', name = 'Georgia')
95
+ ax.set_title(datetime.datetime.strptime(last_date, '%Y-%m-%d').strftime('%a, %d %B %Y') + " | " + last_time + " SGT", fontname = 'Georgia')
96
+ plt.show()
97
+
98
+
99
+ calling_map_viz()