Skip to content

Instantly share code, notes, and snippets.

def my_float(s):
""" bug in python cant convert -12,345.0 to float so use this workaround
"""
return (float(s.replace(",","")))
#show large nubmers with commas pandas dataframe
# column is string column name
df[column] = df.apply(lambda x: "{:,}".format(x[column]), axis=1)
@jj-github-jj
jj-github-jj / jupyter environment modules.py
Last active December 28, 2021 02:48
jupyter environment copy installed modules to another system
!jupyter --data-dir
#!pip install datamodel-code-generator
!pip install pandasgui
!pip install quantiphy
!pip install plotly.express
!pip install scikit-rf
!pip install nptdms
!pip install cufflinks
@jj-github-jj
jj-github-jj / labview call.py
Last active February 13, 2022 16:05
labview run active x
# !pip install pywin32
import win32com
labview = win32com.client.Dispatch("Labview.Application")
VI = labview.getvireference(vi_path)
print(f'Name: {VI.Name}')
VI.setcontrolvalue(control_name,f) # this works to set float values
s=(VI.getcontrolvalue(ind_name))
@jj-github-jj
jj-github-jj / labview xy plot.py
Created December 6, 2021 05:52
tuple of tuples labview xy multiplot data to py class
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):
@jj-github-jj
jj-github-jj / tdms group channel names autocomplete.py
Last active November 29, 2021 03:28
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 _
@jj-github-jj
jj-github-jj / dataframeto numbers only rows.py
Created November 28, 2021 06:01
# convert data frame strings to numeric where possible and drop rows with non numeric content
# 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
@jj-github-jj
jj-github-jj / numpy utility snippets.py
Last active December 28, 2021 14:13
numpy utility snippets
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):
@jj-github-jj
jj-github-jj / plotly snippets.py
Last active November 9, 2022 03:59
plotly snippets
# 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
@jj-github-jj
jj-github-jj / tdms snippets
Last active November 28, 2021 05:54
tdms utilities
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)
@jj-github-jj
jj-github-jj / json to class
Created November 28, 2021 05:49
json to class for autocomplete
# 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