def test_overlap_component(simple_model_overlap, simple_model_alias_overlap): """Test if model builder detects the overlap component correctly.""" mb = ModelBuilder() # Test overlap overlap = mb._get_component_param_overlap(simple_model_overlap) assert "Spindown" in overlap.keys() assert overlap["Spindown"][0] == set(["F0"]) # Only one over lap parameter F0 # Since the _get_component_param_overlap returns non-overlap part, # we test if the non-overlap number makes sense. assert overlap["Spindown"][1] == len(simple_model_overlap.params) - 1 assert (overlap["Spindown"][2] == len( mb.all_components.components["Spindown"].params) - 1) a_overlap = mb._get_component_param_overlap(simple_model_alias_overlap) assert a_overlap["Spindown"][0] == set(["F0"]) assert a_overlap["Spindown"][1] == len( simple_model_alias_overlap.params) - 1 assert (a_overlap["Spindown"][2] == len( mb.all_components.components["Spindown"].params) - 1) assert a_overlap["AstrometryEcliptic"][0] == set(["ELONG"]) assert (a_overlap["AstrometryEcliptic"][1] == len( simple_model_alias_overlap.params) - 1) assert (a_overlap["AstrometryEcliptic"][2] == len( mb.all_components.components["AstrometryEcliptic"].params) - 1)
def test_pintify_parfile_repeat_line_with_repeat_param(): repeat_par = base_par + "JUMP -fe 1 1 3\nJUMP -fe 2 1 3" repeat_par += "\nECORR -fe 1 1 2\nECORR -fe 2 1 2" m = ModelBuilder() pint_dict, original_name, unknow = m._pintify_parfile(StringIO(repeat_par)) assert len(pint_dict["JUMP1"]) == 2 assert len(pint_dict["ECORR1"]) == 2
def test_model_fillup(test_timing_model): """Test model value fill up""" mb = ModelBuilder() tm = mb._setup_model(test_timing_model, pint_dict_base, validate=False) assert tm.PSR.value == "J1234+5678" assert np.isclose(tm.F0.value, 173.6879489990983) assert np.isclose(tm.F0.uncertainty_value, 3.000e-13) assert tm.DMX_0001.value == 3.01718358e-03 assert tm.DMX_0001.uncertainty_value == 3.89019948e-05 jump_map = tm.get_prefix_mapping("JUMP") assert len(jump_map.keys()) == len(pint_dict_base["JUMP1"]) assert tm.JUMP1.key == "-fe" assert tm.JUMP1.key_value == ["L-wide"] assert tm.JUMP1.value == 1 assert tm.JUMP2.key == "-fe" assert tm.JUMP2.key_value == ["430"] assert tm.JUMP2.value == 1 assert tm.JUMP3.key == "-fe1" assert tm.JUMP3.key_value == ["430"] assert tm.JUMP3.value == 1 efac = tm.get_prefix_mapping("EFAC") assert len(efac.keys()) == len(pint_dict_base["EFAC1"]) assert tm.EFAC1.key == "-f" assert tm.EFAC1.key_value == ["L-wide_PUPPI"] assert tm.EFAC1.value == 1.156 assert tm.EFAC2.key == "-f" assert tm.EFAC2.key_value == ["430_ASP"] assert tm.EFAC2.value == 0.969 equad = tm.get_prefix_mapping("EQUAD") assert len(equad.keys()) == len(pint_dict_base["EQUAD1"])
def test_parse_parfile_index_param(): indexed_par = base_par + "DMX_0001 1 1 2\nDMX_0025 2 1 3" m = ModelBuilder() pint_dict, original_name, unknow = m._pintify_parfile( StringIO(indexed_par)) assert pint_dict["DMX_0001"] == ["1 1 2"] assert pint_dict["DMX_0025"] == ["2 1 3"]
def test_subset_component(sub_set_model): """Test if model builder detects the subset component. """ mb = ModelBuilder() # Test subset superset = mb._is_subset_component(sub_set_model) assert superset == "Spindown"
def test_pintify_parfile_alises(): aliases_par = base_par.replace("ELONG", "LAMBDA") m = ModelBuilder() pint_dict, original_name, unknow = m._pintify_parfile( StringIO(aliases_par)) assert pint_dict["ELONG"] == ["0 1 3"] assert len(unknow) == 0 assert "LAMBDA" not in pint_dict.keys()
def test_model_fillup_prefix_adding(test_timing_model): pint_dict_prefix = copy.deepcopy(pint_dict_base) pint_dict_prefix["DMX_0002"] = ["5.01718358D-03 1 3.89019948D-05"] pint_dict_prefix["DMX_0345"] = ["3.01718358D-03 1 5.89019948D-05"] mb = ModelBuilder() tm = mb._setup_model(test_timing_model, pint_dict_prefix, validate=False) assert np.isclose(tm.DMX_0002.value, 5.01718358e-03) assert np.isclose(tm.DMX_0002.uncertainty_value, 3.89019948e-05) assert np.isclose(tm.DMX_0345.value, 3.01718358e-03) assert np.isclose(tm.DMX_0345.uncertainty_value, 5.89019948e-05)
def test_pintify_parfile_repeat_line_mix_alises(): repeat_par = base_par + "EQUAD -fe 1 1 3\nT2EQUAD -fe 2 1 3\nEQUAD -fe 4 2 2" m = ModelBuilder() with pytest.raises(TimingModelError): m._pintify_parfile(StringIO(repeat_par)) pint_dict, original_name, unknow = m._pintify_parfile( StringIO(repeat_par), allow_name_mixing=True) # The list order is swaped since parse_parfile will pack EQUADs first and # then the pintify will add T2ECORR to the list. assert pint_dict["EQUAD1"] == ["-fe 1 1 3", "-fe 4 2 2", "-fe 2 1 3"] assert "T2EQUAD" not in pint_dict.keys()
def test_model_fillup_prefix_adding_spin_freq(test_timing_model): pint_dict_prefix = copy.deepcopy(pint_dict_base) pint_dict_prefix["F1"] = ["5.0D-11 1 3.0e-15"] pint_dict_prefix["F2"] = ["5.0D-13 1 3.0e-15"] pint_dict_prefix["F3"] = ["3.0D-14 1 5.0e-15"] mb = ModelBuilder() tm = mb._setup_model(test_timing_model, pint_dict_prefix) assert np.isclose(tm.F2.value, 5.0e-13) assert np.isclose(tm.F2.uncertainty_value, 3.0e-15) assert np.isclose(tm.F3.value, 3.0e-14) assert np.isclose(tm.F3.uncertainty_value, 5.0e-15)
def test_model_from_par_hassubset(): """Test Get model from test par file with a subset component.""" # Define a subset parameter model that is registered. So the metaclass can # catch it. class SubsetModel2(PhaseComponent): """Test model component hosting the parameters which are a subset of spindown.""" register = True category = "simple_test" def __init__(self): super(SubsetModel2, self).__init__() self.add_param(floatParameter(name="F0", value=0.0, unit="1/s")) with pytest.raises(ComponentConflict): mb = ModelBuilder() # Have to remove the SubsetModel2, since it will fail other tests. del Component.component_types["SubsetModel2"]
def test_model_from_par(): """Test Get model from test par file.""" test_par1 = """ PSR B1855+09 LAMBDA 286.8634893301156 1 0.0000000165859 BETA 32.3214877555037 1 0.0000000273526 PMLAMBDA -3.2701 1 0.0141 PMBETA -5.0982 1 0.0291 PX 0.2929 1 0.2186 F0 186.4940812707752116 1 0.0000000000328468 F1 -6.205147513395D-16 1 1.379566413719D-19 PEPOCH 54978.000000 START 53358.726 FINISH 56598.873 DM 13.299393 DMX 14.000000 DMX_0001 1.51618630D-02 1 3.51684846D-03 DMXEP_0001 53358.72746 DMXR1_0001 53358.72746 DMXR2_0001 53358.77841 DMXF1_0001 424.000 DMXF2_0001 1442.000 DMX_0002 1.52370685D-02 1 3.51683449D-03 DMXEP_0002 53420.54893 DMXR1_0002 53420.54893 DMXR2_0002 53420.58620 DMXF1_0002 424.000 DMXF2_0002 1442.000 FD1 1.61666384D-04 1 3.38650356D-05 FD2 -1.88210030D-04 1 4.13173074D-05 SOLARN0 0.00 EPHEM DE421 ECL IERS2003 CLK TT(BIPM) UNITS TDB TIMEEPH FB90 T2CMETHOD TEMPO CORRECT_TROPOSPHERE N PLANET_SHAPIRO N #DILATEFREQ N NTOA 4005 TRES 5.52 TZRMJD 54981.28084616488447 TZRFRQ 424.000 TZRSITE 3 NITS 1 INFO -f BINARY DD A1 9.230780480 1 0.000000203 E 0.0000216340 1 0.0000000236 T0 54975.5128660817 1 0.0019286695 PB 12.32717119132762 1 0.00000000019722 OM 276.536118059963 1 0.056323656112 SINI 0.999461 1 0.000178 M2 0.233837 1 0.011278 RNAMP 0.17173D-01 RNIDX -4.91353 TNRedAmp -14.227505410948254 TNRedGam 4.91353 TNRedC 45 T2EFAC -f L-wide_PUPPI 1.507 T2EFAC -f 430_ASP 1.147 T2EQUAD -f L-wide_PUPPI 0.25518 T2EQUAD -f 430_ASP 0.01410 ECORR -f 430_PUPPI 0.00601 ECORR -f L-wide_PUPPI 0.31843 JUMP -fe L-wide -0.000009449 1 0.000009439 """ mb = ModelBuilder() param_inpar, original_name, unknown = mb._pintify_parfile( io.StringIO(test_par1)) # valid_param_inline = [] # for l in test_par1.split("\n"): # l = l.strip() # if not (l.startswith("#") or l == ""): # valid_param_inline.append(l.split()[0]) # assert set(repeat) == {"JUMP", "ECORR", "T2EQUAD", "T2EFAC"} comps, conflict, _ = mb.choose_model(param_inpar) assert comps == { "Spindown", "FD", "AbsPhase", "ScaleToaError", "TroposphereDelay", "PhaseJump", "DispersionDMX", "AstrometryEcliptic", "BinaryDD", "DispersionDM", "EcorrNoise", "SolarWindDispersion", "PLRedNoise", "SolarSystemShapiro", } tm = mb(io.StringIO(test_par1)) assert len(tm.get_prefix_mapping("EQUAD")) == 2 assert len(tm.get_prefix_mapping("EFAC")) == 2 assert len(tm.get_prefix_mapping("JUMP")) == 1
def test_pintify_parfile_unrecognize(): wrong_par = base_par + "UNKNOWN 2 1 1" m = ModelBuilder() pint_dict, original_name, unknow = m._pintify_parfile(StringIO(wrong_par)) assert unknow == {"UNKNOWN": ["2 1 1"]}
def test_pintify_parfile_duplicate_binary(): dup_par = base_par + "BINARY DD\nBINARY BT" m = ModelBuilder() with pytest.raises(TimingModelError): m._pintify_parfile(StringIO(dup_par))
def test_pintify_parfile_from_parfile_dict(): m = ModelBuilder() par_dict = parse_parfile(StringIO(base_par)) pint_dict, original_name, unknow = m._pintify_parfile(par_dict) assert len(pint_dict) == len((base_par.strip()).split("\n")) assert len(unknow) == 0
def test_pintify_parfile_repeat_line_with_nonrepeat_param(): repeat_par = base_par + "DM 1 1 3\nF0 2 1 3" m = ModelBuilder() with pytest.raises(TimingModelError): m._pintify_parfile(StringIO(repeat_par))
def test_pintify_parfile_repeat_line_with_repeat_aliases(): repeat_par = base_par + "T2EQUAD -fe 1 1 3\nT2EQUAD -fe 2 1 3" m = ModelBuilder() pint_dict, original_name, unknow = m._pintify_parfile(StringIO(repeat_par)) assert len(pint_dict["EQUAD1"]) == 2 assert "T2EQUAD" not in pint_dict.keys()
def test_pintify_parfile_nonrepeat_with_alise_repeating(): aliases_par = base_par + "LAMBDA 2 1 3" m = ModelBuilder() with pytest.raises(TimingModelError): m._pintify_parfile(StringIO(aliases_par))