-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsXML.py
164 lines (122 loc) · 5.07 KB
/
SettingsXML.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
"""
Created on 20170704 21:15:19
@author: Thawann Malfatti
Loads info from the settings.xml file.
Examples:
File = '/Path/To/Experiment/settings.xml
# To get all info the xml file can provide:
AllInfo = SettingsXML.XML2Dict(File)
# AllInfo will be a dictionary following the same structure of the XML file.
# To get the sampling rate used in recording:
Rate = SettingsXML.GetSamplingRate(File)
# To get info only about channels recorded:
RecChs = SettingsXML.GetRecChs(File)[0]
# To get also the processor names:
RecChs, PluginNames = SettingsXML.GetRecChs(File)
# RecChs will be a dictionary:
#
# RecChs
# ProcessorNodeId
# ChIndex
# 'name'
# 'number'
# 'gain'
# 'PluginName'
"""
from xml.etree import ElementTree
def FindRecProcs(Ch, Proc, RecChs):
ChNo = Ch['number']
Rec = Proc['CHANNEL'][ChNo]['SELECTIONSTATE']['record']
if Rec == '1':
if Proc['NodeId'] not in RecChs: RecChs[Proc['NodeId']] = {}
RecChs[Proc['NodeId']][ChNo] = Ch
return(RecChs)
def Root2Dict(El):
Dict = {}
if El.getchildren():
for SubEl in El:
if SubEl.keys():
if SubEl.get('name'):
if SubEl.tag not in Dict: Dict[SubEl.tag] = {}
Dict[SubEl.tag][SubEl.get('name')] = Root2Dict(SubEl)
Dict[SubEl.tag][SubEl.get('name')].update(
{K: SubEl.get(K) for K in SubEl.keys() if K is not 'name'}
)
else:
Dict[SubEl.tag] = Root2Dict(SubEl)
Dict[SubEl.tag].update(
{K: SubEl.get(K) for K in SubEl.keys() if K is not 'name'}
)
else:
if SubEl.tag not in Dict: Dict[SubEl.tag] = Root2Dict(SubEl)
else:
No = len([k for k in Dict if SubEl.tag in k])
Dict[SubEl.tag+'_'+str(No+1)] = Root2Dict(SubEl)
return(Dict)
else:
if El.items(): return(dict(El.items()))
else: return(El.text)
def XML2Dict(File):
Tree = ElementTree.parse(File); Root = Tree.getroot()
Info = Root2Dict(Root)
return(Info)
def GetSamplingRate(File):
Info = XML2Dict(File)
Error = 'Cannot parse sample rate. Check your settings.xml file at SIGNALCHAIN>PROCESSOR>Sources/Rhythm FPGA.'
SignalChains = [_ for _ in Info.keys() if 'SIGNALCHAIN' in _]
try:
for SignalChain in SignalChains:
if 'Sources/Rhythm FPGA' in Info[SignalChain]['PROCESSOR'].keys():
if 'SampleRateString' in Info[SignalChain]['PROCESSOR']['Sources/Rhythm FPGA']['EDITOR']:
Rate = Info[SignalChain]['PROCESSOR']['Sources/Rhythm FPGA']['EDITOR']['SampleRateString']
Rate = float(Rate.split(' ')[0])*1000
elif Info[SignalChain]['PROCESSOR']['Sources/Rhythm FPGA']['EDITOR']['SampleRate'] == '17':
Rate = 30000
elif Info[SignalChain]['PROCESSOR']['Sources/Rhythm FPGA']['EDITOR']['SampleRate'] == '16':
Rate = 25000
else:
Rate = None
else:
Rate = None
if not Rate:
print(Error); return(None)
else:
return(Rate)
except Exception as Ex:
print(Ex); print(Error); return(None)
def GetRecChs(File):
Info = XML2Dict(File)
RecChs = {}; ProcNames = {}
if len([k for k in Info if 'SIGNALCHAIN' in k]) > 1:
for S in [k for k in Info if 'SIGNALCHAIN_' in k]:
for P, Proc in Info[S]['PROCESSOR'].items():
Info['SIGNALCHAIN']['PROCESSOR'][P+'_'+S[-1]] = Proc
del(Info[S])
# print('There are more than one signal chain in file. )
# Ind = input(')
for P, Proc in Info['SIGNALCHAIN']['PROCESSOR'].items():
if 'isSource' in Proc:
if Proc['isSource'] == '1': SourceProc = P[:]
else:
if Proc['name'].split('/')[0] == 'Sources': SourceProc = P[:]
if 'CHANNEL_INFO' in Proc and Proc['CHANNEL_INFO']:
for Ch in Proc['CHANNEL_INFO']['CHANNEL'].values():
RecChs = FindRecProcs(Ch, Proc, RecChs)
elif 'CHANNEL' in Proc:
for Ch in Proc['CHANNEL'].values():
RecChs = FindRecProcs(Ch, Proc, RecChs)
else: continue
if 'pluginName' in Proc:
ProcNames[Proc['NodeId']] = Proc['pluginName']
else:
ProcNames[Proc['NodeId']] = Proc['name']
if Info['SIGNALCHAIN']['PROCESSOR'][SourceProc]['CHANNEL_INFO']:
SourceProc = Info['SIGNALCHAIN']['PROCESSOR'][SourceProc]['CHANNEL_INFO']['CHANNEL']
else:
SourceProc = Info['SIGNALCHAIN']['PROCESSOR'][SourceProc]['CHANNEL']
for P, Proc in RecChs.items():
for C, Ch in Proc.items():
if 'gain' not in Ch:
RecChs[P][C].update([c for c in SourceProc.values() if c['number'] == C][0])
return(RecChs, ProcNames)