-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburdenTableWriter.py
executable file
·163 lines (142 loc) · 5.52 KB
/
burdenTableWriter.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
import tempfile
import json
import pandas as pd
import numpy as np
from . import QgsSBCalcDataBridge
from . import SBCalculator
class burdenTableWriter:
"""
Uses the results of the calculations and produces pandas dataframes
or csv tables of the results in a standard format.
"""
def __init__(
self,
dataBridge: QgsSBCalcDataBridge.QgsSBCalcDataBridge,
SBC: SBCalculator.SBCalculator,
):
self._dataBridge = dataBridge
self._SBCalculator = SBC
def generatePerAreaTable(self):
"""
Helper function for munging the layers - given the appropriate
arrays, creates a table (as pandas dataframe) that contains that information
in the way that we want.
Relies on having previous calculations and other fields available
in the SBCalculator AND dataBridge objects.
"""
ret = pd.DataFrame(
self._SBCalculator.getBurdenArray(),
columns=self._dataBridge.getServiceNames(),
)
ret.insert(ret.shape[1], "total", self._SBCalculator.getPerCapitaTotalBurden())
ret.insert(
ret.shape[1],
"W_total",
self._SBCalculator.getPerCapitaWeightedTotalBurden(),
)
if (
self._dataBridge.getPopulationHasCentroids()
): # if the centroids have been specified
ret.insert(
0,
self._dataBridge.getPopulationLatField(),
self._dataBridge.getPopulationLatitudes(),
)
ret.insert(
1,
self._dataBridge.getPopulationLongField(),
self._dataBridge.getPopulationLongitudes(),
)
else: # put in the ones that were actually used
ret.insert(
0, "centroid_latitudes", self._dataBridge.getPopulationLatitudes()
)
ret.insert(
1, "centroid_longitudes", self._dataBridge.getPopulationLongitudes()
)
# insert the indices
ret.insert(
0,
self._dataBridge.getPopulationIndexField(),
self._dataBridge.getPopulationDataByFieldName(
self._dataBridge.getPopulationIndexField(), expected_type=str
),
)
return ret
def generateTotalsTable(self):
"""
Helper function for munging the layers - creates the table (as pandas dataframe) with the
total-area information.
The desired items in this table are:
- two rows, one for population-weighted aggregates and
one that aggregates per-capita values
- the columns are:
- labels for the rows
- each of the services
- population (NULL and total population are the values)
- total (total per-capita burden and population-weighted total burden, respectively)
"""
idxes = ["total per-capita", "total population-weighted"]
ret = pd.DataFrame(
(
self._SBCalculator.getPerCapitaAggregatedBurdenArray(),
self._SBCalculator.getAggregatedWeightedBurden(),
),
columns=self._dataBridge.getServiceNames(),
)
ret.insert(0, "Agg_type", idxes)
ret.insert(
1, "population", [pd.NA, self._dataBridge.getPopulationTotalPopulation()]
)
ret.insert(
ret.shape[1],
"total",
(
self._SBCalculator.getPerCapitaAggregatedTotalBurden(),
self._SBCalculator.getAggregatedWeightedTotalBurden(),
),
)
return ret
def exportTableAsTempFile(self, table: pd.DataFrame):
"""
Exports as CSV to temporary file.
"""
tf = tempfile.NamedTemporaryFile(mode="w+", encoding="utf8", delete=False)
tf.close()
table.to_csv(tf.name, index=False)
return tf
def exportTable(self, table: pd.DataFrame, path: str):
"""
Exports as CSV to specified path.
"""
table.to_csv(path, index=False)
def exportPerCapitaPerFacilityPerServiceBenefits(self,
arr: np.array,
pop_indices: list,
facility_indices:list,
service_indices: list,
tablepath:str,
indexpath:str
):
"""
This is experimental code. For certain applications, researchers may want to know
the burden reduction associated with each facility. The table is an
interim calculation of /benefits/ and can be processed in some other
script to provide those figures. These are NOT burden values.
Because the table itself is 3-dimensional in numpy, we'll use the built-in
numpy.save function. It will be saved to the same folder as the csv exports,
which are mandatory if using this function.
Facility ordering, services, and so on, will be written to a separate file
in that same folder as json.
"""
with open(tablepath, 'wb') as f:
np.save(tablepath, arr)
with open(indexpath, 'w') as f:
json.dump(
{
'population indices': pop_indices,
'facility indices': facility_indices,
'service indices': service_indices
},
f
)