Skip to content

Commit

Permalink
Merge pull request open-ephys#76 from malfatti/FixSettingsXMLPy
Browse files Browse the repository at this point in the history
Fix recursive function and add function to parse sampling rate
  • Loading branch information
jsiegle authored Jul 29, 2019
2 parents 7903bcc + 6632460 commit de10056
Showing 1 changed file with 60 additions and 26 deletions.
86 changes: 60 additions & 26 deletions Python3/SettingsXML.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
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
Expand All @@ -39,35 +42,39 @@
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():
if El.getchildren():
for SubEl in El:
if SubEl.keys():
if SubEl.get('name'):
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:

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: Dict[SubEl.tag] = Root2Dict(SubEl)


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()))
Expand All @@ -77,44 +84,71 @@ def Root2Dict(El):
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.'

try:
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
return(Rate)
elif Info['SIGNALCHAIN']['PROCESSOR']['Sources/Rhythm FPGA']['EDITOR']['SampleRate'] == '17':
Rate = 30000
return(Rate)
else:
print(Error); return(None)
except:
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 '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)

0 comments on commit de10056

Please sign in to comment.