def lasif_create_successive_iteration(parser, args): """ Create an iteration based on an existing one. It will take all settings in one iteration and transfers them to another iteration. Any comments will be deleted. """ parser.add_argument("existing_iteration", help="name of the existing iteration") parser.add_argument("new_iteration", help="name of the new iteration") args = parser.parse_args(args) existing_iteration_name = args.existing_iteration new_iteration_name = args.new_iteration from lasif.iteration_xml import Iteration # Get the old iteration proj = _find_project_root(".") iterations = proj.get_iteration_dict() if existing_iteration_name not in iterations: msg = ("Iteration '%s' not found. Use 'lasif list_iterations' to get " "a list of all available iterations.") % \ existing_iteration_name raise LASIFCommandLineException(msg) existing_iteration = Iteration(iterations[existing_iteration_name]) # Clone the old iteration, delete any comments and change the name. existing_iteration.comments = [] existing_iteration.iteration_name = new_iteration_name print existing_iteration
def test_iteration_equality(): """ Tests equality/inequality for iteration xml files. """ filename = os.path.join(data_dir, "iteration_example.xml") iteration = Iteration(filename, stf_fct=__stf_fct_dummy) other_iteration = copy.deepcopy(iteration) assert iteration == other_iteration assert not iteration != other_iteration iteration.iteration_name = "blub" assert iteration != other_iteration assert not iteration == other_iteration
def test_reading_writing_with_empty_description(tmpdir): """ Tests reading and writing with an empty description. """ filename = os.path.join(data_dir, "iteration_example.xml") new_filename = os.path.join(str(tmpdir), "iteration.xml") iteration = Iteration(filename) iteration.description = None # Write and read again. iteration.write(new_filename) reread_iteration = Iteration(new_filename) assert iteration == reread_iteration
def get(self, iteration_name): """ Returns an iteration object. :param iteration_name: The name of the iteration to retrieve. >>> comm = getfixture('iterations_comm') >>> comm.iterations.get("1") # doctest: +ELLIPSIS <lasif.iteration_xml.Iteration object at ...> >>> print comm.iterations.get("1") \ # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS LASIF Iteration Name: 1 ... A :class:`~lasif.LASIFNotFoundError` will be raised, if the iteration is not known. >>> comm.iterations.get("99") Traceback (most recent call last): ... LASIFNotFoundError: ... It also works with the long iteration name and event an existing iteration object. This makes it simple to use, one path for all possibilities. >>> it = comm.iterations.get("ITERATION_1") >>> it # doctest: +ELLIPSIS <lasif.iteration_xml.Iteration object at ...> >>> comm.iterations.get(it) <lasif.iteration_xml.Iteration object at ...> """ # Make it work with both the long and short version of the iteration # name, and existing iteration object. try: iteration_name = str(iteration_name.iteration_name) except AttributeError: iteration_name = str(iteration_name) iteration_name = iteration_name.lstrip("ITERATION_") # Access cache. if iteration_name in self.__cached_iterations: return self.__cached_iterations[iteration_name] it_dict = self.get_iteration_dict() if iteration_name not in it_dict: msg = "Iteration '%s' not found." % iteration_name raise LASIFNotFoundError(msg) from lasif.iteration_xml import Iteration it = Iteration(it_dict[iteration_name], stf_fct=self.comm.project.get_project_function( "source_time_function")) # Store in cache. self.__cached_iterations[iteration_name] = it return it
def test_reading_and_writing(tmpdir): """ Tests that reading and writing a file via IterationXML does not alter it. This effectively tests the Iteration XML writing. """ filename = os.path.join(data_dir, "iteration_example.xml") new_filename = os.path.join(str(tmpdir), "iteration.xml") iteration = Iteration(filename, stf_fct=__stf_fct_dummy) iteration.write(new_filename) # Compare the lxml etree's to avoid any difference in formatting and # what not. tree_old = etree.tounicode(etree.parse(filename), pretty_print=True) tree_new = etree.tounicode(etree.parse(new_filename), pretty_print=True) # pytest takes care of meaningful string differences. assert tree_old == tree_new
def get_iteration_detail(iteration_name): """ Returns a list of events. """ from lasif.iteration_xml import Iteration iterations = app.project.get_iteration_dict() iteration = Iteration(iterations[iteration_name]) stf = iteration.get_source_time_function() stf["data"] = stf["data"].tolist() return flask.jsonify( iteration_name=iteration.iteration_name, description=iteration.description, comments=iteration.comments, data_preprocessing=iteration.data_preprocessing, events=iteration.events.keys(), processing_params=iteration.get_process_params(), processing_tag=iteration.get_processing_tag(), solver=iteration.solver_settings["solver"], solver_settings=iteration.solver_settings["solver_settings"], source_time_function=stf)
def test_reading_writing_with_empty_description(tmpdir): """ Tests reading and writing with an empty description. """ filename = os.path.join(data_dir, "iteration_example.xml") new_filename = os.path.join(str(tmpdir), "iteration.xml") iteration = Iteration(filename, stf_fct=__stf_fct_dummy) iteration.description = None # Write and read again. iteration.write(new_filename) reread_iteration = Iteration(new_filename, stf_fct=__stf_fct_dummy) # Change the name as it is always dependent on the filename. reread_iteration.iteration_name = iteration.iteration_name assert iteration == reread_iteration
def create_successive_iteration(self, existing_iteration_name, new_iteration_name): """ Create an iteration based on an existing one. It will take all settings in one iteration and transfers them to another iteration. Any comments will be deleted. :param existing_iteration_name: Name of the iteration to be used as a template. :param new_iteration_name: Name of the new iteration. >>> comm = getfixture('iterations_comm') >>> comm.iterations.has_iteration("3") False >>> comm.iterations.create_successive_iteration("1", "3") >>> comm.iterations.has_iteration("3") True Comments of an iteration will be stripped. >>> comm.iterations.get("1").comments ['Some', 'random comments'] >>> comm.iterations.get("3").comments [] >>> os.remove(comm.iterations.get_iteration_dict()["3"]) If the iteration template does not exist, a :class:`~lasif.LASIFNotFoundError` will be raised. >>> comm.iterations.create_successive_iteration("99", "100") Traceback (most recent call last): ... LASIFNotFoundError: ... A ``ValueError`` will be raised if the new iteration already exists. >>> comm.iterations.create_successive_iteration("1", "2") Traceback (most recent call last): ... ValueError: ... """ it_dict = self.get_iteration_dict() if existing_iteration_name not in it_dict: msg = "Iteration %s does not exists." % existing_iteration_name raise LASIFNotFoundError(msg) if new_iteration_name in it_dict: msg = "Iteration %s already exists." % new_iteration_name raise ValueError(msg) from lasif.iteration_xml import Iteration existing_iteration = Iteration( it_dict[existing_iteration_name], stf_fct=self.comm.project.get_project_function( "source_time_function")) # Clone the old iteration, delete any comments and change the name. existing_iteration.comments = [] existing_iteration.iteration_name = new_iteration_name self.save_iteration(existing_iteration)
def create_successive_iteration(self, existing_iteration_name, new_iteration_name, create_folders=True): """ Create an iteration based on an existing one. It will take all settings in one iteration and transfers them to another iteration. Any comments will be deleted. :param existing_iteration_name: Name of the iteration to be used as a template. :param new_iteration_name: Name of the new iteration. :param create_folders: Create the folders for the next iteration's synthetic waveforms Note that the ``create_folders=False`` argument is only used here for testing purposes. In most cases you will want this to be ``True``. >>> comm = getfixture('iterations_comm') >>> comm.iterations.has_iteration("3") False >>> comm.iterations.create_successive_iteration("1", "3", ... create_folders=False) >>> comm.iterations.has_iteration("3") True Comments of an iteration will be stripped. >>> comm.iterations.get("1").comments ['Some', 'random comments'] >>> comm.iterations.get("3").comments [] >>> os.remove(comm.iterations.get_iteration_dict()["3"]) If the iteration template does not exist, a :class:`~lasif.LASIFNotFoundError` will be raised. >>> comm.iterations.create_successive_iteration("99", "100") Traceback (most recent call last): ... LASIFNotFoundError: ... A ``ValueError`` will be raised if the new iteration already exists. >>> comm.iterations.create_successive_iteration("1", "2") Traceback (most recent call last): ... ValueError: ... """ it_dict = self.get_iteration_dict() if existing_iteration_name not in it_dict: msg = "Iteration %s does not exists." % existing_iteration_name raise LASIFNotFoundError(msg) if new_iteration_name in it_dict: msg = "Iteration %s already exists." % new_iteration_name raise ValueError(msg) from lasif.iteration_xml import Iteration existing_iteration = Iteration( it_dict[existing_iteration_name], stf_fct=self.comm.project.get_project_function( "source_time_function")) # Clone the old iteration, delete any comments and change the name. existing_iteration.comments = [] existing_iteration.iteration_name = new_iteration_name self.save_iteration(existing_iteration) if create_folders: self.create_synthetics_folder_for_iteration(new_iteration_name) self.create_stf_folder_for_iteration(new_iteration_name)
def test_reading_iteration_xml(): """ Tests the reading of an IterationXML file to the internal representation. """ iteration = Iteration(os.path.join(data_dir, "iteration_example.xml"), stf_fct=__stf_fct_dummy) # The name is always dependent on the filename assert iteration.iteration_name == "iteration_example" assert iteration.description == "Some description" assert iteration.comments == ["Comment 1", "Comment 2", "Comment 3"] assert iteration.data_preprocessing == { "highpass_period": 100.0, "lowpass_period": 8.0 } assert iteration.rejection_criteria == { "minimum_trace_length_in_s": 500.0, "signal_to_noise": { "test_interval_from_origin_in_s": 100.0, "max_amplitude_ratio": 100.0 } } # Test some settings. The rest should work just fine as this is parsed # by useing a recursive dictionary. solver_params = iteration.solver_settings assert solver_params["solver"] == "SES3D 4.1" solver_settings = solver_params["solver_settings"] assert solver_settings["simulation_parameters"] == { "number_of_time_steps": 4000, "time_increment": 0.13, "is_dissipative": True } # Small type check. assert isinstance( solver_settings["simulation_parameters"]["number_of_time_steps"], int) # Assert the events and stations up to a certain extend. assert len(iteration.events) == 2 assert "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15" in iteration.events assert "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11" in iteration.events event_1 = iteration.events["GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15"] assert event_1["event_weight"] == 1.0 assert event_1["time_correction_in_s"] == 0.0 stations = event_1["stations"] assert len(stations) == 8 assert sorted(stations.keys()) == \ sorted(["HL.ARG", "IU.ANTO", "GE.ISP", "HL.RDO", "HT.SIGR", "GE.SANT", "HT.ALN", "HL.SANT"]) assert set([_i["station_weight"] for _i in stations.values()]) == \ set([1.0]) assert set([_i["time_correction_in_s"] for _i in stations.values()]) == \ set([0.0]) # Test reading of comments for single events and stations. event_with_comments = iteration.events[ "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11"] assert event_with_comments["comments"] == [ "This is some event, I tell you.", "Another comment just to test that multiple ones work." ] station_with_comments = event_with_comments["stations"]["GE.APE"] assert station_with_comments["comments"] == [ "Stations can also have comments!", "Who would have known?" ]