File size: 4,414 Bytes
0936134 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #! /opt/local/bin/python
import os
import string
import numpy as np
"""
This is just a bunch of functions that I use all the time
"""
def read_file(filename, delimiter=None, startline=0):
"""General function to read text file into a 2D list."""
data_list = []
ifile = open(filename,'rU')
for line in ifile:
if delimiter:
data = line.split(delimiter)
else:
data = line.split()
data_list.append(data)
ifile.close()
return data_list[startline:]
def get_dict_list(data_array):
"""Returns a list of dictionaries based on the column headers
(the 0th line in the column headers)
"""
key_list = data_array[0]
dict_list = []
for index, line in enumerate(data_array[1:]):
params = {}
for i in range(len(key_list)):
# try:
# params[key_list[i]] = float(line[i])
# except ValueError:
params[key_list[i]] = line[i]
dict_list.append(params)
return dict_list
def make_dir(path):
"""General function for making a new directory without raising errors"""
if not os.path.isdir(path):
os.mkdir(path)
#DEFIITION OF FUNCTION TO SORT A LIST IN PLACE USING A KEY THAT'S CONTAINED WITHIN A STRING
def sort_by_key(list, split_char, key_position):
def find_key(line):
key = int(line.split(split_char)[key_position])
return key
list.sort(key=find_key)
return list
#DEFINITION OF A FUNCTION TO SAVE AN ARRAY AS A JUSTIFIED TEXT FILE
def save_data_array(array, save_path):
"""Function to write a (square) array with justified column widths"""
#gets column width
column_width_list = []
for column in zip(*array):
column = map(str,column)
column_width = max(len(x) for x in column) + 2
column_width_list.append(column_width)
#writes array to file
ofile = open(save_path,'w')
for i in range(len(array)):
for j in range(len(array[i])):
element = str(array[i][j]).ljust(column_width_list[j])
ofile.write(element + ' ')
ofile.write('\n')
ofile.close
#DEFINITION OF FIND FIRST HIGHER INDEX
def find_first_higher_index(list,target):
endindex = 0
for index, x in enumerate(list):
if x < target:
endindex = index
return endindex
#DEFINITION OF FUNCTION TO FIND INDEX OF VALUE IN A LIST NEAREST TO A TARGET
def find_nearest(list, target):
target_index = (np.abs(list - target)).argmin()
return target_index
#DEFINITION OF FUNCTION TO FIND INDEX OF VALUE IN A LIST FURTHEST FROM TARGET
def find_furthest(list, target):
target_index = (np.abs(list - target)).argmax()
return target_index
#DEFINITION OF FUNCTION TO COMPRESS MIXED LIST OF ITERABLE AND NON-SEQUENCE TYPES TO A 1-D LIST
#(THIS FUNCTION WORKS FOR TO INFINITE DIMENSIONS)
def flatten_list(old_list):
repeat = 'yes'
while repeat == 'yes':
new_list = []
repeat = 'no'
for item in old_list:
try:
getattr(item,'__iter__')
new_list.extend(item)
repeat = 'yes'
except AttributeError:
new_list.append(item)
old_list = new_list
return new_list
#DEFINITION OF SMOOTHING FUNCTION
def smooth_moving_window(l, window_len=11, include_edges='Off'):
if window_len%2==0:
raise ValueError('>window_len< kwarg in function >smooth_moving_window< must be odd')
l = np.reshape(map(float,l),len(l))
w = np.ones(window_len,'d')
if include_edges == 'On':
edge_list = np.ones(window_len)
begin_list = [x * l[0] for x in edge_list]
end_list = [x * l[-1] for x in edge_list]
s = np.r_[begin_list, l, end_list]
y = np.convolve(w/w.sum(), s , mode='same')
y = y[window_len + 1:-window_len + 1]
elif include_edges == 'Wrap':
s=np.r_[2 * l[0] - l[window_len-1::-1], l, 2 * l[-1] - l[-1:-window_len:-1]]
y = np.convolve(w/w.sum(), s , mode='same')
y = y[window_len:-window_len+1]
elif include_edges == 'Off':
y = np.convolve(w/w.sum(), l, mode='valid')
else:
raise NameError('Error in >include_edges< kwarg of function >smooth_moving_window<')
return y
|