def read_iar(iarfile: os.PathLike) -> dict: """Read .iar file into dict.""" iar = read_file(iarfile, "r") iar = dict([line.split(",") for line in iar.splitlines()]) for key, value in iar.items(): try: iar[key] = float(value) except ValueError: continue return iar
def raw_to_filterbank( rawfile: os.PathLike, header: dict = None, outfile: os.PathLike = None, overwrite: bool = False, ) -> None: """Generate .fil file from header dict and raw data file.""" bin_raw = read_file(rawfile, "rb") _binraw_to_filterbank(bin_raw, header, outfile, overwrite) return
def raw_to_df( rawfile: os.PathLike, n_channels: int = 2048, fmt: np.dtype = ">i8", order: str = "F", ) -> pd.DataFrame: """Read a binary raw data file, and returns a dataframe.""" bin_raw = read_file(rawfile, "rb") dt = np.dtype(fmt) np_data = np.frombuffer(bin_raw, dtype=dt) bytes_per_data = dt.alignment total_bytes = len(bin_raw) n_records = int(total_bytes / n_channels / bytes_per_data) np_data = np_data.reshape(n_channels, n_records, order=order) return pd.DataFrame(np_data)
def test_read_file_bin(): f = read_file(rawpath, "rb") assert isinstance(f, bytes) assert f[:10] == b"\x00\x00\x00\x00\x00\x00\xc0:\x00\x00" assert f[-10:] == b"\xd3\xee\x00\x00\x00\x00\x00\x00\x00\x00"
def test_read_file_wrong_uft8(): with pytest.raises(UnicodeDecodeError): read_file(rawpath)
def test_read_file(): f = read_file(iarpath) assert isinstance(f, str) assert f[:10] == "Source Nam" assert f[-10:] == "s,1\nCal,0\n"