test_integration_3inversions.py 18.6 KB
Newer Older
1
2
3
import os
import time
import shutil
4
5
6
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
Antoine Berchet's avatar
Antoine Berchet committed
7
from pathlib import Path
8
9
10
11
12
13
14
15
16
17
18
19
20

import pytest

from pycif.utils.classes.setup import Setup
from pycif.utils.datastores.dump import read_datastore
from pycif.utils.yml import ordered_dump
from pycif.utils.path import init_dir


@pytest.mark.dummy
@pytest.mark.article
@pytest.mark.parametrize(
    "settings", [
Antoine Berchet's avatar
Antoine Berchet committed
21
        {"mode": "4dvar", "minimizer": "M1QN3"},
22
23
24
25
26
27
28
        # pytest.param({"mode": "4dvar", "minimizer": "M1QN3", "montecarlo": 10},
        #              marks=pytest.mark.uncertainties),
        # {"mode": "4dvar", "minimizer": "congrad"},
        # {"mode": "ensrf"},
        # pytest.param({"mode": "ensrf", "nsample": 5},
        #              marks=pytest.mark.uncertainties),
        # {"mode": "analytical"}
29
    ]
30
)
Antoine Berchet's avatar
Antoine Berchet committed
31
def test_integration_inversion(dummy_config_inversion, settings, pytestconfig):
32
33
34
35
36
37
    """
    Integration test that runs the dummy_forward model.
    """

    tmpdir, config, tag = dummy_config_inversion
    
Antoine Berchet's avatar
Antoine Berchet committed
38
39
40
41
    # Force reloading observation operator
    # from forward to make computation faster
    config["model"]["reload_H"] = "{}/../H_matrix.pickle".format(tmpdir)
    
Antoine Berchet's avatar
Antoine Berchet committed
42
43
    # Update number of simulations depending on pytests options
    marker = pytestconfig.getoption('-m')
44
    nsimmax = 10
Antoine Berchet's avatar
Antoine Berchet committed
45
46
47
48
49
50
51
52
    montecarlo = settings.get("montecarlo", 10)
    nsample = settings.get("nsample", 5)
    if "allsimulations" not in marker:
        nsimmax = 4
        montecarlo = 4
        nsample = 1
        
    # Changing mode
53
    if config["datavect"]["components"]["flux"]\
54
            ["parameters"]["CH4"]["hresol"] == "hpixels":
Antoine Berchet's avatar
Antoine Berchet committed
55
        nsimmax = int(2.5 * nsimmax)
56
    
57
    elif config["datavect"]["components"]["flux"]\
58
            ["parameters"]["CH4"]["hresol"] == "global" \
59
            and settings.get("minimizer", "") == "congrad":
60
61
62
63
64
65
66
67
68
69
70
71
72
        nsimmax = 1
        
    if settings["mode"] == "4dvar":
        mode = {
            "plugin": {"name": "4dvar", "version": "std"},
            "minimizer": {
                "plugin": {"name": settings["minimizer"], "version": "std"},
                "simulator": {
                    "plugin": {"name": "gausscost", "version": "std"},
                    "reload_from_previous": True
                },
                "maxiter": nsimmax,
                "nsim": nsimmax,
73
74
                "epsg": 0.0002,
                "df1": 0.5
75
76
77
            },
            "save_out_netcdf": True
        }
78
79
80
81
        if settings["minimizer"] == "congrad":
            mode["minimizer"]["save_uncertainties"] = True
        
        if "montecarlo" in settings:
82
83
84
85
86
            mode["montecarlo"] = {
                "nsample": settings["montecarlo"],
                "perturb_x": True,
                "perturb_y": False
            }
87
        
88
89
90
91
92
93
94
95
96
97
        prior_dir = os.path.join(tmpdir, "obsoperator/fwd_-001/obsvect")
        posterior_dir = os.path.join(tmpdir, "obsoperator/fwd_-002/obsvect")
    
    elif settings["mode"] == "analytical":
        mode = {"plugin": {"name": "analytic", "version": "std",
                           "dump_nc_base_control": True}}
        prior_dir = os.path.join(tmpdir, "obsvect_prior")
        posterior_dir = os.path.join(tmpdir, "obsvect_posterior")
    
    elif settings["mode"] == "ensrf":
Antoine Berchet's avatar
Antoine Berchet committed
98
        nsimmax = 2 * settings.get("nsample", 1) * nsimmax
99
100
101
102
103
104
        mode = {"plugin": {"name": "EnSRF", "version": "std"},
                "nsample": nsimmax}
        prior_dir = os.path.join(tmpdir, "obsvect_prior")
        posterior_dir = os.path.join(tmpdir, "obsvect_posterior")
      
    config["mode"] = mode
105
106
107
108
109
110
    config["platform"] = {
        "plugin":
            {"name": "LSCE", "version": "obelix"}
            if os.getenv("PYCIF_PLATFORM") == "LSCE"
            else {"name": "docker", "version": "cif"}
    }
Antoine Berchet's avatar
Antoine Berchet committed
111
112
113

    # Save ID for later plot of cost function
    resolution = \
114
        config["datavect"]["components"]["flux"]["parameters"]["CH4"][
Antoine Berchet's avatar
Antoine Berchet committed
115
116
            "hresol"]
    sigma = config["datavect"]["components"]\
117
                ["flux"]["parameters"]["CH4"]["hcorrelations"]["sigma"]
Antoine Berchet's avatar
Antoine Berchet committed
118
119
120
121
122
    correlations = "lowcorr" if sigma == 500 else "highcorr"
    test_id = (settings["mode"], settings.get("minimizer", ""),
               resolution, correlations)

    pytest.test_ids[test_id] = (tmpdir, nsimmax)
123
124
125
    
    # Removing MCF to make computation quicker
    del config["model"]["chemistry"]["acspecies"]["MCF"]
126
    del config["datavect"]["components"]["flux"]["parameters"]["MCF"]
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    del config["datavect"]["components"]["concs"]["parameters"]["MCF"]
    
    # Dump yml config file
    dummy_config_file = os.path.join(tmpdir, "dummy_config.yml")
    with open(dummy_config_file, "w") as outfile:
        ordered_dump(outfile, config)
    
    # Run as job
    setup = Setup.yaml_to_setup(dummy_config_file)
    setup.todo_init = ["platform"]
    setup = Setup.load_config(setup)
    platform = setup.platform

    job_file = os.path.join(tmpdir,
                            "job_pycif_{}".format(tmpdir[-5:]))

    exe = "{} -m pycif {}".format(platform.python, dummy_config_file)

    job_id = platform.submit_job(
        exe,
        job_file
    )
Antoine Berchet's avatar
Antoine Berchet committed
149
    
150
151
152
    # Wait for the end of the execution
    while not platform.check_jobs([job_id]):
        time.sleep(platform.sleep_time)
Antoine Berchet's avatar
Antoine Berchet committed
153
    
154
155
156
157
158
159
160
161
162
    # Get results and check that posterior closer to prior
    components = os.listdir(prior_dir)
    for comp in components:
        comp_dir_prior = os.path.join(prior_dir, comp)
        comp_dir_post = os.path.join(posterior_dir, comp)
        parameters = os.listdir(comp_dir_prior)
        for param in parameters:
            param_dir = os.path.join(comp_dir_prior, param)
            monitor_prior = \
Antoine Berchet's avatar
Antoine Berchet committed
163
                read_datastore(os.path.join(param_dir, "monitor.nc"))["maindata"]
164
165
166

            param_dir = os.path.join(comp_dir_post, param)
            monitor_post = \
Antoine Berchet's avatar
Antoine Berchet committed
167
                read_datastore(os.path.join(param_dir, "monitor.nc"))["maindata"]
168
169
170
171
172
173
174
175
176
177
178
179
180
            assert (
                    (monitor_prior.loc[:, "obs"]
                     - monitor_prior.loc[:, "sim"]).pow(2).sum()
                    - (monitor_post.loc[:, "obs"]
                       - monitor_post.loc[:, "sim"]).pow(2).sum()
                    > 0
            )

    # Dump configuration into CIF examples
    tag += "_{}_{}".format(settings["mode"],
                           "" if settings["mode"] != "4dvar"
                           else settings["minimizer"])

Antoine Berchet's avatar
Antoine Berchet committed
181
    current_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
182
183
    root_dir = os.path.abspath(os.path.join(current_dir, "../../"))
    pytest_dir = os.path.abspath(tmpdir + "/../")
Antoine Berchet's avatar
Antoine Berchet committed
184
    example_dir = \
185
        os.path.abspath(os.path.join(root_dir, "examples_artifact/dummy/"))
Antoine Berchet's avatar
Antoine Berchet committed
186
    Path(example_dir).mkdir(parents=True, exist_ok=True)
187
188
    
    config["workdir"] = "{}/inversion_{}/".format(pytest_dir, tag)
189
190

    dummy_config_file = \
Antoine Berchet's avatar
Antoine Berchet committed
191
        os.path.join(example_dir, "config_inversion_{}.yml".format(tag))
192
    with open(dummy_config_file, "w") as outfile:
193
        ordered_dump(outfile, config,
194
195
                     ref_directories={"outdir": pytest_dir,
                                      "rootdir": root_dir},
196
                     replace_values={"rootdir": "/tmp/CIF/"})
197
    
Antoine Berchet's avatar
Antoine Berchet committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    # Loop with different number of simulations for non analytical inversions
    title = ""
    obs_root = "obsvect_posterior"
    control_root = "controlvect"
    if settings["mode"] != "analytical":
        for nsim in range(1, nsimmax + 1, 2):
            file_config = "{}/dummy_config.yml".format(tmpdir)
            inv_setup = Setup.from_yaml(file_config)
            
            if settings.get("minimizer", None) == "congrad":
                title = "CONGRAD"
                obs_root = "obsoperator/fwd_-002/obsvect"
                control_root = "obsoperator/fwd_-002/controlvect"
                inv_setup["mode"]["minimizer"]["maxiter"] = \
                    max(1, int(nsim))
Antoine Berchet's avatar
Antoine Berchet committed
213
                nsim *= 2
Antoine Berchet's avatar
Antoine Berchet committed
214
215
216
217
218
219
220
221
222
223
                if os.path.isdir("{}/obsoperator/fwd_-002".format(tmpdir)):
                    shutil.rmtree("{}/obsoperator/fwd_-002".format(tmpdir))
            elif settings.get("minimizer", None) == "M1QN3":
                title = "M1QN3"
                obs_root = "obsoperator/fwd_-002/obsvect"
                control_root = "obsoperator/fwd_-002/controlvect"
                inv_setup["mode"]["minimizer"]["maxiter"] = \
                    max(1, int(nsim))
                inv_setup["mode"]["minimizer"]["nsim"] = \
                    max(1, int(nsim))
Antoine Berchet's avatar
Antoine Berchet committed
224
                nsim *= 2
Antoine Berchet's avatar
Antoine Berchet committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
                if os.path.isdir("{}/obsoperator/fwd_-002".format(tmpdir)):
                    shutil.rmtree("{}/obsoperator/fwd_-002".format(tmpdir))
            elif settings["mode"] == "ensrf":
                title = "EnSRF"
                inv_setup["mode"]["nsample"] = nsim
            
            # Execute a partial inversion with less simulations
            inv_setup = Setup.from_dict(inv_setup, convert_none=True)
            inv_setup = inv_setup.load_config(inv_setup)
            controlvect, obsvect = inv_setup.mode.execute()
            
            # Compute the cost function
            departures = obsvect.ysim - obsvect.yobs
            j_o = 0.5 * (departures * obsvect.rinvprod(departures)).sum()
            
            dx = controlvect.x - controlvect.xb
            bfull = np.linalg.inv(controlvect.build_b(inv_setup.controlvect))
            j_b = dx[np.newaxis, :].dot(bfull.dot(dx[:, np.newaxis])).sum() / 2
            
            # Dump to text file for later plot
            with open("{}/varying_cost_function.txt".format(tmpdir), "a") as f:
                f.write("{} {} {}\n".format(nsim, j_o, j_b))
    
    else:
249
250
        title = "Analytical"
        
Antoine Berchet's avatar
Antoine Berchet committed
251
252
253
        # Load results
        file_config = "{}/dummy_config.yml".format(tmpdir)
        inv_setup = Setup.from_yaml(file_config)
254
255
        inv_setup = Setup.from_dict(inv_setup, convert_none=True)
        inv_setup = inv_setup.load_config(inv_setup)
256
257
258
259
260
261
262
263
264
265
266
        controlvect, obsvect = inv_setup.mode.execute()
        
        # Compute the cost function
        departures = obsvect.ysim - obsvect.yobs
        j_o = 0.5 * (departures * obsvect.rinvprod(departures)).sum()
        
        dx = controlvect.x - controlvect.xb
        bfull = np.linalg.inv(
            inv_setup.controlvect
                .build_b(inv_setup.controlvect)
        )
Antoine Berchet's avatar
Antoine Berchet committed
267
        j_b = dx[np.newaxis, :].dot(bfull.dot(dx[:, np.newaxis])).sum() / 2
268
269
270
        
        # Dump to text file for later plot
        with open("{}/varying_cost_function.txt".format(tmpdir), "a") as f:
Antoine Berchet's avatar
Antoine Berchet committed
271
272
            f.write("{} {} {}\n".format(0, j_o, j_b))
            f.write("{} {} {}\n".format(nsimmax, j_o, j_b))
273
    
Antoine Berchet's avatar
Antoine Berchet committed
274
275
276
277
278
279
280
    marker = pytestconfig.getoption('-m')
    if "article" in marker and "not article" not in marker:
        # Domain limits
        xmin = inv_setup.domain.xmin
        xmax = inv_setup.domain.xmax
        ymin = inv_setup.domain.ymin
        ymax = inv_setup.domain.ymax
281
    
Antoine Berchet's avatar
Antoine Berchet committed
282
283
        # Read observations
        file_obs = "{}/obsvect/concs/CH4/monitor.nc".format(tmpdir)
284
        monitor_ref = read_datastore(file_obs)["metadata"]
Antoine Berchet's avatar
Antoine Berchet committed
285
286
287
        coords = monitor_ref.loc[:, ["lon", "lat", "alt"]].drop_duplicates()
        
        # Compute fluxes from control vector
288
289
        file_flx = "{}/{}/flux/" \
                   "controlvect_flux_CH4.nc".format(tmpdir, control_root)
Antoine Berchet's avatar
Antoine Berchet committed
290
291
        ds = xr.open_dataset(file_flx)
        dflx = ds["x_phys"].mean(axis=(0, 1)) - ds["xb_phys"].mean(axis=(0, 1))
292
        dx = ds["x"].mean(axis=(0, 1)) - ds["xb"].mean(axis=(0, 1))
Antoine Berchet's avatar
Antoine Berchet committed
293
294
295
        
        # Fetch resolution for figure name
        resol = ""
296
        if config["datavect"]["components"][
297
                "flux"]["parameters"]["CH4"]["hresol"] == "hpixels":
Antoine Berchet's avatar
Antoine Berchet committed
298
            if config["datavect"]["components"][
299
                    "flux"]["parameters"]["CH4"][
Antoine Berchet's avatar
Antoine Berchet committed
300
                    "hcorrelations"]["sigma"] == 500:
Antoine Berchet's avatar
Antoine Berchet committed
301
302
303
                resol = "lowcorr"
            else:
                resol = "highcorr"
304
    
305
        elif config["datavect"]["components"]["flux"] \
Antoine Berchet's avatar
Antoine Berchet committed
306
307
                ["parameters"]["CH4"]["hresol"] == "ibands":
            resol = "bands"
308
    
309
        elif config["datavect"]["components"]["flux"] \
Antoine Berchet's avatar
Antoine Berchet committed
310
311
312
                ["parameters"]["CH4"]["hresol"] == "global":
            resol = "global"
        
313
        # Plot the figure of physical fluxes increments
Antoine Berchet's avatar
Antoine Berchet committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
        plt.figure(figsize=(21, 11))
        
        ax0 = plt.axes([0.05, 0.05, 0.73, 0.87])
        im = plt.imshow(dflx, extent=(xmin, xmax, ymin, ymax), cmap="YlOrRd",
                        vmin=-0.2, vmax=0.5)
        sc = plt.scatter(coords["lon"], coords["lat"], c=coords["alt"],
                         cmap="Blues", linewidths=1, edgecolors="k", s=600)
        plt.yticks(fontsize=25)
        plt.xticks(fontsize=25)
        
        ax1 = plt.axes([0.74, 0.05, 0.05, 0.87])
        cb1 = plt.colorbar(im, cax=ax1)
        plt.yticks(fontsize=25)
        plt.ylabel("Fluxes (a.u.)", fontsize=30)
        
        ax2 = plt.axes([0.86, 0.05, 0.05, 0.87])
        cb2 = plt.colorbar(sc, cax=ax2)
        plt.yticks(fontsize=25)
        plt.ylabel("Station altitude (m a.g.l)", fontsize=30)
        
        ax0.set_title(title, fontsize=45)
Antoine Berchet's avatar
Antoine Berchet committed
335
336
337
338
339
340
341
        
        current_dir = os.path.abspath(
            os.path.dirname(os.path.realpath(__file__)))
        figure_dir = \
            os.path.abspath(os.path.join(current_dir,
                                         "../../figures_artifact/"))
        Path(figure_dir).mkdir(parents=True, exist_ok=True)
342
343
        plt.savefig("{}/map_dx_{}_{}_{}.pdf".format(
            figure_dir, title, resol, nsimmax))
Antoine Berchet's avatar
Antoine Berchet committed
344
        plt.close()
345
        
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
        # Plot the figure of scalar fluxes increments
        plt.figure(figsize=(21, 11))
        
        ax0 = plt.axes([0.05, 0.05, 0.73, 0.87])
        im = plt.imshow(dx, extent=(xmin, xmax, ymin, ymax), cmap="YlOrRd")
        sc = plt.scatter(coords["lon"], coords["lat"], c=coords["alt"],
                         cmap="Blues", linewidths=1, edgecolors="k", s=600)
        plt.yticks(fontsize=25)
        plt.xticks(fontsize=25)
        
        ax1 = plt.axes([0.74, 0.05, 0.05, 0.87])
        cb1 = plt.colorbar(im, cax=ax1)
        plt.yticks(fontsize=25)
        plt.ylabel("Fluxes (a.u.)", fontsize=30)
        
        ax2 = plt.axes([0.86, 0.05, 0.05, 0.87])
        cb2 = plt.colorbar(sc, cax=ax2)
        plt.yticks(fontsize=25)
        plt.ylabel("Station altitude (m a.g.l)", fontsize=30)
        
        ax0.set_title(title, fontsize=45)
        
        current_dir = os.path.abspath(
            os.path.dirname(os.path.realpath(__file__)))
        figure_dir = \
            os.path.abspath(os.path.join(current_dir,
                                         "../../figures_artifact/"))
        Path(figure_dir).mkdir(parents=True, exist_ok=True)
        plt.savefig("{}/map_dx_scale_{}_{}_{}.pdf".format(
            figure_dir, title, resol, nsimmax))
        plt.close()
        
378
379
380
381
382
383
384
385
386
387
        # Plot uncertainty reduction
        if "pa_std" in ds:
            dstd = ds["b_std"].mean(axis=(0, 1)) \
                   - ds["pa_std"].mean(axis=(0, 1))
        
            # Plot the figure
            plt.figure(figsize=(21, 11))
            
            ax0 = plt.axes([0.05, 0.05, 0.73, 0.87])
            im = plt.imshow(dstd, extent=(xmin, xmax, ymin, ymax),
Antoine Berchet's avatar
Antoine Berchet committed
388
                            cmap="YlOrRd", vmin=0, vmax=1)
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
            sc = plt.scatter(coords["lon"], coords["lat"], c=coords["alt"],
                             cmap="Blues", linewidths=1, edgecolors="k", s=600)
            plt.yticks(fontsize=25)
            plt.xticks(fontsize=25)
            
            ax1 = plt.axes([0.74, 0.05, 0.05, 0.87])
            cb1 = plt.colorbar(im, cax=ax1)
            plt.yticks(fontsize=25)
            plt.ylabel("Uncertainty reduction (a.u.)", fontsize=30)
            
            ax2 = plt.axes([0.86, 0.05, 0.05, 0.87])
            cb2 = plt.colorbar(sc, cax=ax2)
            plt.yticks(fontsize=25)
            plt.ylabel("Station altitude (m a.g.l)", fontsize=30)
            
            ax0.set_title(title, fontsize=45)
            
            current_dir = os.path.abspath(
                os.path.dirname(os.path.realpath(__file__)))
            figure_dir = \
                os.path.abspath(os.path.join(current_dir,
                                             "../../figures_artifact/"))
            Path(figure_dir).mkdir(parents=True, exist_ok=True)
412
413
            plt.savefig("{}/map_dstd_{}_{}_{}.pdf"
                        .format(figure_dir, title, resol, nsimmax))
414
415
416
417
418
419
            plt.close()
        
        # Plot matrix of uncertainty reduction
        if hasattr(controlvect, "pa"):
            pa = controlvect.pa
            
Antoine Berchet's avatar
Antoine Berchet committed
420
421
422
423
            plt.figure(figsize=(21, 11))
            ax0 = plt.axes([0.05, 0.05, 0.73, 0.87])
            im = plt.imshow(pa[:int(controlvect.dim / 2),
                               :int(controlvect.dim / 2)],
424
                            vmin=-0.5, vmax=0.5, cmap="RdBu")
Antoine Berchet's avatar
Antoine Berchet committed
425
426
427
428
429
430
            plt.xticks(fontsize=20)
            plt.yticks(fontsize=20)
            
            ax1 = plt.axes([0.74, 0.05, 0.05, 0.87])
            cb1 = plt.colorbar(im, cax=ax1)
            plt.yticks(fontsize=25)
431
432
433
            plt.ylabel("Posterior uncertainties", fontsize=30)

            ax0.set_title(title, fontsize=45)
434
435
436
437
438
439
440
            
            current_dir = os.path.abspath(
                os.path.dirname(os.path.realpath(__file__)))
            figure_dir = \
                os.path.abspath(os.path.join(current_dir,
                                             "../../figures_artifact/"))
            Path(figure_dir).mkdir(parents=True, exist_ok=True)
441
            plt.savefig("{}/posterior_matrix_{}_{}_{}.pdf"
Antoine Berchet's avatar
Antoine Berchet committed
442
443
                        .format(figure_dir, title, resol, nsimmax),
                        bbox_inches='tight')
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
            plt.close()
            
            # Prior matrix
            bfull = controlvect.build_b(controlvect)
            plt.figure(figsize=(21, 11))
            ax0 = plt.axes([0.05, 0.05, 0.73, 0.87])
            im = plt.imshow(bfull[:int(controlvect.dim / 2),
                                  :int(controlvect.dim / 2)],
                            vmin=-0.5, vmax=0.5, cmap="RdBu")
            plt.xticks(fontsize=20)
            plt.yticks(fontsize=20)

            ax1 = plt.axes([0.74, 0.05, 0.05, 0.87])
            cb1 = plt.colorbar(im, cax=ax1)
            plt.yticks(fontsize=25)
459
            plt.ylabel("Prior uncertainties", fontsize=30)
460
461
462
463
464
465
466
467

            current_dir = os.path.abspath(
                os.path.dirname(os.path.realpath(__file__)))
            figure_dir = \
                os.path.abspath(os.path.join(current_dir,
                                             "../../figures_artifact/"))
            Path(figure_dir).mkdir(parents=True, exist_ok=True)
            plt.savefig("{}/prior_matrix_{}_{}_{}.pdf"
Antoine Berchet's avatar
Antoine Berchet committed
468
469
                        .format(figure_dir, title, resol, nsimmax),
                        bbox_inches='tight')
470
            plt.close()