forked from PascalLesage/database_wide_monte_carlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_generation.py
346 lines (291 loc) · 13.3 KB
/
sample_generation.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
""" Generation of samples for all activities in an LCI database
Built on the Brightway2 framework.
Stores each result (supply array, inventory vector, sampled matrices)
for each activity and each iteration is stored as an individual file.
These must then be assembled to be useful.
"""
from brightway2 import *
import numpy as np
import os
import click
import datetime
import multiprocessing as mp
import pickle
import sys
import json
from water_balancing_data import get_water_balancing_data
from water_balancing import balance_water_exchanges
from land_use_balancing_data import get_land_use_balancing_data
from land_use_balancing import balance_land_use_exchanges
__author__ = "Pascal Lesage"
__credits__ = ["Pascal Lesage, Chris Mutel, Nolwenn Kazoum"]
__license__ = "BSD 3-Clause 'New' or 'Revised' License"
__version__ = "1.1"
__maintainer__ = "Pascal Lesage"
__email__ = "[email protected]"
class direct_solving_MC(MonteCarloLCA, DirectSolvingMixin):
"""Class expanding MonteCarloLCA to include `solve_linear_system`."""
pass
def correlated_MCs_worker(project_name,
job_dir,
job_id,
worker_id,
functional_units_list,
iterations,
include_inventory,
include_supply,
include_matrices,
balance_water,
balance_land_use
):
"""Generate database-wide correlated Monte Carlo samples
This function is a worker function. It is called from
the `generate_samples` function, that dispatches the Monte Carlo
work to a specified number of workers.
"""
# Open the project containing the target database
projects.set_current(project_name)
# Create a factice functional unit that spans all possible demands
# Useful if some activities link to other upstream databases
collector_functional_unit = {k: v
for d in functional_units_list
for k, v in d.items()
}
# Create an LCA object that spans all demands
lca = direct_solving_MC(demand=collector_functional_unit)
# Build technosphere and biosphere matrices and corresponding rng
lca.load_data()
for index in range(iterations):
# Make directories for current iteration
it_nb_worker_id = "iteration_{}-{}".format(worker_id, index)
index_dir = os.path.join(job_dir, it_nb_worker_id)
os.mkdir(index_dir)
# Sample new values for technosphere and biosphere matrices
lca.rebuild_technosphere_matrix(lca.tech_rng.next())
lca.rebuild_biosphere_matrix(lca.bio_rng.next())
if balance_water:
lca = balance_water_exchanges(lca, os.path.join(job_dir, 'common_files'))
if balance_land_use:
lca = balance_land_use_exchanges(lca, os.path.join(job_dir, 'common_files'))
if include_matrices:
matrices_dir = os.path.join(index_dir,'Matrices')
os.mkdir(matrices_dir)
np.save(
os.path.join(matrices_dir, "A_matrix"),
lca.technosphere_matrix.tocoo().data.astype(np.float32)
)
np.save(
os.path.join(matrices_dir, "B_matrix"),
lca.biosphere_matrix.tocoo().data.astype(np.float32)
)
if any([include_inventory, include_supply]):
# Factorize technosphere matrix, creating a solver
lca.decompose_technosphere()
# For all activities, calculate and save
# supply and inventory vectors
for fu in functional_units_list:
actKey = str(list(fu.keys())[0][1])
lca.build_demand_array(fu)
lca.supply_array = lca.solve_linear_system()
# Supply arrays
if include_supply:
supply_dir = os.path.join(index_dir,'Supply')
if not os.path.isdir(supply_dir):
os.makedirs(supply_dir)
np.save(
os.path.join(supply_dir, actKey),
np.array(lca.supply_array, dtype = np.float32)
)
# Inventory
if include_inventory:
inventory_dir = os.path.join(index_dir,'Inventory')
if not os.path.isdir(inventory_dir):
os.makedirs(inventory_dir)
lca.inventory = lca.biosphere_matrix * lca.supply_array
np.save(
os.path.join(inventory_dir, actKey),
np.array(lca.inventory, dtype = np.float32)
)
print(
"Worker {} finished {} iterations".format(
worker_id,
iterations
)
)
def get_useful_info(collector_functional_unit, job_dir, activities, database_name, project_name, balance_water, balance_land_use):
"""Collect and save job-level data"""
# Generate sacrificial LCA whose attributes will be saved
sacrificial_lca = LCA(collector_functional_unit)
sacrificial_lca.lci()
# Make folder to contain extracted information
common_dir = os.path.join(job_dir, 'common_files')
if not os.path.isdir(common_dir):
os.makedirs(common_dir)
# Save various attributes for eventual reuse in interpretation
file = os.path.join(common_dir, 'activity_UUIDs.json')
with open(file, "w") as f:
json.dump(activities, f, indent=4)
fp = os.path.join(common_dir, 'product_dict.pickle')
with open(fp, "wb") as f:
pickle.dump(sacrificial_lca.product_dict, f)
fp = os.path.join(common_dir, 'bio_dict.pickle')
with open(fp, "wb") as f:
pickle.dump(sacrificial_lca.biosphere_dict, f)
fp = os.path.join(common_dir, 'activity_dict.pickle')
with open(fp, "wb") as f:
pickle.dump(sacrificial_lca.activity_dict, f)
fp = os.path.join(common_dir, 'tech_params.pickle')
with open(fp, "wb") as f:
pickle.dump(sacrificial_lca.tech_params, f)
fp = os.path.join(common_dir, 'bio_params.pickle')
with open(fp, "wb") as f:
pickle.dump(sacrificial_lca.bio_params, f)
fp = os.path.join(common_dir, 'IO_Mapping.pickle')
with open(fp, "wb") as f:
pickle.dump({v:k for k,v in mapping.items()}, f)
fp = os.path.join(common_dir, 'tech_row_indices')
np.save(fp, sacrificial_lca.technosphere_matrix.tocoo().row)
fp = os.path.join(common_dir, 'tech_col_indices')
np.save(fp, sacrificial_lca.technosphere_matrix.tocoo().col)
fp = os.path.join(common_dir, 'bio_row_indices')
np.save(fp, sacrificial_lca.biosphere_matrix.tocoo().row)
fp = os.path.join(common_dir, 'bio_col_indices')
np.save(fp, sacrificial_lca.biosphere_matrix.tocoo().col)
if balance_water:
get_water_balancing_data(job_dir, activities, database_name, project_name, sacrificial_lca)
if balance_land_use:
get_land_use_balancing_data(job_dir, activities, database_name, project_name, sacrificial_lca)
return None
@click.command()
@click.option('--project_name', default='default', help='Brightway2 project name', type=str)
@click.option('--database_name', help='Database name', type=str)
@click.option('--iterations', default=1000, help='Number of Monte Carlo iterations', type=int)
@click.option('--cpus', default=mp.cpu_count(), help='Number of used CPU cores', type=int)
@click.option('--base_dir', help='Base directory path for precalculated samples', type=str)
@click.option('--include_inventory', help='Save inventory vector', default=True, type=bool)
@click.option('--include_supply', help='Save supply vector', default=False, type=bool)
@click.option('--include_matrices', help='Save A and B matrices', default=False, type=bool)
@click.option('--balance_water', help='Balance water exchanges', default=False, type=bool)
@click.option('--balance_land_use', help='Balance land use exchanges', default=False, type=bool)
def generate_samples_job(project_name, database_name, iterations,
cpus, base_dir,
include_inventory=False, include_supply=False,
include_matrices=False, balance_water=False, balance_land_use=False):
"""Parent function for database-wide sample generation
Arguments:
project_name -- Brightway2 project where the database is saved (str)
database_name -- Database name (str)
iterations -- Number of Monte Carlo iterations required
cpus -- Number of cpus over which the work is to be distributed
base_dir -- Root directory for all presampling files
include_supply -- If True, save the supply vector. Careful: supply vectors take lots of memory.
include_matrices -- If True, save A and B matrices
balance_water -- If True, balance water exchanges
balance_land_use -- If True, balance land use exchanges
Does not return anything, but saves files in a "job" folder.
Note: The use of @click allows the function arguments to be passed
from a command line, but imposes project and database names with no
white spaces.
"""
if not any([include_inventory, include_supply, include_matrices]):
print("No output requested. At least one of the following must be true:")
print("include_inventory, include_supply or include_matrices")
sys.exit(0)
# Open the Brighway2 project
assert project_name in projects, "The requested project does not exist"
projects.set_current(project_name)
# Create a unique job name
now = datetime.datetime.now()
try: # Works with Linux
job_id = "{}_{}-{}-{}_{}h{}".format(
os.environ['USER'],
now.year,
now.month,
now.day,
now.hour,
now.minute
)
except:
try: #Works with Windows
job_id = "{}_{}-{}-{}_{}h{}".format(
os.environ['COMPUTERNAME'],
now.year,
now.month,
now.day,
now.hour,
now.minute
)
except:
job_id = "{}_{}-{}-{}_{}h{}".format(
'job',
now.year,
now.month,
now.day,
now.hour,
now.minute
)
# Identify all activities for which samples are required
db = Database(database_name)
activities = [activity.key[1] for activity in db]
# Specify all functional units
functional_units = [{(database_name, act): 1} for act in activities]
# Make directory to store results
samples_dir = os.path.join(base_dir, database_name, 'jobs')
if not os.path.isdir(samples_dir):
os.makedirs(samples_dir)
job_dir = os.path.join(samples_dir, job_id)
os.makedirs(job_dir)
# Generate and save job-level information
collector_functional_unit = {k:v for d in functional_units for k, v in d.items()}
get_useful_info(collector_functional_unit, job_dir, activities, database_name, project_name, balance_water, balance_land_use)
# Calculate number of iterations per worker.
it_per_worker = [iterations//cpus for _ in range(cpus)]
for _ in range(iterations-cpus*(iterations//cpus)):
it_per_worker[_]+=1
# Dispatch actual sampling work to workers
workers = []
for worker_id in range(cpus):
child = mp.Process(target=correlated_MCs_worker,
args=(
project_name,
job_dir,
job_id,
worker_id,
functional_units,
it_per_worker[worker_id],
include_inventory,
include_supply,include_matrices,
balance_water,
balance_land_use
)
)
workers.append(child)
child.start()
for c in workers:
c.join()
now = datetime.datetime.now()
log = {'samples_generated':
{
'included_elements':
{
'Matrices':include_matrices*1,
'Inventory': include_inventory*1,
'Supply': include_supply*1
},
'completed':
"{}-{}-{}_{}h{}".format(
now.year,
now.month,
now.day,
now.hour,
now.minute)
}
}
with open(os.path.join(job_dir, 'log.json'), 'w') as f:
json.dump(log, f, indent=4)
print("{} samples generated for {} activities, saved to directory {}.".format(iterations, len(activities), job_dir))
print("Use `clean_jobs.py` to sanitize the data, and then `concatenate_within_jobs.py` to consolidate samples")
print("See log file for more information")
if __name__ == '__main__':
__spec__ = None
generate_samples_job()