"""Helper functions to support the analysis of a single measurementfrom a MultispeQ device."""
[docs]defGetLabelLookup(data=None):""" Generate a protocol lookup table for a protocol set. :param data: The protocol output :type data: dict :return: Lookup table :rtype: dict """ifdataisNone:returnNoneifnot'set'indata:returnNonelookup={}fori,iteminenumerate(data['set']):if'label'initem:ifitem['label']notinlookup:lookup[item['label']]=[]lookup[item['label']].append(i)else:if'unknown'notinlookup:lookup['unknown']=[]lookup['unknown'].append(i)iflen(lookup.items())==0:returnNoneelse:returnlookup
[docs]defGetProtocolByLabel(label=None,data=None,array=False):""" Returns the protocol from within the protocol set matching the provided label. If only one label exists, one protocol object is returned. When multiple protocols in the set have the same label an array with all protcol objects of matching labels is returned. :param label: The label from the protocol set :type label: str :param data: The protocol data :type data: dict :param array: Always return an array, default is False :type array: bool :return: Single protocol or a list of protocols :rtype: dict or list[dict] """iflabelisNone:returnNoneifdataisNone:returnNoneifnot'set'indata:returnNoneif(arrayisNone)or(notisinstance(array,bool)):array=Falseout=[aforaindata['set']if('label'ina)and(a['label']==label)]iflen(out)==0:returnNoneiflen(out)==1:returnoutifarrayelseout[0]else:returnout
[docs]defGetIndexByLabel(label=None,data=None,array=False):""" Find the positions for protocols within a protocol set matching the provided label. If only one label exists within a set, a number is returned. When multiple protocols in the set have the same label an array with all indexes of matching labels is returned. :param label: The label from the protocol set :type label: str :param data: The protocol data :type data: dict :param array: Always return an array, default is False :type array: bool :return: Single index or an array of indexes :rtype: int or list[int] """iflabelisNone:returnNoneifdataisNone:returnNoneifnot'set'indata:returnNoneif(arrayisNone)or(notisinstance(array,bool)):array=Falseout=[ifori,ainenumerate(data['set'])if('label'ina)and(a['label']==label)]iflen(out)==0:returnNoneiflen(out)==1:returnoutifarrayelseout[0]else:returnout