示例#1
0
def test_validate_data(cli):
    """
    Tests the validate_data command with mock and some real tests.
    """
    vc = "lasif.components.validator.ValidatorComponent."
    with mock.patch(vc + "validate_data") as patch:
        cli.run("lasif validate_data")
        patch.assert_called_once_with(station_file_availability=False,
                                      raypaths=False, waveforms=False)

    with mock.patch(vc + "validate_data") as patch:
        cli.run("lasif validate_data --full")
        patch.assert_called_once_with(station_file_availability=True,
                                      raypaths=True, waveforms=True)

    # Have the raypath check fail.
    with mock.patch('lasif.components.validator.ValidatorComponent'
                    '.is_event_station_raypath_within_boundaries') as p:
        p.return_value = False
        out = cli.run("lasif validate_data --full")
        assert "Some files failed the raypath in domain checks." in out.stdout
        # Created script that deletes the extraneous files.
        filename = out.stdout.splitlines()[-1].strip().strip("'")
        assert os.path.exists(filename)
        assert filename.endswith("delete_raypath_violating_files.sh")
        lines = []
        with open(filename, "rt") as fh:
            for line in fh.readlines():
                if not line.startswith("rm"):
                    continue
                lines.append(line)
        # Make sure all 6 files are actually deleted.
        assert len(lines) == 6
def test_plot_stf(cli):
    """
    Tests the source time function plots.
    """
    with mock.patch("lasif.visualization.plot_tf") as patch:
        cli.run("lasif plot_stf")
    assert patch.call_count == 1

    data, delta = patch.call_args[0]
    stf_fct = cli.comm.project.get_project_function("source_time_function")
    stf_delta = cli.comm.project.simulation_settings["time_step_in_s"]
    stf_npts = cli.comm.project.simulation_settings["number_of_time_steps"]
    stf_freqmin = (
        1.0 / cli.comm.project.simulation_settings["maximum_period_in_s"]
    )
    stf_freqmax = (
        1.0 / cli.comm.project.simulation_settings["minimum_period_in_s"]
    )

    stf_data = stf_fct(
        npts=stf_npts,
        delta=stf_delta,
        freqmin=stf_freqmin,
        freqmax=stf_freqmax,
    )
    np.testing.assert_array_equal(data, stf_data)
    assert stf_delta == delta
def test_calculate_all_adjoint_sources(cli):
    """
    Simple mock test.
    """
    with mock.patch(
        "lasif.components.adjoint_sources.AdjointSourcesComponent"
        ".calculate_adjoint_sources"
    ) as p:
        out = cli.run(
            "lasif calculate_adjoint_sources 1 B "
            "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11"
        )
    assert "Window set B not known to LASIF" in out.stderr
    assert p.call_count == 0

    with mock.patch(
        "lasif.components.adjoint_sources.AdjointSourcesComponent"
        ".calculate_adjoint_sources"
    ) as p:
        out = cli.run("lasif calculate_adjoint_sources")
    assert (
        "error: the following arguments are required: iteration_name, "
        "window_set_name, events" in out.stderr
    )
    assert p.call_count == 0

    """
示例#4
0
def test_open_tutorial(cli):
    """
    Simple mock test.
    """
    with mock.patch("webbrowser.open") as patch:
        cli.run("lasif tutorial")
        patch.assert_called_once_with("http://krischer.github.io/LASIF/")
示例#5
0
def test_validate_data(cli):
    """
    Tests the validate_data command with mock and some real tests.
    """
    vc = "lasif.components.validator.ValidatorComponent."
    with mock.patch(vc + "validate_data") as patch:
        cli.run("lasif validate_data")
        patch.assert_called_once_with(station_file_availability=False,
                                      raypaths=False,
                                      waveforms=False)

    with mock.patch(vc + "validate_data") as patch:
        cli.run("lasif validate_data --full")
        patch.assert_called_once_with(station_file_availability=True,
                                      raypaths=True,
                                      waveforms=True)

    # Have the raypath check fail.
    with mock.patch('lasif.components.validator.ValidatorComponent'
                    '.is_event_station_raypath_within_boundaries') as p:
        p.return_value = False
        out = cli.run("lasif validate_data --full")
        assert "Some files failed the raypath in domain checks." in out.stdout
        # Created script that deletes the extraneous files.
        filename = out.stdout.splitlines()[-1].strip().strip("'")
        assert os.path.exists(filename)
        assert filename.endswith("delete_raypath_violating_files.sh")
        lines = []
        with open(filename, "rt") as fh:
            for line in fh.readlines():
                if not line.startswith("rm"):
                    continue
                lines.append(line)
        # Make sure all 6 files are actually deleted.
        assert len(lines) == 6
示例#6
0
def test_download_utitlies(cli):
    """
    Testing the invocation of the downloaders.
    """
    # SPUD interface downloader.
    with mock.patch("lasif.scripts.iris2quakeml.iris2quakeml") as patch:
        cli.run("lasif add_spud_event https://test.org")
    patch.assert_called_once_with(
        "https://test.org", cli.comm.project.paths["events"])
    assert patch.call_count == 1

    # Test the download data invocation.
    with mock.patch("lasif.components.downloads.DownloadsComponent"
                    ".download_data") \
            as download_patch:
        out = cli.run("lasif download_data "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert out.stderr == ""
    download_patch.assert_called_once_with(
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11", providers=None)
    assert download_patch.call_count == 1

    # Test setting the providers.
    with mock.patch("lasif.components.downloads.DownloadsComponent"
                    ".download_data") \
            as download_patch:
        out = cli.run("lasif download_data "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                      "--providers IRIS ORFEUS")
    assert out.stderr == ""
    download_patch.assert_called_once_with(
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        providers=["IRIS", "ORFEUS"])
    assert download_patch.call_count == 1
示例#7
0
def test_download_utitlies(cli):
    """
    Testing the invocation of the downloaders.
    """
    # SPUD interface downloader.
    with mock.patch("lasif.scripts.iris2quakeml.iris2quakeml") as patch:
        cli.run("lasif add_spud_event https://test.org")
    patch.assert_called_once_with("https://test.org",
                                  cli.comm.project.paths["events"])
    assert patch.call_count == 1

    # Test the download data invocation.
    with mock.patch("lasif.components.downloads.DownloadsComponent"
                    ".download_data") \
            as download_patch:
        out = cli.run("lasif download_data "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert out.stderr == ""
    download_patch.assert_called_once_with(
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11", providers=None)
    assert download_patch.call_count == 1

    # Test setting the providers.
    with mock.patch("lasif.components.downloads.DownloadsComponent"
                    ".download_data") \
            as download_patch:
        out = cli.run("lasif download_data "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                      "--providers IRIS ORFEUS")
    assert out.stderr == ""
    download_patch.assert_called_once_with(
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        providers=["IRIS", "ORFEUS"])
    assert download_patch.call_count == 1
def test_open_tutorial(cli):
    """
    Simple mock test.
    """
    with mock.patch("webbrowser.open") as patch:
        cli.run("lasif tutorial")
        patch.assert_called_once_with("http://dirkphilip.github.io/LASIF_2.0/")
def test_iteration_creation(cli):
    """
    Tests the generation of an iteration and removal of one
    """
    cli.run("lasif set_up_iteration 3")
    assert cli.comm.iterations.has_iteration("3")
    cli.run("lasif set_up_iteration 3 --remove_dirs")
    assert not cli.comm.iterations.has_iteration("3")
示例#10
0
def test_remove_empty_coordinate_entries(cli):
    """
    Simple mock test.
    """
    with mock.patch("lasif.tools.inventory_db.reset_coordinate_less_stations")\
            as patch:
        cli.run("lasif remove_empty_coordinate_entires")
        assert patch.assert_run_once_with(cli.project.paths["inv_db_file"])
示例#11
0
def test_finalize_adjoint_sources(cli):
    """
    Simple mock test.
    """
    with mock.patch("lasif.project.Project.finalize_adjoint_sources") as p:
        cli.run("lasif generate_input_files 1 "
                "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
        p.assert_calles_once_with(
            "1", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
示例#12
0
def test_iteration_info(cli):
    """
    Tests the 'lasif iteration_info' command.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")

    out = cli.run("lasif iteration_info 1").stdout
    assert "LASIF Iteration" in out
    assert "Name: 1" in out
    assert "Solver: SES3D 4.1" in out
示例#13
0
def test_invocation_without_parameters(cli):
    """
    Tests the invocation without any parameters.
    """
    default_output = cli.run("lasif")
    # Should be the same as if invoced with --help.
    assert default_output == cli.run("lasif --help")
    # It should furthermore contain a list of all commands.
    for cmd in CMD_LIST:
        assert cmd in default_output.stdout
示例#14
0
def test_iteration_info(cli):
    """
    Tests the 'lasif iteration_info' command.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")

    out = cli.run("lasif iteration_info 1").stdout
    assert "LASIF Iteration" in out
    assert "Name: 1" in out
    assert "Solver: SES3D 4.1" in out
示例#15
0
def test_invocation_without_parameters(cli):
    """
    Tests the invocation without any parameters.
    """
    default_output = cli.run("lasif")
    # Should be the same as if invoced with --help.
    assert default_output == cli.run("lasif --help")
    # It should furthermore contain a list of all commands.
    for cmd in CMD_LIST:
        assert cmd in default_output.stdout
示例#16
0
def test_preprocessing_event_limiting_works(cli):
    """
    Asserts that the event parsing is correct.
    """
    ac = "lasif.components.actions.ActionsComponent."
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")

    # No event should result in None.
    with mock.patch(ac + "preprocess_data") as patch:
        cli.run("lasif preprocess_data 1")
    assert patch.call_count == 1
    patch.assert_called_once_with("1", None)

    # One specified event should result in one event.
    with mock.patch(ac + "preprocess_data") as patch:
        cli.run("lasif preprocess_data 1 "
                "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert patch.call_count == 1
    patch.assert_called_once_with(
        "1", ["GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11"])

    # Multiple result in multiple.
    with mock.patch(ac + "preprocess_data") as patch:
        cli.run("lasif preprocess_data 1 "
                "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15")
    assert patch.call_count == 1
    patch.assert_called_once_with(
        "1", ["GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
              "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15"])

    out = cli.run("lasif preprocess_data 1 blub wub").stdout
    assert "Event 'blub' not found." in out
示例#17
0
def test_preprocessing_event_limiting_works(cli):
    """
    Asserts that the event parsing is correct.
    """
    ac = "lasif.components.actions.ActionsComponent."
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")

    # No event should result in None.
    with mock.patch(ac + "preprocess_data") as patch:
        cli.run("lasif preprocess_data 1")
    assert patch.call_count == 1
    patch.assert_called_once_with("1", None)

    # One specified event should result in one event.
    with mock.patch(ac + "preprocess_data") as patch:
        cli.run("lasif preprocess_data 1 "
                "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert patch.call_count == 1
    patch.assert_called_once_with(
        "1", ["GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11"])

    # Multiple result in multiple.
    with mock.patch(ac + "preprocess_data") as patch:
        cli.run("lasif preprocess_data 1 "
                "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15")
    assert patch.call_count == 1
    patch.assert_called_once_with("1", [
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15"
    ])

    out = cli.run("lasif preprocess_data 1 blub wub").stdout
    assert "Event 'blub' not found." in out
示例#18
0
def test_Q_model_plotting(cli):
    """
    Tests the Q model plotting via mocking.
    """
    cli.run("lasif create_new_iteration 1 7.0 70.0 SES3D_4_1")
    with mock.patch("lasif.tools.Q_discrete.plot") as patch:
        cli.run("lasif plot_Q_model 1")
        patch.assert_called_once()
        kwargs = patch.call_args[1]

    assert round(kwargs["f_min"] - 1.0 / 70.0, 5) == 0
    assert round(kwargs["f_max"] - 1.0 / 7.0, 5) == 0
示例#19
0
def test_read_only_option_not_working_for_certain_commands(cli):
    """
    The read-only option should not work for certain function. Mainly the
    "build_all_caches" option right now. It makes little sense to have it
    for this function...
    """
    err = cli.run("lasif build_all_caches").stderr
    assert err.strip() == ""

    # Should raise here.
    err = cli.run("lasif build_all_caches --read_only_caches").stderr
    assert "unrecognized arguments: --read_only_caches" in err
示例#20
0
def test_read_only_option_not_working_for_certain_commands(cli):
    """
    The read-only option should not work for certain function. Mainly the
    "build_all_caches" option right now. It makes little sense to have it
    for this function...
    """
    err = cli.run("lasif build_all_caches").stderr
    assert err.strip() == ""

    # Should raise here.
    err = cli.run("lasif build_all_caches --read_only_caches").stderr
    assert "unrecognized arguments: --read_only_caches" in err
示例#21
0
def test_debug_information(cli):
    """
    Tests the debugging information.
    """
    # Files not found.
    out = cli.run("lasif debug DUMMY_1 DUMMY_2").stdout
    assert "Path 'DUMMY_1' does not exist." in out
    assert "Path 'DUMMY_2' does not exist." in out

    # Check a file to make sure the binding works. Other file types are
    # tested elsewhere.
    out = cli.run("lasif debug " + cli.comm.project.paths["config_file"])
    assert "The main project configuration file" in out.stdout
示例#22
0
def test_validate_data(cli):
    """
    Simple mock test.
    """
    with mock.patch("lasif.project.Project.validate_data") as patch:
        cli.run("lasif validate_data")
        patch.assert_called_once_with(station_file_availability=False,
                                      raypaths=False, waveforms=False)

    with mock.patch("lasif.project.Project.validate_data") as patch:
        cli.run("lasif validate_data --full")
        patch.assert_called_once_with(station_file_availability=True,
                                      raypaths=True, waveforms=True)
示例#23
0
def test_debug_information(cli):
    """
    Tests the debugging information.
    """
    # Files not found.
    out = cli.run("lasif debug DUMMY_1 DUMMY_2").stdout
    assert "Path 'DUMMY_1' does not exist." in out
    assert "Path 'DUMMY_2' does not exist." in out

    # Check a file to make sure the binding works. Other file types are
    # tested elsewhere.
    out = cli.run("lasif debug " + cli.comm.project.paths["config_file"])
    assert "The main project configuration file" in out.stdout
示例#24
0
def test_help_messages(cli):
    """
    Tests the help messages.
    """
    for cmd in CMD_LIST:
        # Both invocations should work
        assert cli.run("lasif %s --help" % cmd) == \
            cli.run("lasif help %s" % cmd)
        # Some things should always be shown. This also more or less tests that
        # the argparse parser is used everywhere.
        help_string = cli.run("lasif %s --help" % cmd).stdout
        assert help_string.startswith("usage: lasif %s" % cmd)
        assert "show this help message and exit" in help_string
        assert "optional arguments:" in help_string
示例#25
0
def test_help_messages(cli):
    """
    Tests the help messages.
    """
    for cmd in CMD_LIST:
        # Both invocations should work
        assert cli.run("lasif %s --help" % cmd) == \
            cli.run("lasif help %s" % cmd)
        # Some things should always be shown. This also more or less tests that
        # the argparse parser is used everywhere.
        help_string = cli.run("lasif %s --help" % cmd).stdout
        assert help_string.startswith("usage: lasif %s" % cmd)
        assert "show this help message and exit" in help_string
        assert "optional arguments:" in help_string
示例#26
0
def test_read_only_cache_flag_passed_on(cli):
    """
    Make sure the read_only_caches flag is passed on.
    """
    # This is not comprehensive but the handling is the same for each CLI
    # function so it should be good enough I hope.
    with mock.patch("lasif.components.project.Project.__init__") as p:
        cli.run("lasif list_events")
    assert p.call_count == 1
    assert p.call_args[1] == {"read_only_caches": False}

    with mock.patch("lasif.components.project.Project.__init__") as p:
        cli.run("lasif list_events --read_only_caches")
    assert p.call_count == 1
    assert p.call_args[1] == {"read_only_caches": True}

    # Should also be passed on to the underlying actual cache objects.
    with mock.patch(
            "lasif.tools.cache_helpers.event_cache.EventCache.__init__") as p:
        cli.run("lasif list_events")
    assert p.call_count == 1
    assert p.call_args[1]["read_only"] is False

    with mock.patch(
            "lasif.tools.cache_helpers.event_cache.EventCache.__init__") as p:
        cli.run("lasif list_events --read_only_caches")
    assert p.call_count == 1
    assert p.call_args[1]["read_only"] is True
示例#27
0
def test_read_only_cache_flag_passed_on(cli):
    """
    Make sure the read_only_caches flag is passed on.
    """
    # This is not comprehensive but the handling is the same for each CLI
    # function so it should be good enough I hope.
    with mock.patch("lasif.components.project.Project.__init__") as p:
        cli.run("lasif list_events")
    assert p.call_count == 1
    assert p.call_args[1] == {"read_only_caches": False}

    with mock.patch("lasif.components.project.Project.__init__") as p:
        cli.run("lasif list_events --read_only_caches")
    assert p.call_count == 1
    assert p.call_args[1] == {"read_only_caches": True}

    # Should also be passed on to the underlying actual cache objects.
    with mock.patch(
            "lasif.tools.cache_helpers.event_cache.EventCache.__init__") as p:
        cli.run("lasif list_events")
    assert p.call_count == 1
    assert p.call_args[1]["read_only"] is False

    with mock.patch(
            "lasif.tools.cache_helpers.event_cache.EventCache.__init__") as p:
        cli.run("lasif list_events --read_only_caches")
    assert p.call_count == 1
    assert p.call_args[1]["read_only"] is True
示例#28
0
def test_Q_model_plotting(cli):
    """
    Tests the Q model plotting via mocking.
    """
    cli.run("lasif create_new_iteration 1 7.0 70.0 SES3D_4_1")
    with mock.patch("lasif.tools.Q_discrete.plot") as patch:
        out = cli.run("lasif plot_Q_model 1")

    assert out.stderr == ""
    assert patch.call_count == 1
    kwargs = patch.call_args[1]

    assert round(kwargs["f_min"] - 1.0 / 70.0, 5) == 0
    assert round(kwargs["f_max"] - 1.0 / 7.0, 5) == 0
示例#29
0
def test_input_file_generation(cli):
    """
    Mock test to see if the input file generation routine is called. The
    routine is tested partially by the event tests and more by the input file
    generation module.
    """
    ac = "lasif.components.actions.ActionsComponent."
    # No simluation type specified.
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_input_files 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert out.stderr == ""
    patch.assert_called_once_with(
        "1", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "normal simulation")
    assert patch.call_count == 1

    # Normal simulation
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_input_files 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                      "--simulation_type=normal_simulation")
    assert out.stderr == ""
    patch.assert_called_once_with(
        "1", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "normal simulation")
    assert patch.call_count == 1

    # Adjoint forward.
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_input_files 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                      "--simulation_type=adjoint_forward")
    assert out.stderr == ""
    patch.assert_called_once_with(
        "1", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "adjoint forward")
    assert patch.call_count == 1

    # Adjoint reverse.
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_input_files 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                      "--simulation_type=adjoint_reverse")
    assert out.stderr == ""
    patch.assert_called_once_with(
        "1", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "adjoint reverse")
    assert patch.call_count == 1
示例#30
0
def test_input_file_generation(cli):
    """
    Mock test to see if the input file generation routine is called. The
    routine is tested partially by the event tests and more by the input file
    generation module.
    """
    ac = "lasif.components.actions.ActionsComponent."
    # No simluation type specified.
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_input_files 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert out.stderr == ""
    patch.assert_called_once_with("1",
                                  "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
                                  "normal simulation")
    assert patch.call_count == 1

    # Normal simulation
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_input_files 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                      "--simulation_type=normal_simulation")
    assert out.stderr == ""
    patch.assert_called_once_with("1",
                                  "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
                                  "normal simulation")
    assert patch.call_count == 1

    # Adjoint forward.
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_input_files 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                      "--simulation_type=adjoint_forward")
    assert out.stderr == ""
    patch.assert_called_once_with("1",
                                  "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
                                  "adjoint forward")
    assert patch.call_count == 1

    # Adjoint reverse.
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_input_files 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11 "
                      "--simulation_type=adjoint_reverse")
    assert out.stderr == ""
    patch.assert_called_once_with("1",
                                  "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
                                  "adjoint reverse")
    assert patch.call_count == 1
示例#31
0
def test_iteration_creation_and_stf_plotting(cli):
    """
    Tests the generation of an iteration and the supsequent STF plotting.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")
    assert cli.comm.iterations.has_iteration("1")

    with mock.patch("lasif.visualization.plot_tf") as patch:
        cli.run("lasif plot_stf 1")
    assert patch.call_count == 1
    data, delta = patch.call_args[0]
    np.testing.assert_array_equal(
        data,
        cli.comm.iterations.get("1").get_source_time_function()["data"])
    assert delta == 0.75
示例#32
0
def test_iteration_creation_and_stf_plotting(cli):
    """
    Tests the generation of an iteration and the supsequent STF plotting.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")
    assert cli.comm.iterations.has_iteration("1")

    with mock.patch("lasif.visualization.plot_tf") as patch:
        cli.run("lasif plot_stf 1")
    assert patch.call_count == 1
    data, delta = patch.call_args[0]
    np.testing.assert_array_equal(
        data,
        cli.comm.iterations.get("1").get_source_time_function()["data"])
    assert delta == 0.75
示例#33
0
def test_iteration_creation_and_stf_plotting(cli):
    """
    Tests the generation of an iteration and the supsequent STF plotting.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")
    assert "1" in cli.project.get_iteration_dict().keys()

    with mock.patch("lasif.visualization.plot_tf") as patch:
        cli.run("lasif plot_stf 1")
        patch.assert_called_once()
        data, delta = patch.call_args[0]
        np.testing.assert_array_equal(
            data,
            cli.project._get_iteration("1").get_source_time_function()["data"])
        assert delta == 0.75
示例#34
0
def test_version_str(cli):
    """
    Tests if the version is printed correctly.
    """
    out = cli.run("lasif --version")
    assert out.stderr == ""
    assert out.stdout.strip() == "LASIF version %s" % lasif.__version__
示例#35
0
def test_cli_parsing_corner_cases(cli):
    """
    Tests any funky corner cases related to the command line parsing.
    """
    out = cli.run("lasif help --help")
    assert out.stdout == ""
    assert out.stderr == "lasif: Invalid command. See 'lasif --help'.\n"
示例#36
0
def test_Q_model_calculating(cli):
    """
    Tests the Q model calculation via mocking.
    """
    # Mocked in the fixture's run() function.
    out = cli.run("lasif calculate_constant_Q_model 12 234").stdout
    assert out == ''
示例#37
0
def test_project_init(cli):
    """
    Tests the project initialization with the CLI interface.
    """
    # Invocation without a folder path fails.
    log = cli.run("lasif init_project")
    assert "error: too few arguments" in log.stderr
示例#38
0
def test_cli_parsing_corner_cases(cli):
    """
    Tests any funky corner cases related to the command line parsing.
    """
    out = cli.run("lasif help --help")
    assert out.stdout == ""
    assert out.stderr == "lasif: Invalid command. See 'lasif --help'.\n"
示例#39
0
def test_project_init_without_arguments(cli):
    """
    Tests the project initialization with the CLI interface without passed
    arguments.
    """
    # Invocation without a folder path fails.
    log = cli.run("lasif init_project")
    assert (
        "error: the following arguments are required: folder_path"
        in log.stderr
    )
    log2 = cli.run("lasif plot_event")
    assert (
        "error: the following arguments are required: event_name"
        in log2.stderr
    )
示例#40
0
def test_version_str(cli):
    """
    Tests if the version is printed correctly.
    """
    out = cli.run("lasif --version")
    assert out.stderr == ""
    assert out.stdout.strip() == "LASIF version %s" % lasif.__version__
示例#41
0
def test_Q_model_calculating(cli):
    """
    Tests the Q model calculation via mocking.
    """
    # Mocked in the fixture's run() function.
    out = cli.run("lasif calculate_constant_Q_model 12 234").stdout
    assert out == ''
示例#42
0
def test_lasif_info(cli):
    """
    Tests the 'lasif info' command.
    """
    out = cli.run("lasif info").stdout
    assert '"example_project"' in out
    assert "Toy Project used in the Test Suite" in out
    assert "2 events" in out
示例#43
0
def test_unknown_command(cli):
    """
    Tests the message when an unknown command is called.
    """
    out = cli.run("lasif asdflkjaskldfj")
    assert out.stdout == ""
    assert out.stderr == ("lasif: 'asdflkjaskldfj' is not a LASIF command. "
                          "See 'lasif --help'.\n")
示例#44
0
def test_project_init_without_arguments(cli):
    """
    Tests the project initialization with the CLI interface without passed
    arguments.
    """
    # Invocation without a folder path fails.
    log = cli.run("lasif init_project")
    assert "error: too few arguments" in log.stderr
示例#45
0
def test_unknown_command(cli):
    """
    Tests the message when an unknown command is called.
    """
    out = cli.run("lasif asdflkjaskldfj")
    assert out.stdout == ""
    assert out.stderr == ("lasif: 'asdflkjaskldfj' is not a LASIF command. "
                          "See 'lasif --help'.\n")
示例#46
0
def test_validate_data(cli):
    """
    Tests the validate_data command with mock and some real tests.
    """
    vc = "lasif.components.validator.ValidatorComponent."
    with mock.patch(vc + "validate_data") as patch:
        cli.run("lasif validate_data")
        patch.assert_called_once_with(
            data_and_station_file_availability=False, raypaths=False
        )

    with mock.patch(vc + "validate_data") as patch:
        cli.run("lasif validate_data --full")
        patch.assert_called_once_with(
            data_and_station_file_availability=True, raypaths=True
        )

    with mock.patch("lasif.domain.HDF5Domain.point_in_domain") as patch:
        cli.run("lasif validate_data")
        assert patch.call_count == 2

    # Have the raypath check fail.
    # Add this one when we have some data. Now it doesn't perform the ray check
    with mock.patch(
        "lasif.components.validator.ValidatorComponent"
        ".is_event_station_raypath_within_boundaries"
    ) as p:
        p.return_value = False
        out = cli.run("lasif validate_data --full")
        assert "Validating 2 event files" in out.stdout
示例#47
0
def test_lasif_event_info(cli):
    """
    Tests the event info function.
    """
    event_1 = cli.run("lasif event_info "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11").stdout
    event_2 = cli.run("lasif event_info "
                      "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15").stdout

    assert "5.1 Mw" in event_1
    assert "TURKEY" in event_1
    assert "38.820" in event_1
    assert "available at 4 stations" in event_1

    assert "5.9 Mw" in event_2
    assert "TURKEY" in event_2
    assert "39.150" in event_2
    assert "available at 0 stations" in event_2
示例#48
0
def test_lasif_event_info(cli):
    """
    Tests the event info function.
    """
    event_1 = cli.run("lasif event_info "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11").stdout
    event_2 = cli.run("lasif event_info "
                      "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15").stdout

    assert "5.1 Mw" in event_1
    assert "TURKEY" in event_1
    assert "38.820" in event_1
    assert "available at 4 stations" in event_1

    assert "5.9 Mw" in event_2
    assert "TURKEY" in event_2
    assert "39.150" in event_2
    assert "available at 0 stations" in event_2
示例#49
0
def test_iteration_status_command(cli):
    """
    The iteration status command returns the current state of any iteration. It
    returns the number of already preprocessed data files, how many synthetics
    are available, the windows and adjoint sources.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")
    out = cli.run("lasif iteration_status 1").stdout.splitlines()
    assert [_i.strip() for _i in out] == [
        "Iteration 1 is defined for 1 events:",
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "0.00 % of the events stations have picked windows",
        "Lacks processed data for 4 stations",
        "Lacks synthetic data for 2 stations",
    ]

    cli.run("lasif preprocess_data 1")
    out = cli.run("lasif iteration_status 1").stdout.splitlines()
    assert [_i.strip() for _i in out] == [
        "Iteration 1 is defined for 1 events:",
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "0.00 % of the events stations have picked windows",
        "Lacks synthetic data for 2 stations",
    ]

    # Copy the data for the first event to the second.
    shutil.rmtree(os.path.join(
        cli.comm.project.paths["data"],
        "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15"))
    shutil.copytree(
        os.path.join(cli.comm.project.paths["data"],
                     "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11"),
        os.path.join(cli.comm.project.paths["data"],
                     "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15"))
    # The iteration has to be recreated.
    os.remove(os.path.join(cli.comm.project.paths["iterations"],
                           "ITERATION_1.xml"))

    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")
    out = cli.run("lasif iteration_status 1").stdout.splitlines()
    assert [_i.strip() for _i in out] == [
        "Iteration 1 is defined for 2 events:",
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "0.00 % of the events stations have picked windows",
        "Lacks synthetic data for 2 stations",
        "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15",
        "0.00 % of the events stations have picked windows",
        "Lacks synthetic data for 4 stations",
    ]
示例#50
0
def test_iteration_status_command(cli):
    """
    The iteration status command returns the current state of any iteration. It
    returns the number of already preprocessed data files, how many synthetics
    are available, the windows and adjoint sources.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")
    out = cli.run("lasif iteration_status 1").stdout.splitlines()
    assert [_i.strip() for _i in out] == [
        "Iteration 1 is defined for 1 events:",
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "0.00 % of the events stations have picked windows",
        "Lacks processed data for 4 stations",
        "Lacks synthetic data for 2 stations",
    ]

    cli.run("lasif preprocess_data 1")
    out = cli.run("lasif iteration_status 1").stdout.splitlines()
    assert [_i.strip() for _i in out] == [
        "Iteration 1 is defined for 1 events:",
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "0.00 % of the events stations have picked windows",
        "Lacks synthetic data for 2 stations",
    ]

    # Copy the data for the first event to the second.
    shutil.rmtree(
        os.path.join(cli.comm.project.paths["data"],
                     "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15"))
    shutil.copytree(
        os.path.join(cli.comm.project.paths["data"],
                     "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11"),
        os.path.join(cli.comm.project.paths["data"],
                     "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15"))
    # The iteration has to be recreated.
    os.remove(
        os.path.join(cli.comm.project.paths["iterations"], "ITERATION_1.xml"))

    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")
    out = cli.run("lasif iteration_status 1").stdout.splitlines()
    assert [_i.strip() for _i in out] == [
        "Iteration 1 is defined for 2 events:",
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "0.00 % of the events stations have picked windows",
        "Lacks synthetic data for 2 stations",
        "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15",
        "0.00 % of the events stations have picked windows",
        "Lacks synthetic data for 4 stations",
    ]
示例#51
0
def test_preprocessing(cli):
    """
    Tests the proprocessing.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")

    event = "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11"
    data_path = cli.comm.project.paths["data"]
    processing_tag = cli.comm.iterations.get("1").processing_tag
    preprocessing_path = os.path.join(data_path, event, processing_tag)

    # Nothing should exist yet.
    assert not os.path.exists(preprocessing_path)

    # Preprocess some data.
    cli.run("lasif preprocess_data 1")

    assert os.path.exists(preprocessing_path)
    assert len(os.listdir(preprocessing_path)) == 6
示例#52
0
def test_generate_all_input_files(cli):
    """
    Mock test for generate_all_input_files.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")

    ac = "lasif.components.actions.ActionsComponent."

    # Now there actually is just one event with data so it will only be
    # called once.
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_all_input_files 1 "
                      "--simulation_type=adjoint_forward")
    assert out.stderr == ""

    patch.assert_called_once_with("1",
                                  "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
                                  "adjoint forward")
    assert patch.call_count == 1
示例#53
0
def test_generate_all_input_files(cli):
    """
    Mock test for generate_all_input_files.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")

    ac = "lasif.components.actions.ActionsComponent."

    # Now there actually is just one event with data so it will only be
    # called once.
    with mock.patch(ac + "generate_input_files") as patch:
        out = cli.run("lasif generate_all_input_files 1 "
                      "--simulation_type=adjoint_forward")
    assert out.stderr == ""

    patch.assert_called_once_with(
        "1", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11",
        "adjoint forward")
    assert patch.call_count == 1
示例#54
0
def test_preprocessing(cli):
    """
    Tests the proprocessing.
    """
    cli.run("lasif create_new_iteration 1 8.0 100.0 SES3D_4_1")

    event = "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11"
    data_path = cli.comm.project.paths["data"]
    processing_tag = cli.comm.iterations.get("1").processing_tag
    preprocessing_path = os.path.join(data_path, event, processing_tag)

    # Nothing should exist yet.
    assert not os.path.exists(preprocessing_path)

    # Preprocess some data.
    cli.run("lasif preprocess_data 1")

    assert os.path.exists(preprocessing_path)
    assert len(os.listdir(preprocessing_path)) == 6
示例#55
0
def test_finalize_adjoint_sources(cli):
    """
    Simple mock test.
    """
    with mock.patch("lasif.components.actions.ActionsComponent"
                    ".finalize_adjoint_sources") as p:
        out = cli.run("lasif finalize_adjoint_sources 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert out.stderr == ""
    p.assert_called_once_with("1", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert p.call_count == 1
示例#56
0
def test_remove_empty_coordinate_entries(cli):
    """
    Simple mock test.
    """
    with mock.patch("lasif.components.inventory_db.InventoryDBComponent"
                    ".remove_coordinate_less_stations")\
            as patch:
        out = cli.run("lasif remove_empty_coordinate_entries")
    assert out.stderr == ""
    patch.assert_called_once_with()
    assert patch.call_count == 1
示例#57
0
def test_remove_empty_coordinate_entries(cli):
    """
    Simple mock test.
    """
    with mock.patch("lasif.components.inventory_db.InventoryDBComponent"
                    ".remove_coordinate_less_stations")\
            as patch:
        out = cli.run("lasif remove_empty_coordinate_entries")
    assert out.stderr == ""
    patch.assert_called_once_with()
    assert patch.call_count == 1
示例#58
0
def test_building_all_cached(cli):
    """
    Tests the build all caches routine.
    """
    out = cli.run("lasif build_all_caches")
    assert out.stderr == ""

    assert "Building/updating station cache" in out.stdout
    assert ("Building/updating data cache for event "
            "'GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11'") in out.stdout
    assert ("Building/updating data cache for event "
            "'GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15'") in out.stdout
示例#59
0
def test_lasif_info(cli):
    """
    Tests the 'lasif info' command.
    """
    out = cli.run("lasif info").stdout
    assert "\"ExampleProject\"" in out
    assert "Toy Project used in the Test Suite" in out
    assert "2 events" in out
    assert "4 station files" in out
    assert "6 raw waveform files" in out
    assert "0 processed waveform files" in out
    assert "6 synthetic waveform files" in out
示例#60
0
def test_finalize_adjoint_sources(cli):
    """
    Simple mock test.
    """
    with mock.patch("lasif.components.actions.ActionsComponent"
                    ".finalize_adjoint_sources") as p:
        out = cli.run("lasif finalize_adjoint_sources 1 "
                      "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert out.stderr == ""
    p.assert_called_once_with(
        "1", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11")
    assert p.call_count == 1