Mercurial > repos > sagun98 > micropita
comparison galaxy_micropita/src/breadcrumbs/src/UtilityMath.py @ 3:8fb4630ab314 draft default tip
Uploaded
author | sagun98 |
---|---|
date | Thu, 03 Jun 2021 17:07:36 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:1c5736dc85ab | 3:8fb4630ab314 |
---|---|
1 """ | |
2 Author: Timothy Tickle | |
3 Description: Utility class for generic math functions. | |
4 """ | |
5 | |
6 ##################################################################################### | |
7 #Copyright (C) <2012> | |
8 # | |
9 #Permission is hereby granted, free of charge, to any person obtaining a copy of | |
10 #this software and associated documentation files (the "Software"), to deal in the | |
11 #Software without restriction, including without limitation the rights to use, copy, | |
12 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
13 #and to permit persons to whom the Software is furnished to do so, subject to | |
14 #the following conditions: | |
15 # | |
16 #The above copyright notice and this permission notice shall be included in all copies | |
17 #or substantial portions of the Software. | |
18 # | |
19 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
20 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
21 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
22 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
23 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
24 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
25 ##################################################################################### | |
26 | |
27 __author__ = "Timothy Tickle" | |
28 __copyright__ = "Copyright 2012" | |
29 __credits__ = ["Timothy Tickle"] | |
30 __license__ = "MIT" | |
31 __maintainer__ = "Timothy Tickle" | |
32 __email__ = "ttickle@sph.harvard.edu" | |
33 __status__ = "Development" | |
34 | |
35 #Import libaries | |
36 import itertools | |
37 import numpy as np | |
38 import operator | |
39 import random | |
40 from ValidateData import ValidateData | |
41 | |
42 class UtilityMath(): | |
43 """ | |
44 Class to perform misc math methods. | |
45 """ | |
46 | |
47 ## | |
48 #Happy path test 2 | |
49 @staticmethod | |
50 def funcConvertToBHQValue(ldPValues, iNumberOfTests=None): | |
51 """ | |
52 Convert a list of p-value to a list of q-values. | |
53 | |
54 :param ldPValues: List of doubles (p-values) to convert. | |
55 :type List | |
56 :param iNumberOfTests: Number of (multiple) tests if different than the ldValue length. If not set the length of ldPValues is used. | |
57 :type Integer | |
58 :return List: List of Q-values made with a BH modification. | |
59 """ | |
60 | |
61 #If the number of tests is not specified, use the number of pvalues | |
62 if(iNumberOfTests == None): | |
63 iNumberOfTests = len(ldPValues) | |
64 #Used to hold the pvalues as they are being manipulated | |
65 lsConvertToQValues = list() | |
66 #Is used to set the ordr of the pvalues as they are placed in the lsConvertToQValues | |
67 dOrder = 1 | |
68 for dValue in ldPValues: | |
69 lsConvertToQValues.append([dValue,dOrder,None]) | |
70 dOrder = dOrder + 1 | |
71 | |
72 #Sort by pvalue | |
73 lsConvertToQValues.sort(key=lambda x: x[0]) | |
74 | |
75 #Used to keep track of the current test number | |
76 iTest = 1 | |
77 for dConvValue in lsConvertToQValues: | |
78 dConvValue[2] = dConvValue[0] * iNumberOfTests / iTest | |
79 iTest = iTest + 1 | |
80 | |
81 #Sort by original order | |
82 lsConvertToQValues.sort(key=lambda x: x[1]) | |
83 | |
84 #return just 1 dimension (the qvalue) | |
85 return [ldValues[2] for ldValues in lsConvertToQValues] | |
86 | |
87 #Happy path tested 5 | |
88 @staticmethod | |
89 def funcSampleWithReplacement(aData, iSelect): | |
90 """ | |
91 Sample from a vector of data (aData) with replacement iSelect many objects. | |
92 | |
93 :param aData: Data to sample from with replacement. | |
94 :type List | |
95 :param iSelect: Amount of data to select from the original data population. | |
96 :type Integer. | |
97 :return List: List of sampled data. | |
98 Returns an empty list on error. | |
99 """ | |
100 | |
101 if iSelect and aData: | |
102 iDataSize = len(aData) | |
103 funcRandom, funcInt = random.random, int | |
104 lsSampling = operator.itemgetter(*[funcInt(funcRandom() * iDataSize) for selected in itertools.repeat(None, iSelect)])(aData) | |
105 if isinstance(lsSampling, basestring): | |
106 lsSampling = [lsSampling] | |
107 return lsSampling | |
108 return [] | |
109 | |
110 #Happy Path Tested 2 | |
111 @staticmethod | |
112 def funcSumRowsOfColumns(npaAbundance, lsSampleNames): | |
113 """ | |
114 Takes the column names of a npArray and sums the rows into one column. | |
115 | |
116 :param npaAbundance: Array of data to sum. | |
117 :type Numpy Array | |
118 :param lsSampleNames: List of sample names. | |
119 :type List List of strings. | |
120 :return List List of data summed at each row. | |
121 """ | |
122 | |
123 #Compress by data name | |
124 npPooledSample = npaAbundance[lsSampleNames[0]] | |
125 for strSampleName in lsSampleNames[1:]: | |
126 #When combining, combine counts by summing | |
127 npPooledSample = npPooledSample + npaAbundance[strSampleName] | |
128 return list(npPooledSample) | |
129 | |
130 #Testing Status: Light happy path testing 2 | |
131 @staticmethod | |
132 def funcTransposeDataMatrix(npaMatrix, fRemoveAdornments=False): | |
133 """ | |
134 Transposes a numpy array. | |
135 | |
136 :param npaMatrix: Data matrix to transpose. | |
137 :type Numpy Array | |
138 :param fRemoveAdornments: Remove the first column before transposing. | |
139 :type Boolean True indicates removing the column. | |
140 :return Boolean or Numpy Array: Transposed array or a boolean indicating error. | |
141 Boolean False is returned on error. | |
142 """ | |
143 | |
144 #Validate parameters | |
145 if(not ValidateData.funcIsValidNPArray(npaMatrix)): | |
146 print "".join(["Utility_Math:transposeDataMatrix::Error, transposeDataMatrix was an invalid structured array. Value =",str(npaMatrix)]) | |
147 return False | |
148 if(not ValidateData.funcIsValidBoolean(fRemoveAdornments)): | |
149 print "".join(["Utility_Math:transposeDataMatrix::Error, fRemoveAdornments was an invalid boolean. Value =",str(fRemoveAdornments)]) | |
150 return False | |
151 | |
152 #Change to samples x taxa as is needed for the compute method below | |
153 #Also remove the first row which is taxa identification | |
154 conversionMatrix = [list(row)[fRemoveAdornments:] for row in npaMatrix] | |
155 return np.array(conversionMatrix).transpose() | |
156 |