Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import pywt
import csv
import pandas as pd
import numpy as np
def read_csv(filename):
columns = defaultdict(list) # each value in each column is appended to a list
with open(filename) as f:
reader = csv.DictReader(f) # read rows into a dictionary format
for row in reader: # read a row as {column1: value1, column2: value2,...}
for (k,v) in row.items(): # go over each column name and value
columns[k].append(v) # append the value into the appropriate list
# based on column name k
return columns
def csv_read_fast(filename, col_header_name, start_row, end_row):
if end_row == -1:
df = pd.read_csv(filename, sep=',', skipinitialspace=True, names=col_header_name, dtype=np.float32, skiprows=range(0, start_row)) #dtype={'a': np.float32}
else:
df = pd.read_csv(filename, sep=',', skipinitialspace=True, names=col_header_name, dtype=np.float32, skiprows=range(0, start_row), nrows=end_row-start_row) #dtype={'a': np.float32}
return df
def waveFn(wavelet):
if not isinstance(wavelet, pywt.Wavelet):
return pywt.Wavelet(wavelet)
else:
return wavelet
# http://stackoverflow.com/questions/5707353/how-to-extend-pywavelets-to-work-with-n-dimensional-data
# given a single dimensional array ... returns the coefficients.
def wavedec(data, wavelet, mode='periodic'):
wavelet = waveFn(wavelet)
dLen = len(data)
level = pywt.dwt_max_level(dLen, wavelet.dec_len)
coeffs = np.zeros((1,level))
a = data
for idx in xrange(level):
a, d = pywt.dwt(a, wavelet, mode)
coeffs[0,idx] = a[0]
return coeffs
def main():
col_header_name = ['1', '2', '3']
df = csv_read_fast('../data/wavelet_test.csv', col_header_name, 0, -1)
res = wavedec(df['2'], 'haar', 'periodic')
print res
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.