This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
executable file
·186 lines (174 loc) · 9.65 KB
/
app.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
#!/usr/bin/python3
from __future__ import annotations
from re import compile as reg_compile
from os.path import basename, join
from os import listdir
from functools import reduce
from itertools import chain
try:
from model.corporateStat import CompaniesUnderState
from model.post import PostOfficeGraph
from util import *
from utilMultiState import *
from districts import plotDistributionOverDistricts
except ImportError as e:
print('[!]Module Unavailable: {}'.format(str(e)))
exit(1)
'''
Reads from each CSV datafiles present in `./data/`,
holding Ministry of Corporate Affair's ( M.C.A. ) Company Data
for a certain state & generates 6 plots for each of them.
5 of them will be PIE charts & 1 will be simple plotting
of registration year vs. #-of companies registered in that year.
'''
def main(targetPath='./data/') -> float:
'''
Given a CSV data file name, it'll simply extract
State name using Regular Expression(s)
In case of error, returns None
'''
def __extract_state__(fromIt: str) -> str:
match_obj = reg_compile(r'^mca_(\w+)_[0-9]{8,}\.csv$').match(fromIt)
return match_obj.group(1).capitalize() if match_obj else None
'''
Given a iterable of boolean values, it'll return a 2-element tuple,
where first one will hold number of Truth values & last one will keep
total # of values we're considering
'''
def __calculateSuccess__(result: List[bool]) -> float:
'''
A utility function to calculate division of 2-element tuple
'''
def __divide__(a: int, b: int) -> float:
return 0 if not b else a/b
return __divide__(
*reduce(
lambda acc, cur: (acc[0] + 1, acc[1] +
1) if cur else (acc[0], acc[1]+1),
result, (0, 0)))
def __getAllPossibleCompanyStatus__(companyDataSet):
return reduce(lambda acc, cur: acc.union(set(
companyDataSet[cur].keys())), companyDataSet, set())
try:
'''
allCompanyStatus = dict(map(lambda v:
(__extract_state__(basename(v)), categorizeAsPerCompanyStatus(CompaniesUnderState.importFromCSV(
__extract_state__(basename(v)), targetPath=v).companies)),
map(lambda v: join(targetPath, v),
filter(lambda v: v.startswith('mca') and v.endswith('csv'), listdir(targetPath)))))
reduce(lambda acc, cur: [
plotCategorizedCompanyDataForACertainState(categorizeAsPerCompanyStatus(
CompaniesUnderState.importFromCSV(
__extract_state__(basename(cur)), targetPath=cur).companies), './plots/{}_company_status.png'.format(basename(cur)[:-4]), 'Status of Companies in {}'.format(__extract_state__(basename(cur)))),
plotCategorizedCompanyDataForACertainState(categorizeAsPerCompanyClass(
CompaniesUnderState.importFromCSV(
__extract_state__(basename(cur)), targetPath=cur).companies), './plots/{}_company_class.png'.format(basename(cur)[:-4]), 'Class of Companies in {}'.format(__extract_state__(basename(cur)))),
plotCategorizedCompanyDataForACertainState(categorizeAsPerCompanyCategory(
CompaniesUnderState.importFromCSV(
__extract_state__(basename(cur)), targetPath=cur).companies), './plots/{}_company_category.png'.format(basename(cur)[:-4]), 'Category of Companies in {}'.format(__extract_state__(basename(cur)))),
plotCategorizedCompanyDataForACertainState(categorizeAsPerCompanySubCategory(
CompaniesUnderState.importFromCSV(
__extract_state__(basename(cur)), targetPath=cur).companies), './plots/{}_company_subCategory.png'.format(basename(cur)[:-4]), 'SubCategory of Companies in {}'.format(__extract_state__(basename(cur)))),
plotCategorizedCompanyDataForACertainState(categorizeAsPerCompanyPrincipalBusinessActivity(
CompaniesUnderState.importFromCSV(
__extract_state__(basename(cur)), targetPath=cur).companies), './plots/{}_company_principalBusinessActivity.png'.format(basename(cur)[:-4]), 'Principal Business Activity of Companies in {}'.format(__extract_state__(basename(cur)))),
plotCompanyRegistrationDateWiseCategorizedData(categorizeAsPerCompanyDateOfRegistration(
CompaniesUnderState.importFromCSV(
__extract_state__(basename(cur)), targetPath=cur).companies), './plots/{}_company_dateOfRegistration.png'.format(basename(cur)[:-4]), 'Registration of Companies in {}'.format(__extract_state__(basename(cur))))
] + acc,
map(lambda v: join(targetPath, v),
filter(lambda v: v.startswith('mca') and v.endswith('csv'), listdir(targetPath))), [])
+
list(map(lambda v: plotAllCompaniesByStateUsingStatus(
allCompanyStatus, v,
'./allCompanyStatusPlots/mca_all_{}_companies.png'.format(v.replace(' ', '_').lower())),
__getAllPossibleCompanyStatus__(allCompanyStatus)))
+
[plotTopEmailProvidersShare(
*extractAllCompanyEmailProvider(
map(
lambda v: CompaniesUnderState.importFromCSV(
__extract_state__(v),
targetPath=join(targetPath, v)).companies,
filter(
lambda v: v.startswith('mca') and v.endswith('csv'), listdir(targetPath)))),
'Email Service used by Companies in India',
'./plots/mca_email_service_used_by_companies.png')]
+
[
plotAllRoCStatistics(
extractRoCStatForAllCompanies(
map(
lambda v: CompaniesUnderState.importFromCSV(
__extract_state__(v),
targetPath=join(targetPath, v)).companies,
filter(
lambda v: v.startswith('mca') and v.endswith('csv'), listdir(targetPath)))),
'./plots/mca_company_registration_under_roc.png'
)
]
+
[
plotCompanyRegistrationDateWiseCategorizedData(
categorizeAsPerCompanyDateOfRegistration(
chain(
*[CompaniesUnderState.importFromCSV(
__extract_state__(i),
targetPath=join(targetPath, i)).companies for i in filter(
lambda v: v.startswith('mca') and v.endswith('csv'), listdir(targetPath))]
)),
'./plots/registration_of_companies_around_years_all_india.png',
'Rate of Registration of Companies around the Years'
)
]
+
[plotDistributionOverDistricts(i,
'Distribution of Companies over Districts in {}, India'.format(
i.name),
'plots/distributionOfCompaniesOverDistrictsIn{}.jpg'.format(
'_'.join(i.name.lower().split())))
for i in pincodeToDistrictNameMapper(
classifyCompaniesUsingPinCodeOfRegisteredAddress(
chain(
*[CompaniesUnderState.importFromCSV(
__extract_state__(i),
targetPath=join(targetPath, i)).companies for i in filter(
lambda v: v.startswith('mca') and v.endswith('csv'), listdir(targetPath))]
)
),
PostOfficeGraph.importFromCSV(
'./data/all_india_PO_list_without_APS_offices_ver2_lat_long.csv')
)]
+
'''
return __calculateSuccess__(
# plots distribution of active companies across different districts of India
[plotDistributionOverDistricts(i,
'Distribution of Active Companies over Districts in {}, India'.format(
i.name),
'plots/distributionOfActiveCompaniesOverDistrictsIn{}.jpg'.format(
'_'.join(i.name.lower().split())))
for i in pincodeToDistrictNameMapper(
classifyCompaniesUsingPinCodeOfRegisteredAddress(
# filtering out only ACTIVE companies
filter(lambda e: e.status.strip().lower() == 'active', chain(
*[CompaniesUnderState.importFromCSV(
__extract_state__(i),
targetPath=join(targetPath, i)).companies for i in filter(
lambda v: v.startswith('mca') and v.endswith('csv'), listdir(targetPath))]
))
),
PostOfficeGraph.importFromCSV(
'./data/all_india_PO_list_without_APS_offices_ver2_lat_long.csv')
)]
)
except Exception as e:
print('[!]Error : {}'.format(e))
return 0.0
if __name__ == '__main__':
try:
print('Success: {} %'.format(main()*100))
except KeyboardInterrupt:
print('\n[!]Terminated')
finally:
exit(0)