コード例 #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))
コード例 #2
0
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(
    eofs.eofdata_for_doy(doy), doy=doy, max_eof_number=10)
fig.show()
fig.savefig(fig_dir / "EOF_SampleExplainedVariance.png")

# ############## Calculation of the PCs ##################

# Load the OLR data.
# This is the second place to insert your own OLR data, if you want to compute OMI for a different dataset.
olr = olr.load_noaa_interpolated_olr(olr_data_filename)
# Load EOFs
eofs = eof.restore_all_eofs_from_npzfile(eofnpzfile)

# Calculate the PCs.
# Restrict calculation to the length of the official OMI time series.
pcs = omi.calculate_pcs_from_olr(olr,
                                 eofs,
                                 np.datetime64("1979-01-01"),
                                 np.datetime64("2018-08-28"),
                                 use_quick_temporal_filter=False)
# Save PCs.
pcs.save_pcs_to_txt_file(pctxtfile)

# ### Diagnostic plot: Comparison to original PCs.
pcs = pc.load_pcs_from_txt_file(pctxtfile)
orig_pcs = pc.load_original_pcs_from_txt_file(originalOMIPCFile)
fig = mjoindices.evaluation_tools.plot_comparison_orig_calc_pcs(pcs, orig_pcs)
fig.savefig(fig_dir / "PCs_TimeSeries.png")
コード例 #3
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))