This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
import re | |
#str1, n = re.subn('[0-9]', 'X',str1) | |
def ntuples(df): | |
list_of_names = df.columns.values | |
print(list_of_names[0:2]) | |
list_of_names_dict = {x:x for x in list_of_names} | |
Varnames = namedtuple('Varnames', list_of_names) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create an 8 row by 2 columns dataframe with column labels | |
df=pd.DataFrame(np.reshape(np.zeros(16),(8,2)),columns=['x','y']) # create an 8 row by 2 columns df | |
c=df23.columns.to_list() | |
c[0] | |
df.iloc[0:,0:2] # rows 0 through end of column 0 and 1 | |
#---------- | |
df.drop(df.columns[[0, 4, 2]], axis = 1, inplace = True) #drop columns using column index | |
#---------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Turns a dictionary into a class | |
# type variable name such as t4. and wait for the class attributes to appear in editor using Kite or other language processors | |
class Dict2Class(object): | |
def __init__(self, my_dict): | |
for key in my_dict: | |
key2=key.replace(" ","_") #key2 for valid names without spaces for object attributes | |
key2=key2.replace(":","_") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for key,value in x.iteritems(): | |
# new_key = key.replace(" ","_") | |
# del x[key] # Deleting Previous Key | |
# x[new_key] = value # Adding Modified key | |
# for key,value in x.iteritems(): | |
# new_key = key.replace(" ","_") | |
# del x[key] # Deleting Previous Key | |
# x[new_key] = value # Adding Modified key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tdms_file=TdmsFile.read(fpath) | |
for name, value in tdms_file.properties.items(): | |
print("{0}: {1}".format(name, value)) | |
print ("------ Groups --------") | |
all_groups = tdms_file.groups() | |
for group in all_groups: | |
print ("Group Name:",group.name) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# speed up figures with lots of data | |
pip install plotly-resampler | |
from plotly_resampler import register_plotly_resampler, unregister_plotly_resampler | |
register_plotly_resampler(mode="auto", default_n_shown_samples=1000) | |
import plotly.io as pio | |
pio.renderers.default = "notebook" # patch for exporting to html in vscode for plotly interactive plots |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_nearest(array, value): | |
array = np.asarray(array) | |
idx = (np.abs(array - value)).argmin() | |
return idx,array[idx] #return in x,y format x is index, y is value | |
[print (i,v/1e9) for i,v in enumerate(x)] #print index and value of elements in list | |
def avg_dB(dB): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert data frame strings to numeric where possible and drop rows with non numeric content | |
df23=df23.apply(pd.to_numeric, errors='ignore') # converts to numbers when possible for all columns | |
df23 | |
c=df23.columns.to_list() | |
data_columns=[c[x] for x in range(1,6)] | |
# Eliminate invalid data from dataframe (see Example below for more context) | |
num_df = (df23.drop(data_columns, axis=1) | |
.join(df23[data_columns].apply(pd.to_numeric, errors='coerce'))) #numeric dataframe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 _ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass, is_dataclass | |
def nested_dataclass(*args, **kwargs): | |
def wrapper(cls): | |
cls = dataclass(cls, **kwargs) | |
original_init = cls.__init__ | |
def __init__(self, *args, **kwargs): | |
for name, value in kwargs.items(): | |
field_type = cls.__annotations__.get(name, None) | |
if is_dataclass(field_type) and isinstance(value, dict): |
OlderNewer