Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions ch_util/_db_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def set_user(u):
# Find the user.
if isinstance(u, int):
q = chimedb.core.proxy.execute_sql(
"SELECT user_id FROM chimewiki.user WHERE user_id = %d;" % u
f"SELECT user_id FROM chimewiki.user WHERE user_id = {u};"
)
else:
q = chimedb.core.proxy.execute_sql(
Expand Down Expand Up @@ -359,7 +359,7 @@ def start(self, time=datetime.datetime.now(), notes=None):
e = event.create(
graph_obj=o, type=event_type.global_flag(), start=start, end=None
)
logger.info("Created global flag as event %d." % e.id)
logger.info(f"Created global flag as event {e.id}.")
return g

def end(self, time=datetime.datetime.now(), notes=None):
Expand Down Expand Up @@ -649,7 +649,7 @@ def add_history(self, notes, time=datetime.datetime.now(), timestamp_notes=None)
h = component_history.create(id=o, comp=self, notes=notes)
t_stamp = timestamp.create(time=time, notes=timestamp_notes)
e = event.create(graph_obj=o, type=event_type.comp_history(), start=t_stamp)
logger.info("Added component history as event %d." % e.id)
logger.info(f"Added component history as event {e.id}.")
return h

def add_doc(self, repo, ref, time=datetime.datetime.now(), notes=None):
Expand Down Expand Up @@ -681,7 +681,7 @@ def add_doc(self, repo, ref, time=datetime.datetime.now(), notes=None):
d = component_doc.create(id=o, comp=self, repo=repo, ref=ref)
t_stamp = timestamp.create(time=time, notes=notes)
e = event.create(graph_obj=o, type=event_type.comp_doc(), start=t_stamp)
logger.info("Added component document as event %d." % e.id)
logger.info(f"Added component document as event {e.id}.")
return d

def get_property(self, type=None, time=datetime.datetime.now()):
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def deactivate(self):
fail = []

if not self.active:
logger.info("Event %d is already deactivated." % (self.id))
logger.info(f"Event {self.id} is already deactivated.")
return

# If this is about component availability, do not deactivate if it is
Expand Down Expand Up @@ -1278,7 +1278,7 @@ def deactivate(self):

self.active = False
self.save()
logger.info("Deactivated event %d." % self.id)
logger.info(f"Deactivated event {self.id}.")

def _replace(self, start=None, end=None, force_end=False):
"""Replace one or both timestamps for an event.
Expand Down Expand Up @@ -1474,7 +1474,7 @@ def _graph_obj_iter(sel, obj, time, when, order, active):
elif order == ORDER_DESC:
ret = ret.order_by(timestamp.time.desc())
else:
raise ValueError("Unknown value of 'when' passed (%d)." % when)
raise ValueError(f"Unknown value of 'when' passed ({when}).")

return ret

Expand Down Expand Up @@ -1668,18 +1668,16 @@ def add_component(comp, time=datetime.datetime.now(), notes=None, force=False):
time, event_type.comp_avail(), EVENT_AFTER, ORDER_ASC
).get()
e_old._replace(start=t_stamp)
logger.debug(
"Added %s by replacing previous event %d." % (comp.sn, e_old.id)
)
logger.debug(f"Added {comp.sn} by replacing previous event {e_old.id}.")
except pw.DoesNotExist:
e = event.create(
graph_obj=comp.id, type=event_type.comp_avail(), start=t_stamp
)
logger.debug("Added %s with new event %d." % (comp.sn, e.id))
logger.debug(f"Added {comp.sn} with new event {e.id}.")
if len(to_add):
logger.info(
"Added %d new component%s: %s"
% (len(to_add), _plural(to_add), ", ".join(to_add_sn))
f"Added {len(to_add)} new component{_plural(to_add)}: "
+ ", ".join(to_add_sn)
)
else:
logger.info("Added no new component.")
Expand Down Expand Up @@ -1823,11 +1821,10 @@ def remove_component(comp, time=datetime.datetime.now(), notes=None, force=False
for e in ev:
e.end = t_stamp
e.save()
logger.debug("Removed component by ending event %d." % e.id)
logger.debug(f"Removed component by ending event {e.id}.")
if len(ev):
logger.info(
"Removed %d component%s: %s."
% (len(ev), _plural(ev), ", ".join(ev_comp_sn))
f"Removed {len(ev)} component{_plural(ev)}: " + ", ".join(ev_comp_sn)
)
else:
logger.info("Removed no component.")
Expand Down Expand Up @@ -1915,8 +1912,9 @@ def set_property(
fail,
force,
PropertyUnchanged,
"The following component%s "
"property does not change" % ("'s" if len(fail) == 1 else "s'"),
"The following component"
+ ("'s" if len(fail) == 1 else "s'")
+ " property does not change",
)

# End any events that need to be ended.
Expand All @@ -1930,8 +1928,9 @@ def set_property(
# If no value was passed, then we are done.
if not value:
logger.info(
"Removed property %s from the following %d component%s: %s."
% (type.name, len(to_end), _plural(to_end), ", ".join(to_end_sn))
f"Removed property {type.name} from the following {len(to_end)} "
+ f"component{_plural(to_end)}: "
+ ", ".join(to_end_sn)
)
return

Expand Down Expand Up @@ -2014,7 +2013,7 @@ def make_connexion(
# starting at this new time.
e_old = c.event(time, EVENT_AFTER, ORDER_ASC).get()
e_old._replace(start=t_stamp)
logger.debug("Added connexion by replacing previous event %d." % e_old.id)
logger.debug(f"Added connexion by replacing previous event {e_old.id}.")
except pw.DoesNotExist:
try:
conn = connexion.from_pair(c.comp1, c.comp2, allow_new=False)
Expand All @@ -2027,11 +2026,11 @@ def make_connexion(
else:
e_type = event_type.connexion()
e = event.create(graph_obj=o, type=e_type, start=t_stamp)
logger.debug("Added connexion with new event %d." % e.id)
logger.debug(f"Added connexion with new event {e.id}.")
if len(to_conn):
logger.info(
"Added %d new connexion%s: %s"
% (len(to_conn), _plural(to_conn), ", ".join(to_conn_sn))
f"Added {len(to_conn)} new connexion{_plural(to_conn)}: "
+ ", ".join(to_conn_sn)
)
else:
logger.info("Added no new connexions.")
Expand Down Expand Up @@ -2109,11 +2108,10 @@ def sever_connexion(conn, time=datetime.datetime.now(), notes=None, force=False)
for e in ev:
e.end = t_stamp
e.save()
logger.debug("Severed connexion by ending event %d." % e.id)
logger.debug(f"Severed connexion by ending event {e.id}.")
if len(ev):
logger.info(
"Severed %d connexion%s: %s."
% (len(ev), _plural(ev), ", ".join(ev_conn_sn))
f"Severed {len(ev)} connexion{_plural(ev)}: " + ", ".join(ev_conn_sn)
)
else:
logger.info("Severed no connexion.")
11 changes: 4 additions & 7 deletions ch_util/andata.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ def _find_mux(self, mux):
for dummy, d in self.datasets.items():
if d.attrs["mux_address"] == mux:
return d
raise ValueError("No dataset with mux = %d is present." % (mux))
raise ValueError(f"No dataset with mux = {mux} is present.")

def chan(self, mux=-1):
"""Convenience access to the list of channels in a given mux.
Expand Down Expand Up @@ -1062,7 +1062,7 @@ def tod(self, chan, mux=-1):
try:
idx = list(self.index_map[chan_map]).index(chan)
except KeyError:
raise ValueError("No channel %d exists for mux %d." % (chan, mux))
raise ValueError(f"No channel {chan} exists for mux {mux}.")

# Return the data.
return ds[idx, :]
Expand Down Expand Up @@ -3381,8 +3381,7 @@ def _remap_crate_corr(afile, slot):
]

# Create new list of serials
serial_pat = crate_serial + ("%02i%%02i" % int(slot))
serials = [serial_pat % ci for ci in chanlist]
serials = [f"{crate_serial}{slot:02}{ci:02}" for ci in chanlist]

# Create a list of channel ids (taking into account that they are
# meaningless for the old crate)
Expand Down Expand Up @@ -3517,9 +3516,7 @@ def _insert_gains(data, input_sel):
elif key in data.history["acq"]:
g_data = data.history["acq"][key]
else:
warnings.warn(
"Cannot find gain entry [%s] for channel %i" % (key, chan)
)
warnings.warn(f"Cannot find gain entry [{key}] for channel {chan}")
continue

# Unpack the gain values and construct the gain array
Expand Down
14 changes: 7 additions & 7 deletions ch_util/cal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def parameter_names(self):
parameter_names : np.ndarray[nparam,]
Names of the parameters.
"""
return np.array(["param%d" % p for p in range(self.nparam)], dtype=np.bytes_)
return np.array([f"param{p}" for p in range(self.nparam)], dtype=np.bytes_)

@property
def param_corr(self):
Expand Down Expand Up @@ -753,8 +753,8 @@ def ndofi(self):
def parameter_names(self):
"""Array of strings containing the name of the fit parameters."""
return np.array(
["%s_poly_real_coeff%d" % (self.poly_type, p) for p in range(self.nparr)]
+ ["%s_poly_imag_coeff%d" % (self.poly_type, p) for p in range(self.npari)],
[f"{self.poly_type}_poly_real_coeff{p}" for p in range(self.nparr)]
+ [f"{self.poly_type}_poly_imag_coeff{p}" for p in range(self.npari)],
dtype=np.bytes_,
)

Expand Down Expand Up @@ -1144,8 +1144,8 @@ def ndofp(self):
def parameter_names(self):
"""Array of strings containing the name of the fit parameters."""
return np.array(
["%s_poly_amp_coeff%d" % (self.poly_type, p) for p in range(self.npara)]
+ ["%s_poly_phi_coeff%d" % (self.poly_type, p) for p in range(self.nparp)],
[f"{self.poly_type}_poly_amp_coeff{p}" for p in range(self.npara)]
+ [f"{self.poly_type}_poly_phi_coeff{p}" for p in range(self.nparp)],
dtype=np.bytes_,
)

Expand Down Expand Up @@ -1421,7 +1421,7 @@ def parameter_names(self):
"""Array of strings containing the name of the fit parameters."""
return np.array(
["peak_amplitude", "centroid", "fwhm"]
+ ["%s_poly_phi_coeff%d" % (self.poly_type, p) for p in range(self.nparp)],
+ [f"{self.poly_type}_poly_phi_coeff{p}" for p in range(self.nparp)],
dtype=np.bytes_,
)

Expand Down Expand Up @@ -1695,7 +1695,7 @@ def fit_point_source_map(
absolute_sigma=True,
) # , bounds=bounds)
except ValueError as error:
print("index (" + ", ".join(["%d" % ii for ii in index]) + "):", error)
print("index (" + ", ".join([str(ii) for ii in index]) + "):", error)
continue

# Save the results
Expand Down
78 changes: 37 additions & 41 deletions ch_util/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import time
import socket
import peewee as pw
import tabulate

import caput.time as ctime

Expand Down Expand Up @@ -144,22 +145,24 @@ class Finder:
>>> from datetime import datetime
>>> f = finder.Finder()
>>> f.only_corr()
>>> f.set_time_range(datetime(2014,02,24), datetime(2014,02,25))
>>> f.set_time_range(datetime(2014,2,24), datetime(2014,2,25))
>>> f.print_results_summary()
interval | acquisition | offset from start (s) | length (s) | N files
1 | 20140219T145849Z_abbot_corr | 378053.1 | 86400.0 | 25
2 | 20140224T051212Z_stone_corr | 0.0 | 67653.9 | 19
Total 154053.858720 seconds of data.
# acquisition start (s) len (s) files MB
--- --------------------------- ----------- --------- ------- -------
0 20140219T145849Z_abbot_corr 378053 86400 25 3166.08
1 20140224T051212Z_stone_corr 0 67653.9 19 2406.78
Total 154054 seconds, 5573 MB of data.

Search for transits of a given source.

>>> import ch_ephem.sources
>>> f.include_transits(ch_ephem.sources.CasA, time_delta=3600)
>>> f.print_results_summary()
interval | acquisition | offset from start (s) | length (s) | N files
1 | 20140219T145849Z_abbot_corr | 452087.2 | 3600.0 | 2
2 | 20140224T051212Z_stone_corr | 55288.0 | 3600.0 | 2
Total 7200.000000 seconds of data.
# acquisition start (s) len (s) files MB
--- --------------------------- ----------- --------- ------- -------
0 20140219T145849Z_abbot_corr 452092 3600 2 253.286
1 20140224T051212Z_stone_corr 55292.9 3600 2 253.346
Total 7200 seconds, 507 MB of data.

To read the data,

Expand All @@ -183,50 +186,45 @@ class Finder:
... & (di.CorrAcqInfo.nprod == 36)
... & (di.ArchiveInst.name == 'stone'))
>>> f.print_results_summary()
interval | acquisition | offset from start (s) | length (s) | N files
1 | 20140211T020307Z_stone_corr | 0.0 | 391.8 | 108
2 | 20140128T135105Z_stone_corr | 0.0 | 4165.2 | 104
3 | 20131208T070336Z_stone_corr | 0.0 | 1429.8 | 377
4 | 20140212T014603Z_stone_corr | 0.0 | 2424.4 | 660
5 | 20131210T060233Z_stone_corr | 0.0 | 1875.3 | 511
6 | 20140210T021023Z_stone_corr | 0.0 | 874.1 | 240
Total 11160.663510 seconds of data.
# acquisition start (s) len (s) files MB
--- --------------------------- ----------- --------- ------- --------
0 20140211T020307Z_stone_corr 0 391.764 108 13594.1
1 20140128T135105Z_stone_corr 0 4165.22 104 131711
2 20131208T070336Z_stone_corr 0 1429.78 377 47676.4
3 20140212T014603Z_stone_corr 0 2424.43 660 83604.1
4 20131210T060233Z_stone_corr 0 1875.32 511 64704.8
5 20140210T021023Z_stone_corr 0 874.144 240 30286.4
Total 11161 seconds, 371577 MB of data.


Here is an example that uses node spoofing and also filters files within
acquisitions to include only LNA housekeeping files:

>>> f = finder.Finder(node_spoof = {"gong" : "/mnt/gong/archive",
"suzu" : "/mnt/suzu/hk_data"})
>>> f = finder.Finder(node_spoof={"scinet_hpss": "/dev/null"})
>>> f.only_hk()
>>> f.set_time_range(datetime(2014, 9, 1), datetime(2014, 10, 10))
>>> f.print_results_summary()
# | acquisition |start (s)| len (s) |files | MB
0 | 20140830T005410Z_ben_hk | 169549 | 419873 | 47 | 2093
1 | 20140905T203905Z_ben_hk | 0 | 16969 | 2 | 0
2 | 20140908T153116Z_ben_hk | 0 | 1116260 | 56 | 4
3 | 20141009T222415Z_ben_hk | 0 | 5745 | 2 | 0
# acquisition start (s) len (s) files MB
--- ----------------------- ----------- ---------------- ------- ------------
0 20140830T005410Z_ben_hk 169549 419873 47 2093
1 20140905T203905Z_ben_hk 0 16969.1 2 0.0832596
2 20140908T153116Z_ben_hk 0 1.11626e+06 56 4.45599
3 20141009T222415Z_ben_hk 0 5744.8 2 0.191574
Total 1558847 seconds, 2098 MB of data.
>>> res = f.get_results(file_condition = (di.HKFileInfo.atmel_name == "LNA"))
>>> for r in res:
... print("No. files: %d" % (len(r[0]))
... print(f"No. files: {len(r[0])}")
...
No. files: 8
No. files: 1
No. files: 19
No. files: 1
>>> data = res[0].as_loaded_data()
>>> for m in data.mux:
... print("Mux %d: %s", (m, data.chan(m)))
Mux 0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Mux 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Mux 2: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> print(data.tod(14, 1))
[1744.190917, 1766.344726, 1771.033569, ..., 1928.612792, 1938.900756, 1945.534912]

In the above example, the restriction to LNA housekeeping could also have
been accomplished with the convenience method :meth:`Finder.set_hk_input`:

>>> f.set_hk_input("LNA")
>>> res = f.get_results()

"""

# Constructors and setup
Expand Down Expand Up @@ -1327,17 +1325,14 @@ def print_acq_info(self):
def print_results_summary(self):
"""Print a summary of the search results."""

row_proto = "%4d | %-36s | %7.f | %7.f | %4d | %6.f"
total_data = 0.0
total_size = 0.0
interval_number = 0
titles = ("# ", " acquisition", "start (s)", "len (s) ", "files ", "MB ")
print("%5s|%-38s|%9s|%9s|%6s|%8s" % titles)
titles = ("#", "acquisition", "start (s)", "len (s)", "files", "MB")
rows = []
for ii, acq in enumerate(self.acqs):
acq_start = acq.start_time
intervals = self.get_results_acq(ii)
# if len(intervals):
# print intervals[0]
for interval in intervals:
offset = interval[1][0] - acq_start
length = interval[1][1] - interval[1][0]
Expand Down Expand Up @@ -1366,11 +1361,12 @@ def print_results_summary(self):
except TypeError:
s = 0
info = (interval_number, acq.name, offset, length, n_files, s)
print(row_proto % info)
rows.append(info)
total_data += length
total_size += s
interval_number += 1
print(f"Total {total_data:6.f} seconds, {total_size:6.f} MB of data.")
print(tabulate.tabulate(rows, headers=titles))
print(f"Total {total_data:6.0f} seconds, {total_size:6.0f} MB of data.")


def _trim_intervals_range(intervals, time_range, min_interval=0.0):
Expand Down
Loading