-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneralTupleListFunctions.py
142 lines (126 loc) · 4.62 KB
/
GeneralTupleListFunctions.py
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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 28 21:05:28 2016
@author: Lily
"""
#%matplotlib inline #impt for graphing in jupyter notebook, but messes up spyder
import csv
import random
import math
import operator
import statistics as stat
import matplotlib.pyplot as plt
import scipy.stats
import numpy as np
##idk if you need all of these but some are definately required
def includeIfValue(some_tuple_list, element, operator, value):
new_list = []
if (operator == "=="):
for ii in range(len(some_tuple_list)):
if some_tuple_list[ii][element] == value:
new_list.append(some_tuple_list[ii])
elif (operator == "<"):
for ii in range(len(some_tuple_list)):
if some_tuple_list[ii][element] < value:
new_list.append(some_tuple_list[ii])
elif (operator == ">"):
for ii in range(len(some_tuple_list)):
if some_tuple_list[ii][element] > value:
new_list.append(some_tuple_list[ii])
return new_list
#reads in a tuple list, a function that returns a boolean (input is a tuple), and a value
#returns a list of tuples
def newTupleList(some_tuple_list, f, Value):
new_list = []
for ii in range(len(some_tuple_list)):
if (f(some_tuple_list[ii], Value)):
new_list.append(some_tuple_list[ii])
return new_list
#prints an element of a tuple list
def printElementOf(some_tuple_list, element):
for ii in range(len(some_tuple_list)):
print(some_tuple_list[ii][element])
#turns an element of a tuple list into an array
def tupleList(some_tuple_list, element):
new_list = []
for ii in range(len(some_tuple_list)):
new_list.append(some_tuple_list[ii][element])
return new_list
#finds the percent of an element in a list that is a value
def percent(some_tuple_list, element, value):
elementPresent = 0
for ii in range(len(some_tuple_list)):
if some_tuple_list[ii][element] == value:
elementPresent+=1
return (elementPresent/len(some_tuple_list))*100
#averages a column in a tuple list (averageType = mean, median or mode)
def averageTuple(some_tuple_list, element, averageType):
new_list = []
for ii in range(len(some_tuple_list)):
new_list.append(some_tuple_list[ii][element])
try:
if averageType == "mean":
return stat.mean(new_list)
if averageType == "median":
return stat.median(new_list)
if averageType == "mode":
return stat.mode(new_list)
except:
print("No data points found")
return 0
#finds the correlation coefficient bewteen two elements in a tuple array
def pearsonRTuple(some_tuple_list, element1, element2):
new_list1 = []
new_list2 = []
for ii in range(len(some_tuple_list)):
new_list1.append(some_tuple_list[ii][element1])
new_list2.append(some_tuple_list[ii][element2])
return scipy.stats.pearsonr(new_list1, new_list2)[0]
#creates a histogram of an element in a tuple array, with a specified bin size
#if bin_size = 0, auto sets binsize
def createHist(some_tuple_list, element, bin_size):
new_list = []
for ii in range(len(some_tuple_list)):
new_list.append(some_tuple_list[ii][element])
maximum = int(math.ceil(max(new_list)))
if bin_size != 0:
bins_list = []
for hh in range(int(math.ceil(maximum/bin_size))):
bins_list.append(bin_size*hh)
plt.hist(new_list, bins_list)
else:
plt.hist(new_list)
def createScatter(some_tuple_list, elementx, elementy):
new_listx = []
new_listy = []
for ii in range(len(some_tuple_list)):
new_listx.append(some_tuple_list[ii][elementx])
new_listy.append(some_tuple_list[ii][elementy])
plt.scatter(new_listx, new_listy)
def createScatterColor(some_tuple_list, elementx, elementy, color):
new_listx = []
new_listy = []
for ii in range(len(some_tuple_list)):
new_listx.append(some_tuple_list[ii][elementx])
new_listy.append(some_tuple_list[ii][elementy])
plt.scatter(new_listx, new_listy, c=color)
def newPlot(title, xlabel, ylabel):
secondPlot = plt.figure()
ax2 = secondPlot.add_subplot(111)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
def newLogPlotY(title, xlabel, ylabel):
secondPlot = plt.figure()
ax2 = secondPlot.add_subplot(111)
ax2.set_yscale("log")
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
def newLogPlotX(title, xlabel, ylabel):
secondPlot = plt.figure()
ax2 = secondPlot.add_subplot(111)
ax2.set_xscale("log")
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)