Skip to content

Instantly share code, notes, and snippets.

@jj-github-jj
Last active November 29, 2021 03:28
Show Gist options
  • Save jj-github-jj/d0a81c7b622d5b6f63a0bda02af27f83 to your computer and use it in GitHub Desktop.
Save jj-github-jj/d0a81c7b622d5b6f63a0bda02af27f83 to your computer and use it in GitHub Desktop.
tdms group names channels name autocomplete. convert group names and convert channel names function do the trick of returning namedtuple that can be used for autocomplete
def ntuples_list(list_of_names):
"""
"""
list_of_names_dict = {x:x for x in list_of_names}
Varnames = namedtuple('Varnames', list_of_names)
return Varnames(**list_of_names_dict)
def change_to_valid_column_names(df3):
df3.columns=[(re.sub("[/' ()-+]","_",x)) for x in df3.columns.to_list()] #replace invalid char with _
df3.columns=[(re.sub("-+","_",x)) for x in df3.columns.to_list()] #replace 1 or more - with _
df3.columns=[(re.sub("_+","_",x)) for x in df3.columns.to_list()] #replace 1 or more _ with single _ to compact
# + is quantifier for 1 or more https://cheatography.com/davechild/cheat-sheets/regular-expressions/
df3.columns=[x.replace("_","n",1) if (x[0] == "_") else x for x in df3.columns.to_list()] #cant start with underscore so change first underscore
return (df3)
#Do-something if <condition>, else do-something else.
#[a if C else b for i in items]
def change_to_valid_names(list_of_names):
s=[(re.sub("[/' ()-+]","_",x)) for x in list_of_names] #replace invalid char with _
s=[(re.sub("-+","_",x)) for x in s] #replace 1 or more _ with single _ to compact
s=[(re.sub("_+","_",x)) for x in s] #replace 1 or more _ with single _ to compact
s=[x.replace("_","n",1) if (x[0] == "_") else x for x in s] #cant start with underscore so change first underscore
return(s)
def abbreviate_names(df):
my_ab={'Phase Noise':'PN','Phase_Noise':'PN','Phase_Noise_at':'PN',
'Frequency':'Freq','FREQUENCY':'Freq',"Power":"Pwr",
'Temperature':'Tmpr',
"Carrier":"Carr",
"Converter":"Conv",
"TEMP_SENSOR":"TS",
"Measured":"Meas",
"Measurement":"Meas"
}
for word in my_ab:
df2.columns=[x.replace(word,my_ab[word]) for x in df2.columns.to_list()] #list comprehension to substiture abbreviations in names
return (df)
def ntuples_list2(list_of_names):
"""
valid names: strings with valid names since
both key and value must be valid name for namedtuple so
changing only key as in following code is not sufficient list_of_names should be cleaned first
"""
valid_names=change_to_valid_names(list_of_names)
list_of_names_dict = {x:y for x,y in zip(valid_names,list_of_names)}
Varnames = namedtuple('Varnames', list_of_names)
return Varnames(**list_of_names_dict)
def convert_group_names(tdms_file):
t=tdms_file._groups.copy()
for group_name in t:
valid_name=change_to_valid_names([group_name]) #list input
tdms_file._groups[valid_name[0]]=tdms_file._groups.pop(group_name) #change key in dict groups with valid name
#dictionary[new_key] = dictionary.pop(old_key)
group_names=[x for x in tdms_file._groups]
#group_names=change_to_valid_names(group_names)
gn_var=ntuples_list2(group_names) #creates a named tuple type variable that autocompletes
return (gn_var) #return group names variable that can be used for autocomplete
#type gn_var. and wait for autocomplete to list group names
def convert_ch_names(tdms_file,group):
chcpy=tdms_file[group]._channels.copy() #must copy else assignment operates on same data a
for ch in chcpy:
valid_name=change_to_valid_names([ch]) #list input
tdms_file[group]._channels[valid_name[0]]=tdms_file[group]._channels.pop(ch)
valid_ch_names=[change_to_valid_names([x])[0] for x in ch_names] #list input and output hence[x] and [0]
#valid_ch_names
cn_var=ntuples_list2(valid_ch_names) #creates a named tuple type variable that autocompletes
return(cn_var) #type cn_var. and wait for auto complete to list channel names
#tdms_file[group]._channels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment