Aux Function.
(filename)
| 69 | |
| 70 | |
| 71 | def _read_stc(filename): |
| 72 | """Aux Function.""" |
| 73 | with open(filename, "rb") as fid: |
| 74 | buf = fid.read() |
| 75 | |
| 76 | stc = dict() |
| 77 | offset = 0 |
| 78 | num_bytes = 4 |
| 79 | |
| 80 | # read tmin in ms |
| 81 | stc["tmin"] = ( |
| 82 | float(np.frombuffer(buf, dtype=">f4", count=1, offset=offset).item()) / 1000.0 |
| 83 | ) |
| 84 | offset += num_bytes |
| 85 | |
| 86 | # read sampling rate in ms |
| 87 | stc["tstep"] = ( |
| 88 | float(np.frombuffer(buf, dtype=">f4", count=1, offset=offset).item()) / 1000.0 |
| 89 | ) |
| 90 | offset += num_bytes |
| 91 | |
| 92 | # read number of vertices/sources |
| 93 | vertices_n = int(np.frombuffer(buf, dtype=">u4", count=1, offset=offset).item()) |
| 94 | offset += num_bytes |
| 95 | |
| 96 | # read the source vector |
| 97 | stc["vertices"] = np.frombuffer(buf, dtype=">u4", count=vertices_n, offset=offset) |
| 98 | offset += num_bytes * vertices_n |
| 99 | |
| 100 | # read the number of timepts |
| 101 | data_n = int(np.frombuffer(buf, dtype=">u4", count=1, offset=offset).item()) |
| 102 | offset += num_bytes |
| 103 | |
| 104 | if ( |
| 105 | vertices_n |
| 106 | and ( # vertices_n can be 0 (empty stc) |
| 107 | (len(buf) // 4 - 4 - vertices_n) % (data_n * vertices_n) |
| 108 | ) |
| 109 | != 0 |
| 110 | ): |
| 111 | raise ValueError("incorrect stc file size") |
| 112 | |
| 113 | # read the data matrix |
| 114 | stc["data"] = np.frombuffer( |
| 115 | buf, dtype=">f4", count=vertices_n * data_n, offset=offset |
| 116 | ) |
| 117 | stc["data"] = stc["data"].reshape([data_n, vertices_n]).T |
| 118 | |
| 119 | return stc |
| 120 | |
| 121 | |
| 122 | def _write_stc(filename, tmin, tstep, vertices, data): |
no test coverage detected