Skip to content

Commit

Permalink
change key, key_value to flag, flag_value
Browse files Browse the repository at this point in the history
  • Loading branch information
aarchiba committed Aug 23, 2021
1 parent 46e10a2 commit 4f8d331
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 374 deletions.
34 changes: 17 additions & 17 deletions src/pint/models/jump.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class PhaseJump(PhaseComponent):
>>> for i in toa_index_list:
... toas.table['flags'][i]['fish'] = 'carp'
>>> np = m.JUMP1.new_param(100)
>>> np.key = '-fish'
>>> np.key_value = 'carp'
>>> np.flag = '-fish'
>>> np.flag_value = 'carp'
>>> m.add_param_from_top(np, "PhaseJump")
More briefly, you could use
``m.add_jump_and_flags(toas.table['flags'][1,3,5], key='-fish', key_value='carp')``,
``m.add_jump_and_flags(toas.table['flags'][1,3,5], flag='-fish', flag_value='carp')``,
which adds the flag ``-fish`` with the value ``carp`` to TOAs numbers 1,3, and 5,
and also creates a new JUMP affecting those TOAs.
Expand Down Expand Up @@ -176,7 +176,7 @@ def print_par(self):
result += jump_par.as_parfile_line()
return result

def add_jump_and_flags(self, toa_flags, key="gui_jump", key_value=None):
def add_jump_and_flags(self, toa_flags, flag="gui_jump", flag_value=None):
"""Add jump object to PhaseJump and appropriate flags to TOA tables.
Given a subset of TOAs (specified by a reference to their flags objects),
Expand All @@ -196,9 +196,9 @@ def add_jump_and_flags(self, toa_flags, key="gui_jump", key_value=None):
toa_flags: array of dict
The TOA flags which must be modified. In pintk (pulsar.py), this will
be all_toas.table["flags"][selected]
key: str
flag: str
The name of the flag to use for the JUMP.
key_value: str or None
flag_value: str or None
The flag value to associate with this JUMP; if not specified, find the first
integer N not associated with a JUMP and use its string representation.
Expand All @@ -209,17 +209,17 @@ def add_jump_and_flags(self, toa_flags, key="gui_jump", key_value=None):
"""
in_use = set()
for pm in self.jumps:
if pm.key == "-" + key:
in_use.add(pm.key_value)
if key_value is None:
if pm.flag == "-" + flag:
in_use.add(pm.flag_value)
if flag_value is None:
i = 1
while True:
key_value = str(i)
if key_value not in in_use:
flag_value = str(i)
if flag_value not in in_use:
break
i += 1
elif key_value in in_use:
raise ValueError(f"A JUMP -{key} {key_value} is already present.")
elif flag_value in in_use:
raise ValueError(f"A JUMP -{flag} {flag_value} is already present.")

used_indices = set()
for pm in self.jumps:
Expand All @@ -231,15 +231,15 @@ def add_jump_and_flags(self, toa_flags, key="gui_jump", key_value=None):
param = maskParameter(
name="JUMP",
index=i,
key="-" + key,
key_value=key_value,
flag="-" + flag,
flag_value=flag_value,
value=0.0,
units="second",
frozen=False,
)
name = param.name
for d in toa_flags:
if key in d:
if flag in d:
raise ValueError(
"The selected toa(s) overlap an existing jump. Remove all "
"interfering jumps before attempting to jump these toas."
Expand All @@ -248,7 +248,7 @@ def add_jump_and_flags(self, toa_flags, key="gui_jump", key_value=None):
self.setup()
# add appropriate flags to TOA table to link jump with appropriate TOA
for d in toa_flags:
d[key] = key_value
d[flag] = flag_value
return name

def tidy_jumps_for_fit(self, toas):
Expand Down
40 changes: 20 additions & 20 deletions src/pint/models/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,32 @@ def setup(self):
for mask_par in self.get_params_of_type("maskParameter"):
if mask_par.startswith("EFAC"):
par = getattr(self, mask_par)
self.EFACs[mask_par] = (par.key, par.key_value)
self.EFACs[mask_par] = (par.flag, par.flag_value)
elif mask_par.startswith("EQUAD"):
par = getattr(self, mask_par)
self.EQUADs[mask_par] = (par.key, par.key_value)
self.EQUADs[mask_par] = (par.flag, par.flag_value)
elif mask_par.startswith("TNEQ"):
par = getattr(self, mask_par)
self.TNEQs[mask_par] = (par.key, par.key_value)
self.TNEQs[mask_par] = (par.flag, par.flag_value)
else:
continue
# convert all the TNEQ to EQUAD

for tneq in self.TNEQs:
tneq_par = getattr(self, tneq)
if tneq_par.key is None:
if tneq_par.flag is None:
continue
if self.TNEQs[tneq] in list(self.EQUADs.values()):
log.warning(
"'%s %s %s' is provided by parameter EQUAD, using"
" EQUAD instead. " % (tneq, tneq_par.key, tneq_par.key_value)
" EQUAD instead. " % (tneq, tneq_par.flag, tneq_par.flag_value)
)
else:
EQUAD_name = "EQUAD" + str(tneq_par.index)
if EQUAD_name in list(self.EQUADs.keys()):
if EQUAD_name in list(self.EQUADs.flags()):
EQUAD_par = getattr(self, EQUAD_name)
EQUAD_par.key = tneq_par.key
EQUAD_par.key_value = tneq_par.key_value
EQUAD_par.flag = tneq_par.flag
EQUAD_par.flag_value = tneq_par.flag_value
EQUAD_par.quantity = tneq_par.quantity.to(u.us)
else:
self.add_param(
Expand All @@ -131,21 +131,21 @@ def setup(self):
)
)
EQUAD_par = getattr(self, EQUAD_name)
EQUAD_par.key = tneq_par.key
EQUAD_par.key_value = tneq_par.key_value
EQUAD_par.flag = tneq_par.flag
EQUAD_par.flag_value = tneq_par.flag_value
EQUAD_par.quantity = tneq_par.quantity.to(u.us)
for pp in self.params:
if pp.startswith("EQUAD"):
par = getattr(self, pp)
self.EQUADs[pp] = (par.key, par.key_value)
self.EQUADs[pp] = (par.flag, par.flag_value)

def validate(self):
super().validate()
# check duplicate
for el in ["EFACs", "EQUADs"]:
l = list(getattr(self, el).values())
if [x for x in l if l.count(x) > 1] != []:
raise ValueError("'%s' have duplicated keys and key values." % el)
raise ValueError("'%s' have duplicated flags and flag values." % el)

def scale_toa_sigma(self, toas):
sigma_scaled = toas.table["error"].quantity.copy()
Expand Down Expand Up @@ -222,12 +222,12 @@ def setup(self):
for mask_par in self.get_params_of_type("maskParameter"):
if mask_par.startswith("DMEFAC"):
par = getattr(self, mask_par)
if par.key is not None:
self.DMEFACs[mask_par] = (par.key, tuple(par.key_value))
if par.flag is not None:
self.DMEFACs[mask_par] = (par.flag, tuple(par.flag_value))
elif mask_par.startswith("DMEQUAD"):
par = getattr(self, mask_par)
if par.key is not None:
self.DMEQUADs[mask_par] = (par.key, tuple(par.key_value))
if par.flag is not None:
self.DMEQUADs[mask_par] = (par.flag, tuple(par.flag_value))
else:
continue

Expand All @@ -242,7 +242,7 @@ def validate(self):
for el in ["DMEFACs", "DMEQUADs"]:
l = list(getattr(self, el).values())
if [x for x in l if l.count(x) > 1] != []:
raise ValueError("'%s' have duplicated keys and key values." % el)
raise ValueError("'%s' have duplicated flags and flag values." % el)

def scale_dm_sigma(self, toas):
"""
Expand Down Expand Up @@ -319,7 +319,7 @@ def setup(self):
for mask_par in self.get_params_of_type("maskParameter"):
if mask_par.startswith("ECORR"):
par = getattr(self, mask_par)
self.ECORRs[mask_par] = (par.key, par.key_value)
self.ECORRs[mask_par] = (par.flag, par.flag_value)
else:
continue

Expand All @@ -330,11 +330,11 @@ def validate(self):
for el in ["ECORRs"]:
l = list(getattr(self, el).values())
if [x for x in l if l.count(x) > 1] != []:
raise ValueError("'%s' have duplicated keys and key values." % el)
raise ValueError("'%s' have duplicated flags and flag values." % el)

def get_ecorrs(self):
ecorrs = []
for ecorr, ecorr_key in list(self.ECORRs.items()):
for ecorr in self.ECORRs:
ecorrs.append(getattr(self, ecorr))
return ecorrs

Expand Down
Loading

0 comments on commit 4f8d331

Please sign in to comment.