Esempio n. 1
0
def test_completeOMIReproduction_coarsegrid(tmp_path):

    errors = []

    # Calculate EOFs
    raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
    shorter_olr = olr.restrict_time_coverage(raw_olr,
                                             np.datetime64('1979-01-01'),
                                             np.datetime64('2012-12-31'))
    coarse_lat = np.arange(-20., 20.1, 8.0)
    coarse_long = np.arange(0., 359.9, 20.0)
    interpolated_olr = olr.interpolate_spatial_grid(shorter_olr, coarse_lat,
                                                    coarse_long)
    eofs = omi.calc_eofs_from_olr(interpolated_olr,
                                  sign_doy1reference=True,
                                  interpolate_eofs=True,
                                  strict_leap_year_treatment=True)
    eofs.save_all_eofs_to_npzfile(
        tmp_path / "test_completeOMIReproduction_coarsegrid_EOFs.npz")

    # Validate EOFs against mjoindices own reference (results should be equal)
    mjoindices_reference_eofs = eof.restore_all_eofs_from_npzfile(
        mjoindices_reference_eofs_filename_coarsegrid)
    for idx, target_eof in enumerate(eofs.eof_list):
        if not mjoindices_reference_eofs.eof_list[idx].close(target_eof):
            errors.append(
                "mjoindices-reference-validation: EOF data at index %i is incorrect"
                % idx)

    # Calculate PCs
    raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
    pcs = omi.calculate_pcs_from_olr(raw_olr,
                                     eofs,
                                     np.datetime64("1979-01-01"),
                                     np.datetime64("2018-08-28"),
                                     use_quick_temporal_filter=False)
    pc_temp_file = tmp_path / "test_completeOMIReproduction_coarsegrid_PCs.txt"
    pcs.save_pcs_to_txt_file(pc_temp_file)

    # Validate PCs against mjoindices own reference (results should be equal)
    # Reload pcs instead of using the calculated ones, because the saving routine has truncated some decimals of the
    # reference values. So do the same with the testing target pcs.
    pcs = pc.load_pcs_from_txt_file(pc_temp_file)
    mjoindices_reference_pcs = pc.load_pcs_from_txt_file(
        mjoindices_reference_pcs_filename_coarsegrid)
    if not np.all(mjoindices_reference_pcs.time == pcs.time):
        errors.append(
            "mjoindices-reference-validation: Dates of PCs do not match.")
    if not np.allclose(mjoindices_reference_pcs.pc1, pcs.pc1):
        errors.append(
            "mjoindices-reference-validation: PC1 values do not match.")
    if not np.allclose(mjoindices_reference_pcs.pc2, pcs.pc2):
        errors.append(
            "mjoindices-reference-validation: PC2 values do not match.")

    assert not errors, "errors occurred:\n{}".format("\n".join(errors))
Esempio n. 2
0
def test_save_all_eofs_to_npzfile(tmp_path):
    filename = tmp_path / "test.npz"
    doys = tools.doy_list()
    lat = np.array([-10., 0., 10.])
    long = np.array([0., 5.])
    explained_variances = np.array([np.arange(1, doys.size + 1, 1) + 111,
                                    np.arange(1, doys.size + 1, 1) + 222,
                                    np.arange(1, doys.size + 1, 1) + 333,
                                    np.arange(1, doys.size + 1, 1) + 444,
                                    np.arange(1, doys.size + 1, 1) + 555,
                                    np.arange(1, doys.size + 1, 1) + 666])
    eigenvalues = np.array([np.arange(1, doys.size + 1, 1) + 1111,
                            np.arange(1, doys.size + 1, 1) + 2222,
                            np.arange(1, doys.size + 1, 1) + 3333,
                            np.arange(1, doys.size + 1, 1) + 4444,
                            np.arange(1, doys.size + 1, 1) + 5555,
                            np.arange(1, doys.size + 1, 1) + 6666])
    no_obs = doys * 5

    eofs = []
    for doy in doys:
        eof1 = np.array([1, 2, 3, 4, 5, 6]) * doy
        eof2 = np.array([10, 20, 30, 40, 50, 60]) * doy
        eofs.append(eof.EOFData(lat, long, eof1, eof2, explained_variances=np.squeeze(explained_variances[:, doy - 1]),
                                eigenvalues=np.squeeze(eigenvalues[:, doy - 1]), no_observations=no_obs[doy - 1]))
    target = eof.EOFDataForAllDOYs(eofs)
    target.save_all_eofs_to_npzfile(filename)

    errors = []
    target_reloaded = eof.restore_all_eofs_from_npzfile(filename)
    if not target_reloaded.eof_list == eofs:
        errors.append("List of EOFData objects incorrect")
    if not np.all(target_reloaded.lat == lat):
        errors.append("Lat is incorrect")
    if not np.all(target_reloaded.long == long):
        errors.append("Long is incorrect")
    if not target_reloaded.eofdata_for_doy(1) == eofs[0]:
        errors.append("Sample EOF data is incorrect")

    assert not errors, "errors occurred:\n{}".format("\n".join(errors))
Esempio n. 3
0
# Calculate the eofs. In the postprocessing, the signs of the EOFs are adjusted and the EOF in a period
# around DOY 300 are replaced by an interpolation see Kiladis, 2014).
# The switch strict_leap_year_treatment has major implications only for the EOFs calculated for DOY 366 and causes only
# minor differences for the other DOYs. While the results for setting strict_leap_year_treatment=False are closer to the
# original values, the calculation strict_leap_year_treatment=True is somewhat more stringently implemented using
# built-in datetime functionality.
# See documentation of mjoindices.tools.find_doy_ranges_in_dates() for details.
eofs = omi.calc_eofs_from_olr(interpolated_olr,
                              sign_doy1reference=True,
                              interpolate_eofs=True,
                              strict_leap_year_treatment=False)
eofs.save_all_eofs_to_npzfile(eofnpzfile)

# ### Some diagnostic plots to evaluate the calculated EOFs.
# Load precalculated EOFs first.
eofs = eof.restore_all_eofs_from_npzfile(eofnpzfile)

# Check the explained variance by the EOFs. Values are lower by about a factor of 2 than in Kiladis (2014),
# which is correct!
fig = eof.plot_explained_variance_for_all_doys(eofs)
fig.show()
fig.savefig(fig_dir / "EOFs_ExplainedVariance.png")

# Check details of the EOF pair for a particular DOY in the following.
doy = 50
# Plot EOFs for this DOY.
fig = eof.plot_individual_eof_map(eofs.eofdata_for_doy(doy), doy)
fig.show()
fig.savefig(fig_dir / "EOF_Sample.png")
# Plot the explained variance for the first 10 EOFs of this DOY to check the drop of explained variances after EOF2.
fig = eof.plot_individual_explained_variance_all_eofs(
Esempio n. 4
0
def test_completeOMIReproduction(tmp_path):

    errors = []

    # Calculate EOFs
    raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
    shorter_olr = olr.restrict_time_coverage(raw_olr,
                                             np.datetime64('1979-01-01'),
                                             np.datetime64('2012-12-31'))
    interpolated_olr = olr.interpolate_spatial_grid_to_original(shorter_olr)
    eofs = omi.calc_eofs_from_olr(interpolated_olr,
                                  sign_doy1reference=True,
                                  interpolate_eofs=True,
                                  strict_leap_year_treatment=False)
    eofs.save_all_eofs_to_npzfile(tmp_path /
                                  "test_completeOMIReproduction_EOFs.npz")

    # Validate EOFs against original (results are inexact but close)
    orig_eofs = eof.load_all_original_eofs_from_directory(
        originalOMIDataDirname)

    corr_1, diff_mean_1, diff_std_1, diff_abs_percent68_1, diff_abs_percent95_1, diff_abs_percent99_1 = mjoindices.evaluation_tools.calc_comparison_stats_for_eofs_all_doys(
        orig_eofs,
        eofs,
        1,
        exclude_doy366=False,
        percentage=False,
        do_print=False)
    if not np.all(corr_1 > 0.994):
        errors.append(
            "original-validation: Correlation for EOF1 at least for one DOY too low!"
        )
    if not np.all(diff_abs_percent99_1 < 0.0084):
        errors.append(
            "original-validation: 99% percentile for EOF1 at least for one DOY too high!"
        )
    if not np.all(diff_abs_percent68_1 < 0.0018):
        errors.append(
            "original-validation: 68% percentile for EOF1 at least for one DOY too high!"
        )

    corr_2, diff_mean_2, diff_std_2, diff_abs_percent68_2, diff_abs_percent95_2, diff_abs_percent99_2 = mjoindices.evaluation_tools.calc_comparison_stats_for_eofs_all_doys(
        orig_eofs,
        eofs,
        2,
        exclude_doy366=False,
        percentage=False,
        do_print=False)
    if not np.all(corr_2 > 0.993):
        errors.append(
            "original-validation: Correlation for EOF2 at least for one DOY too low!"
        )
    if not np.all(diff_abs_percent99_2 < 0.0065):
        errors.append(
            "original-validation: 99% percentile for EOF2 at least for one DOY too high!"
        )
    if not np.all(diff_abs_percent68_2 < 0.0018):
        errors.append(
            "original-validation: 68% percentile for EOF2 at least for one DOY too high!"
        )

    # Validate explained variance against original (results are inexact but close)
    orig_explained_variance_1, orig_explained_variance_2 = mjoindices.evaluation_tools.load_omi_explained_variance(
        original_omi_explained_variance_file)

    corr_var1, diff_mean_var1, diff_std_var1, diff_vec_var1, diff_abs_percent68_var1, diff_abs_percent95_var1, diff_abs_percent99_var1 = mjoindices.evaluation_tools.calc_comparison_stats_for_explained_variance(
        orig_explained_variance_1,
        eofs.explained_variance1_for_all_doys(),
        do_print=False,
        exclude_doy366=False)
    if not diff_std_var1 < 0.0008:
        errors.append(
            "original-validation: Std.Dev. of the difference of both explained variances for EOF1 is to too high!"
        )
    if not diff_abs_percent99_var1 < 0.0017:
        errors.append(
            "original-validation: 99% percentile of the difference of both explained variances for EOF1 is to too high!"
        )
    if not diff_abs_percent68_var1 < 0.0009:
        errors.append(
            "original-validation: 68% percentile of the difference of both explained variances for EOF1 is to too high!"
        )

    corr_var2, diff_mean_var2, diff_std_var2, diff_vec_var2, diff_abs_percent68_var2, diff_abs_percent95_var2, diff_abs_percent99_var2 = mjoindices.evaluation_tools.calc_comparison_stats_for_explained_variance(
        orig_explained_variance_2,
        eofs.explained_variance2_for_all_doys(),
        do_print=False,
        exclude_doy366=False)
    if not diff_std_var2 < 0.0008:
        errors.append(
            "original-validation: Std.Dev. of the difference of both explained variances for EOF2 is to too high!"
        )
    if not diff_abs_percent99_var2 < 0.0018:
        errors.append(
            "original-validation: 99% percentile of the difference of both explained variances for EOF2 is to too high!"
        )
    if not diff_abs_percent68_var2 < 0.001:
        errors.append(
            "original-validation: 68% percentile of the difference of both explained variances for EOF2 is to too high!"
        )

    # Validate EOFs against mjoindices own reference (results should be equal)
    mjoindices_reference_eofs = eof.restore_all_eofs_from_npzfile(
        mjoindices_reference_eofs_filename)
    for idx, target_eof in enumerate(eofs.eof_list):
        if not mjoindices_reference_eofs.eof_list[idx].close(target_eof):
            errors.append(
                "mjoindices-reference-validation: EOF data at index %i is incorrect"
                % idx)

    # Calculate PCs
    raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
    pcs = omi.calculate_pcs_from_olr(raw_olr,
                                     eofs,
                                     np.datetime64("1979-01-01"),
                                     np.datetime64("2018-08-28"),
                                     use_quick_temporal_filter=False)
    pc_temp_file = tmp_path / "test_completeOMIReproduction_PCs.txt"
    pcs.save_pcs_to_txt_file(pc_temp_file)

    # Validate PCs  against original (results are inexact but close)
    orig_pcs = pc.load_original_pcs_from_txt_file(origOMIPCsFilename)
    # Reload pcs instead of using the calculated ones, because the saving routine has truncated some decimals of the
    # reference values. So do the same with the testing target pcs.
    pcs = pc.load_pcs_from_txt_file(pc_temp_file)

    tempa, tempb, corr_pc1, diff_mean_pc1, diff_std_pc1, diff_ts_abs_pc1, diff_abs_percent68_pc1, diff_abs_percent95_pc1, diff_abs_percent99_pc1 = mjoindices.evaluation_tools.calc_timeseries_agreement(
        orig_pcs.pc1,
        orig_pcs.time,
        pcs.pc1,
        pcs.time,
        exclude_doy366=False,
        do_print=False)
    if not corr_pc1 > 0.998:
        errors.append(
            "original-validation: Correlation for PC1 timeseries is to too low!"
        )
    if not diff_std_pc1 < 0.0458:
        errors.append(
            "original-validation: Std.Dev. of the difference of both PC1 timeseries is to too high!"
        )
    if not diff_abs_percent99_pc1 < 0.157:
        errors.append(
            "original-validation: 99% percentile of the difference of both PC1 timeseries is to too high!"
        )
    if not diff_abs_percent68_pc1 < 0.0327:
        errors.append(
            "original-validation: 68% percentile of the difference of both PC1 timeseries is to too high!"
        )

    tempa, tempb, corr_pc2, diff_mean_pc2, diff_std_pc2, diff_ts_abs_pc2, diff_abs_percent68_pc2, diff_abs_percent95_pc2, diff_abs_percent99_pc2 = mjoindices.evaluation_tools.calc_timeseries_agreement(
        orig_pcs.pc2,
        orig_pcs.time,
        pcs.pc2,
        pcs.time,
        exclude_doy366=False,
        do_print=False)
    if not corr_pc2 > 0.998:
        errors.append(
            "original-validation: Correlation for PC2 timeseries is to too low!"
        )
    if not diff_std_pc2 < 0.0488:
        errors.append(
            "original-validation: Std.Dev. of the difference of both PC2 timeseries is to too high!"
        )
    if not diff_abs_percent99_pc2 < 0.1704:
        errors.append(
            "original-validation: 99% percentile of the difference of both PC2 timeseries is to too high!"
        )
    if not diff_abs_percent68_pc2 < 0.0353:
        errors.append(
            "original-validation: 68% percentile of the difference of both PC2 timeseries is to too high!"
        )

    strength = np.sqrt(np.square(pcs.pc1) + np.square(pcs.pc2))
    orig_strength = np.sqrt(np.square(orig_pcs.pc1) + np.square(orig_pcs.pc2))

    tempa, tempb, corr_strength, diff_mean_strength, diff_std_strength, diff_ts_abs_strength, diff_abs_percent68_strength, diff_abs_percent95_strength, diff_abs_percent99_strength = mjoindices.evaluation_tools.calc_timeseries_agreement(
        orig_strength,
        orig_pcs.time,
        strength,
        pcs.time,
        exclude_doy366=False,
        do_print=False)
    if not corr_strength > 0.9998:
        errors.append(
            "original-validation: Correlation for strength timeseries is to too low!"
        )
    if not diff_std_strength < 0.0103:
        errors.append(
            "original-validation: Std.Dev. of the difference of both strength timeseries is to too high!"
        )
    if not diff_abs_percent99_strength < 0.0341:
        errors.append(
            "original-validation: 99% percentile of the difference of both strength timeseries is to too high!"
        )
    if not diff_abs_percent68_strength < 0.0079:
        errors.append(
            "original-validation: 68% percentile of the difference of both strength timeseries is to too high!"
        )

    # Validate PCs against mjoindices own reference (results should be equal)
    mjoindices_reference_pcs = pc.load_pcs_from_txt_file(
        mjoindices_reference_pcs_filename)
    if not np.all(mjoindices_reference_pcs.time == pcs.time):
        errors.append(
            "mjoindices-reference-validation: Dates of PCs do not match.")
    if not np.allclose(mjoindices_reference_pcs.pc1, pcs.pc1):
        errors.append(
            "mjoindices-reference-validation: PC1 values do not match.")
    if not np.allclose(mjoindices_reference_pcs.pc2, pcs.pc2):
        errors.append(
            "mjoindices-reference-validation: PC2 values do not match.")

    assert not errors, "errors occurred:\n{}".format("\n".join(errors))