34 def description(self, svd_bin=None, subtype=None):
38 description.append(svd_bin)
39 description.append(f
"GSTLAL_{self.name}")
41 description.append(subtype.upper())
42 return "_".join(description)
44 def filename(self, ifos, span=None, svd_bin=None, subtype=None, extension=None):
48 extension = self.extension
49 return T050017_filename(ifos, self.
description(svd_bin, subtype), span, extension)
51 def file_pattern(self, svd_bin=None, subtype=None, extension=None):
53 extension = self.extension
54 return f
"*-{self.description(svd_bin, subtype)}-*-*{extension}"
56 def directory(self, root=None, start=None):
57 path = self.name.lower()
59 path = os.path.join(root, path)
61 path = os.path.join(path, gps_directory(start))
97 cache: list = field(default_factory=list)
101 return [entry.path
for entry
in self.
cache]
104 return len(self.
cache)
106 def __add__(self, other):
107 assert self.
name == other.name,
"can't combine two DataCaches with different data types"
110 def chunked(self, chunk_size):
111 for i
in range(0, len(self), chunk_size):
114 def groupby(self, *group):
119 grouped = defaultdict(list)
120 for entry
in self.
cache:
121 grouped[keyfunc(entry)].append(entry)
122 return {key:
DataCache(self.
name, cache)
for key, cache
in sorted(grouped.items())}
124 def groupby_bins(self, bin_type, bins):
125 assert bin_type
in set((
"time",
"segment",
"time_bin")), f
"bin_type: {bin_type} not supported"
128 grouped = defaultdict(list)
130 for entry
in self.
cache:
131 if entry.segment
in bin_:
132 grouped[bin_].append(entry)
134 return {key:
DataCache(self.
name, cache)
for key, cache
in sorted(grouped.items())}
136 def _groupby_keyfunc(self, groups):
137 if isinstance(groups, str):
143 if group
in set((
"ifo",
"instrument",
"observatory")):
144 keys.append(key.observatory)
145 elif group
in set((
"time",
"segment",
"time_bin")):
146 keys.append(key.segment)
147 elif group
in set((
"bin",
"svd_bin")):
148 keys.append(key.description.split(
"_")[0])
149 elif group
in set((
"subtype",
"tag")):
150 keys.append(key.description.rpartition(f
"GSTLAL_{self.name.name}")[2].lstrip(
"_"))
151 elif group
in set((
"directory",
"dirname")):
152 keys.append(os.path.dirname(key.path))
154 raise ValueError(f
"{group} not a valid groupby operation")
162 def copy(self, root=None):
164 for entry
in self.
cache:
165 filedir = self.
_data_path(self.
name, start=entry.segment[0], root=root)
166 filename = os.path.basename(entry.path)
167 cache_paths.append(os.path.join(filedir, filename))
169 return DataCache.from_files(self.
name, cache_paths)
184 if isinstance(ifos, str)
or isinstance(ifos, frozenset):
186 if svd_bins
and isinstance(svd_bins, str):
187 svd_bins = [svd_bins]
188 if subtype
is None or isinstance(subtype, str):
193 time_bins = segmentlistdict({ifo: segmentlist([segment(0, 0)])
for ifo
in ifos})
194 elif isinstance(time_bins, segment):
195 time_bins = segmentlistdict({ifo: segmentlist([time_bins])
for ifo
in ifos})
196 elif isinstance(time_bins, segmentlist):
197 time_bins = segmentlistdict({ifo: time_bins
for ifo
in ifos})
199 time_bins = segmentlistdict({ifo: time_bins[ifo]
for ifo
in ifos
if ifo
in time_bins})
203 for ifo, time_bins
in time_bins.items():
204 for span
in time_bins:
205 path = cls.
_data_path(name, start=span[0], root=root, create=create_dirs)
207 for svd_bin
in svd_bins:
208 for stype
in subtype:
209 filename = name.filename(
210 ifo, span, svd_bin=svd_bin, subtype=stype, extension=extension
212 cache.append(os.path.join(path, filename))
214 for stype
in subtype:
215 filename = name.filename(ifo, span, subtype=stype, extension=extension)
216 cache.append(os.path.join(path, filename))
218 return cls(name, [CacheEntry.from_T050017(entry)
for entry
in cache])
221 def find(cls, name, start=None, end=None, root=None, segments=None, svd_bins=None, extension=None, subtype=None):
224 svd_bins = set([svd_bins])
if isinstance(svd_bins, str)
else set(svd_bins)
227 if subtype
is None or isinstance(subtype, str):
229 for svd_bin
in svd_bins:
230 for stype
in subtype:
231 cache.extend(glob.glob(cls.
_glob_path(name, root, svd_bin, stype, extension=extension)))
232 cache.extend(glob.glob(cls.
_glob_path(name, root, svd_bin, stype, extension=extension, gps_dir=
False)))
234 cache = [CacheEntry.from_T050017(entry)
for entry
in cache]
236 cache = [entry
for entry
in cache
if segments.intersects_segment(entry.segment)]
237 return cls(name, cache)
240 def from_files(cls, name, files):
241 if isinstance(files, str):
243 return cls(name, [CacheEntry.from_T050017(entry)
for entry
in files])
246 def _data_path(datatype, start=None, root=None, create=True):
247 path = datatype.directory(start=start, root=root)
249 os.makedirs(path, exist_ok=
True)
253 def _glob_path(name, root=None, svd_bin=None, subtype=None, extension=None, gps_dir=True):
255 glob_path = os.path.join(str(name).lower(),
"*", name.file_pattern(svd_bin, subtype, extension=extension))
257 glob_path = os.path.join(str(name).lower(), name.file_pattern(svd_bin, subtype, extension=extension))
259 glob_path = os.path.join(root, glob_path)