|
8 | 8 |
|
9 | 9 | import numpy as np |
10 | 10 |
|
11 | | -from ...utils import _pl, _soft_import |
12 | | - |
13 | | - |
14 | | -def _extract(tags, filepath=None, obj=None): |
15 | | - """Extract info from XML.""" |
16 | | - _soft_import("defusedxml", "reading EGI MFF data") |
17 | | - from defusedxml.minidom import parse |
18 | | - |
19 | | - if obj is not None: |
20 | | - fileobj = obj |
21 | | - elif filepath is not None: |
22 | | - fileobj = parse(filepath) |
23 | | - else: |
24 | | - raise ValueError("There is not object or file to extract data") |
25 | | - infoxml = dict() |
26 | | - for tag in tags: |
27 | | - value = fileobj.getElementsByTagName(tag) |
28 | | - infoxml[tag] = [] |
29 | | - for i in range(len(value)): |
30 | | - infoxml[tag].append(value[i].firstChild.data) |
31 | | - return infoxml |
| 11 | +from ...utils import _pl |
32 | 12 |
|
33 | 13 |
|
34 | 14 | def _get_gains(filepath): |
35 | 15 | """Parse gains.""" |
36 | | - _soft_import("defusedxml", "reading EGI MFF data") |
37 | | - from defusedxml.minidom import parse |
38 | | - |
39 | | - file_obj = parse(filepath) |
40 | | - objects = file_obj.getElementsByTagName("calibration") |
41 | | - gains = dict() |
42 | | - for ob in objects: |
43 | | - value = ob.getElementsByTagName("type") |
44 | | - if value[0].firstChild.data == "GCAL": |
45 | | - data_g = _extract(["ch"], obj=ob)["ch"] |
46 | | - gains.update(gcal=np.asarray(data_g, dtype=np.float64)) |
47 | | - elif value[0].firstChild.data == "ICAL": |
48 | | - data_g = _extract(["ch"], obj=ob)["ch"] |
49 | | - gains.update(ical=np.asarray(data_g, dtype=np.float64)) |
| 16 | + from mffpy.xml_files import XML |
| 17 | + |
| 18 | + obj = XML.from_file(filepath) |
| 19 | + cals = obj.get_content().get("calibrations", {}) |
| 20 | + gains = {} |
| 21 | + if "GCAL" in cals: |
| 22 | + ch_dict = cals["GCAL"]["channels"] |
| 23 | + gains["gcal"] = np.asarray( |
| 24 | + [ch_dict[k] for k in sorted(ch_dict)], dtype=np.float64 |
| 25 | + ) |
| 26 | + if "ICAL" in cals: |
| 27 | + ch_dict = cals["ICAL"]["channels"] |
| 28 | + gains["ical"] = np.asarray( |
| 29 | + [ch_dict[k] for k in sorted(ch_dict)], dtype=np.float64 |
| 30 | + ) |
50 | 31 | return gains |
51 | 32 |
|
52 | 33 |
|
|
0 commit comments