def test_split_callback_converts_correctly(self): string = "1 2 3 4" parts = split(string, int) self.assertListEqual([1, 2, 3, 4], parts) string = "1.0 2.1 3.2 4.3" parts = split(string, float) self.assertListEqual([1.0, 2.1, 3.2, 4.3], parts)
def _create_blob(self): """Parse the next event from the current file position""" blob = None for line in self.blob_file: line = try_decode_string(line) line = line.strip() if line == "": self.log.info("Ignoring empty line...") continue if line.startswith("end_event:") and blob: blob["raw_header"] = self.raw_header return blob try: tag, values = line.split(":") except ValueError: self.log.warning("Ignoring corrupt line: {}".format(line)) continue try: values = tuple(split(values.strip(), callback=float)) except ValueError: self.log.info("Empty value: {}".format(values)) if line.startswith("start_event:"): blob = Blob() blob[tag] = tuple(int(v) for v in values) continue if tag not in blob: blob[tag] = [] blob[tag].append(values)
def check_version(h5file): try: version = np.string_(h5file.root._v_attrs.format_version) except AttributeError: log.error( "Could not determine HDF5 format version: '%s'." "You may encounter unexpected errors! Good luck..." % h5file.filename ) return if split(version, int, np.string_('.')) < \ split(MINIMUM_FORMAT_VERSION, int, np.string_('.')): raise H5VersionError( "HDF5 format version {0} or newer required!\n" "'{1}' has HDF5 format version {2}.".format( MINIMUM_FORMAT_VERSION.decode("utf-8"), h5file.filename, version.decode("utf-8") ) )
def parse_doms(self): """Extract dom information from detector file""" self.det_file.seek(0, 0) self.det_file.readline() lines = self.det_file.readlines() try: while True: line = lines.pop(0) if not line: continue try: dom_id, line_id, floor_id, n_pmts = split(line, int) except ValueError: continue self.n_pmts_per_dom = n_pmts for i in range(n_pmts): raw_pmt_info = lines.pop(0) pmt_info = raw_pmt_info.split() pmt_id, x, y, z, rest = unpack_nfirst(pmt_info, 4) dx, dy, dz, t0, rest = unpack_nfirst(rest, 4) if rest: log.warn("Unexpected PMT values: '{0}'".format(rest)) pmt_id = int(pmt_id) pmt_pos = [float(n) for n in (x, y, z)] pmt_dir = [float(n) for n in (dx, dy, dz)] t0 = float(t0) if floor_id < 0: _, new_floor_id, _ = self.pmtid2omkey_old(pmt_id) log.error("Floor ID is negative for PMT {0}.\n" "Guessing correct id: {1}" .format(pmt_id, new_floor_id)) floor_id = new_floor_id #TODO: following line is here bc of the bad MC floor IDs # put it outside the for loop in future self.doms[dom_id] = (line_id, floor_id, n_pmts) omkey = (line_id, floor_id, i) pmt = PMT(pmt_id, pmt_pos, pmt_dir, t0, i, omkey) self.pmts.append(pmt) self._pmts_by_omkey[(line_id, floor_id, i)] = pmt self._pmts_by_id[pmt_id] = pmt except IndexError: pass
def test_split_splits_strings_with_separator(self): string = "1,2,3,4" parts = split(string, sep=',') self.assertListEqual(['1', '2', '3', '4'], parts)
def test_split_splits_strings(self): string = "1 2 3 4" parts = split(string) self.assertListEqual(['1', '2', '3', '4'], parts)
def parse_header(self): """Extract information from the header of the detector file""" self.det_file.seek(0, 0) first_line = self.det_file.readline() self.det_id, self.n_doms = split(first_line, int)
def test_split_splits_strings_with_separator(self): string = "1,2,3,4" parts = split(string, sep=",") self.assertListEqual(["1", "2", "3", "4"], parts)
def test_split_splits_strings(self): string = "1 2 3 4" parts = split(string) self.assertListEqual(["1", "2", "3", "4"], parts)