-
Notifications
You must be signed in to change notification settings - Fork 1
/
__main__.py
201 lines (180 loc) · 6.63 KB
/
__main__.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Tag Maps Clustering
- will read in geotagged (lat/lng) decimal degree point data
- will generate HDBSCAN Cluster Hierarchy
- will output shapefiles with Alpha Shapes/Delauney
for cluster cut at specific distance
"""
__author__ = "Alexander Dunkel"
__license__ = "GNU GPLv3"
import sys
import time
from tagmaps.classes.cluster import ClusterGen
from tagmaps.classes.compile_output import Compile
from tagmaps.classes.interface import UserInterface
from tagmaps.classes.load_data import LoadData
from tagmaps.classes.shared_structure import EMOJI, LOCATIONS, TAGS
from tagmaps.classes.utils import Utils
def main():
"""Main tag maps function for direct execution of package
- will read from 01_Input/ folder
- will output clustered data to 02_Output/
"""
# initialize logger and config
cfg, log = Utils.init_main()
lbsn_data = LoadData(cfg)
print('\n')
log.info(
"########## "
"STEP 1 of 6: Data Cleanup "
"##########")
lbsn_data.parse_input_records()
# get current time
now = time.time()
# get cleaned data for use in clustering
cleaned_post_dict = lbsn_data.get_cleaned_post_dict()
cleaned_post_list = list(cleaned_post_dict.values())
# status report
log.info(
f'\nTotal user count (UC): '
f'{len(lbsn_data.locations_per_userid_dict)}')
log.info(
f'Total post count (PC): '
f'{lbsn_data.stats.count_glob:02d}')
log.info(
f'Total tag count (PTC): '
f'{lbsn_data.stats.count_tags_global}')
log.info(
f'Total emoji count (PEC): '
f'{lbsn_data.stats.count_emojis_global}')
log.info(
f'Total user post locations (UPL): '
f'{len(lbsn_data.distinct_userlocations_set)}')
log.info(
lbsn_data.bounds.get_bound_report())
# get prepared data for statistics and clustering
prepared_data = lbsn_data.get_prepared_data()
if (cfg.cluster_tags or cfg.cluster_emoji):
log.info(
"\n########## "
"STEP 2 of 6: Tag Ranking "
"##########")
location_name_count = len(
prepared_data.locid_locname_dict)
if location_name_count > 0:
log.info(
f"Number of locations with names: "
f"{location_name_count}")
log.info(
f'Total distinct tags (DTC): '
f'{prepared_data.total_unique_tags}')
log.info(
f'Total distinct emoji (DEC): '
f'{prepared_data.total_unique_emoji}')
log.info(
f'Total distinct locations (DLC): '
f'{prepared_data.total_unique_locations}')
log.info(
f'Total tags count for the '
f'{prepared_data.tmax} '
f'most used tags in selected area: '
f'{prepared_data.total_tag_count}.')
log.info(
f'Total emoji count for the '
f'{prepared_data.emax} '
f'most used emoji in selected area: '
f'{prepared_data.total_emoji_count}.')
if cfg.statistics_only is False:
# restart time monitoring for monitoring of
# actual cluster step
now = time.time()
log.info(
"\n########## "
"STEP 3 of 6: Tag & Emoji "
"Location Clustering "
"##########")
# initialize list of types to cluster
cluster_types = list()
if cfg.cluster_tags:
cluster_types.append(TAGS)
if cfg.cluster_emoji:
cluster_types.append(EMOJI)
if cfg.cluster_locations:
cluster_types.append(LOCATIONS)
# initialize clusterers
clusterer_list = list()
for cls_type in cluster_types:
clusterer = ClusterGen.new_clusterer(
clusterer_type=cls_type,
bounds=lbsn_data.bounds,
cleaned_post_dict=cleaned_post_dict,
cleaned_post_list=cleaned_post_list,
prepared_data=prepared_data,
local_saturation_check=cfg.local_saturation_check
)
clusterer_list.append(clusterer)
# get user input for cluster distances
if not cfg.auto_mode:
user_intf = UserInterface(
clusterer_list,
prepared_data.locid_locname_dict)
user_intf.start()
if cfg.auto_mode or user_intf.abort is False:
for clusterer in clusterer_list:
if clusterer.cls_type == LOCATIONS:
# skip location clustering for now
continue
if clusterer.cls_type == TAGS:
log.info("Tag clustering: ")
else:
log.info("Emoji clustering: ")
clusterer.get_itemized_clusters()
log.info(
"########## "
"STEP 4 of 6: Generating Alpha Shapes "
"##########")
# store results for tags and emoji in one list
shapes_and_meta_list = list()
for clusterer in clusterer_list:
if clusterer.cls_type == LOCATIONS:
# skip location clustering for now
continue
cluster_shapes = clusterer.get_cluster_shapes()
shapes_and_meta_list.append(cluster_shapes)
log.info(
"########## "
"STEP 5 of 6: Writing Results to Shapefile "
"##########")
Compile.write_shapes(
bounds=lbsn_data.bounds,
shapes_and_meta_list=shapes_and_meta_list)
else:
print(f'\nUser abort.')
if cfg.cluster_locations and user_intf.abort is False:
log.info(
"\n########## "
"STEP 6 of 6: Calculating Overall Location Clusters "
"##########")
shapes_and_meta_list.clear()
for clusterer in clusterer_list:
if clusterer.cls_type == LOCATIONS:
clusterer.get_overall_clusters()
cluster_shapes = clusterer.get_cluster_centroids()
shapes_and_meta_list.append(cluster_shapes)
Compile.write_shapes(
bounds=lbsn_data.bounds,
shapes_and_meta_list=shapes_and_meta_list)
# time reporting
later = time.time()
hours, rem = divmod(later-now, 3600)
minutes, seconds = divmod(rem, 60)
# difference = int(later - now)
log.info(f'\nDone.\n{int(hours):0>2} Hours '
f'{int(minutes):0>2} Minutes and '
f'{seconds:05.2f} Seconds passed.')
input("Press any key to exit...")
sys.exit()
if __name__ == "__main__":
main()