/usr/lib/python2.7/dist-packages/pebl/test/test_data.py is in python-pebl 1.0.2-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | import numpy as N
from pebl import data
from pebl.test import testfile
class TestFileParsing:
def setUp(self):
self.data = data.fromfile(testfile('testdata1.txt'))
self.expected_observations = N.array([[ 2.5, 0. , 1.7],
[ 1.1, 1.7, 2.3],
[ 4.2, 999.3, 12. ]])
self.expected_dtype = N.dtype(float)
self.expected_varnames = ['var1', 'var2', 'var3']
self.expected_missing = N.array([[False, True, False],
[False, False, False],
[False, False, False]], dtype=bool)
self.expected_interventions = N.array([[ True, True, False],
[False, True, False],
[False, False, False]], dtype=bool)
self.expected_arities = [-1,-1,-1]
def test_observations(self):
assert (self.data.observations == self.expected_observations).all()
def test_dtype(self):
assert self.data.observations.dtype == self.expected_dtype
def test_varnames(self):
assert [v.name for v in self.data.variables] == self.expected_varnames
def test_missing(self):
assert (self.data.missing == self.expected_missing).all()
def test_interventions(self):
assert (self.data.interventions == self.expected_interventions).all()
def test_arities(self):
assert [v.arity for v in self.data.variables] == self.expected_arities
class TestComplexFileParsing(TestFileParsing):
def setUp(self):
self.data = data.fromfile(testfile('testdata2.txt'))
self.expected_observations = N.array([[ 0. , 0. , 1.25, 0. ],
[ 1. , 1. , 1.1 , 1. ],
[ 1. , 2. , 0.45, 1. ]])
self.expected_dtype = N.dtype(float) # because one continuous variable
self.expected_varnames = ['shh', 'ptchp', 'smo', 'outcome']
self.expected_interventions = N.array([[ True, True, False, False],
[False, True, False, False],
[False, False, False, False]], dtype=bool)
self.expected_missing = N.array([[False, False, False, False],
[False, False, False, False],
[False, False, False, False]], dtype=bool)
self.expected_arities = [2, 3, -1, 2]
def test_classlabels(self):
assert self.data.variables[3].labels == ['good', 'bad']
class TestFileParsing_WithSampleNames(TestFileParsing):
def setUp(self):
self.data = data.fromfile(testfile('testdata3.txt'))
self.expected_observations = N.array([[0, 0], [1, 1], [1,2]])
self.expected_missing = N.array([[0, 0], [0, 0], [0, 0]], dtype=bool)
self.expected_interventions = N.array([[1, 1], [0, 1], [0, 0]], dtype=bool)
self.expected_varnames = ['shh', 'ptchp']
self.expected_samplenames = ['sample1', 'sample2', 'sample3']
self.expected_arities = [2,3]
self.expected_dtype = N.dtype(int)
def test_sample_names(self):
assert [s.name for s in self.data.samples] == self.expected_samplenames
class TestFileParsing_WithSampleNames2(TestFileParsing_WithSampleNames):
def setUp(self):
self.data = data.fromfile(testfile('testdata4.txt')) # no tab before variable names
self.expected_observations = N.array([[0, 0], [1, 1], [1,2]])
self.expected_missing = N.array([[0, 0], [0, 0], [0, 0]], dtype=bool)
self.expected_interventions = N.array([[1, 1], [0, 1], [0, 0]], dtype=bool)
self.expected_varnames = ['shh', 'ptchp']
self.expected_samplenames = ['sample1', 'sample2', 'sample3']
self.expected_arities = [2,3]
self.expected_dtype = N.dtype(int)
class TestManualDataCreations:
def setUp(self):
obs = N.array([[1.2, 1.4, 2.1, 2.2, 1.1],
[2.3, 1.1, 2.1, 3.2, 1.3],
[3.2, 0.0, 2.2, 2.5, 1.6],
[4.2, 2.4, 3.2, 2.1, 2.8],
[2.7, 1.5, 0.0, 1.5, 1.1],
[1.1, 2.3, 2.1, 1.7, 3.2] ])
interventions = N.array([[0,0,0,0,0],
[0,1,0,0,0],
[0,0,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,1,0] ])
missing = N.array([[0,0,0,0,0],
[0,0,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0],
[0,0,1,0,0],
[0,0,0,0,0] ])
variablenames = ["gene A", "gene B", "receptor protein C", " receptor D", "E kinase protein"]
samplenames = ["head.wt", "limb.wt", "head.shh_knockout", "head.gli_knockout",
"limb.shh_knockout", "limb.gli_knockout"]
self.data = data.Dataset(
obs,
missing.astype(bool),
interventions.astype(bool),
N.array([data.Variable(n) for n in variablenames]),
N.array([data.Sample(n) for n in samplenames])
)
def test_missing(self):
x,y = N.where(self.data.missing)
assert (x == N.array([2, 3, 4])).all() and \
(y == N.array([1, 1, 2])).all()
def test_missing2(self):
assert self.data.missing[N.where(self.data.missing)].tolist() == [ True, True, True]
def test_missing3(self):
assert (N.transpose(N.where(self.data.missing)) == N.array([[2, 1],[3, 1],[4, 2]])).all()
def test_subset1(self):
expected = N.array([[ 1.2, 2.1, 1.1],
[ 2.3, 2.1, 1.3],
[ 3.2, 2.2, 1.6],
[ 4.2, 3.2, 2.8],
[ 2.7, 0. , 1.1],
[ 1.1, 2.1, 3.2]])
assert (self.data.subset(variables=[0,2,4]).observations == expected).all()
def test_subset2(self):
expected = N.array([[ 1.2, 1.4, 2.1, 2.2, 1.1],
[ 3.2, 0. , 2.2, 2.5, 1.6]])
assert (self.data.subset(samples=[0,2]).observations == expected).all()
def test_subset3(self):
subset = self.data.subset(variables=[0,2], samples=[1,2])
expected = N.array([[ 2.3, 2.1],
[ 3.2, 2.2]])
assert (subset.observations == expected).all()
def test_subset3_interventions(self):
subset = self.data.subset(variables=[0,2], samples=[1,2])
expected = N.array([[False, False],
[False, True]], dtype=bool)
assert (subset.interventions == expected).all()
def test_subset3_missing(self):
subset = self.data.subset(variables=[0,2], samples=[1,2])
expected = N.array([[False, False],
[False, False]], dtype=bool)
assert (subset.missing == expected).all()
def test_subset3_varnames(self):
subset = self.data.subset(variables=[0,2], samples=[1,2])
expected = ['gene A', 'receptor protein C']
assert [v.name for v in subset.variables] == expected
def test_subset3_samplenames(self):
subset = self.data.subset(variables=[0,2], samples=[1,2])
expected = ['limb.wt', 'head.shh_knockout']
assert [s.name for s in subset.samples] == expected
class TestDataDiscretization:
def setUp(self):
self.data = data.fromfile(testfile('testdata5.txt'))
self.data.discretize()
self.expected_original = \
N.array([[ 1.2, 1.4, 2.1, 2.2, 1.1],
[ 2.3, 1.1, 2.1, 3.2, 1.3],
[ 3.2, 0. , 1.2, 2.5, 1.6],
[ 4.2, 2.4, 3.2, 2.1, 2.8],
[ 2.7, 1.5, 0. , 1.5, 1.1],
[ 1.1, 2.3, 2.1, 1.7, 3.2],
[ 2.3, 1.1, 4.3, 2.3, 1.1],
[ 3.2, 2.6, 1.9, 1.7, 1.1],
[ 2.1, 1.5, 3. , 1.4, 1.1]])
self.expected_discretized = \
N.array([[0, 1, 1, 1, 0],
[1, 0, 1, 2, 1],
[2, 0, 0, 2, 2],
[2, 2, 2, 1, 2],
[1, 1, 0, 0, 0],
[0, 2, 1, 0, 2],
[1, 0, 2, 2, 0],
[2, 2, 0, 0, 0],
[0, 1, 2, 0, 0]])
self.expected_arities = [3,3,3,3,3]
def test_orig_observations(self):
assert (self.data.original_observations == self.expected_original).all()
def test_disc_observations(self):
assert (self.data.observations == self.expected_discretized).all()
def test_arity(self):
assert [v.arity for v in self.data.variables] == self.expected_arities
class TestDataDiscretizationWithMissing:
"""Respond to Issue 32: Pebl should ignore the missing values when
selecting bins for each data point. Discretization for this should be
the same as if there were no missing data, as in TestDataDiscretization.
"""
def setUp(self):
self.data = data.fromfile(testfile('testdata5m.txt'))
self.data.discretize()
self.expected_original = \
N.array([[ 1.2, 1.4, 2.1, 2.2, 1.1],
[ 2.3, 1.1, 2.1, 3.2, 1.3],
[ 3.2, 0. , 1.2, 2.5, 1.6],
[ 4.2, 2.4, 3.2, 2.1, 2.8],
[ 2.7, 1.5, 0. , 1.5, 1.1],
[ 1.1, 2.3, 2.1, 1.7, 3.2],
[ 2.3, 1.1, 4.3, 2.3, 1.1],
[ 3.2, 2.6, 1.9, 1.7, 1.1],
[ 2.1, 1.5, 3. , 1.4, 1.1],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ]])
self.expected_discretized = \
N.array([[0, 1, 1, 1, 0],
[1, 0, 1, 2, 1],
[2, 0, 0, 2, 2],
[2, 2, 2, 1, 2],
[1, 1, 0, 0, 0],
[0, 2, 1, 0, 2],
[1, 0, 2, 2, 0],
[2, 2, 0, 0, 0],
[0, 1, 2, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
self.expected_arities = [3,3,3,3,3]
self.expected_missing = N.array([[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[True , True , True , True , True ],
[True , True , True , True , True ],
[True , True , True , True , True ]],
dtype=bool)
def test_orig_observations(self):
assert (self.data.original_observations == self.expected_original).all()
def test_disc_observations(self):
assert (self.data.observations == self.expected_discretized).all()
def test_arity(self):
assert [v.arity for v in self.data.variables] == self.expected_arities
def test_missing(self):
assert (self.data.missing == self.expected_missing).all()
class TestSelectiveDataDiscretization(TestDataDiscretization):
def setUp(self):
self.data = data.fromfile(testfile('testdata5.txt'))
self.data.discretize(includevars=[0,2])
self.expected_original = \
N.array([[ 1.2, 1.4, 2.1, 2.2, 1.1],
[ 2.3, 1.1, 2.1, 3.2, 1.3],
[ 3.2, 0. , 1.2, 2.5, 1.6],
[ 4.2, 2.4, 3.2, 2.1, 2.8],
[ 2.7, 1.5, 0. , 1.5, 1.1],
[ 1.1, 2.3, 2.1, 1.7, 3.2],
[ 2.3, 1.1, 4.3, 2.3, 1.1],
[ 3.2, 2.6, 1.9, 1.7, 1.1],
[ 2.1, 1.5, 3. , 1.4, 1.1]])
self.expected_discretized = \
N.array([[ 0. , 1.4, 1. , 2.2, 1.1],
[ 1. , 1.1, 1. , 3.2, 1.3],
[ 2. , 0. , 0. , 2.5, 1.6],
[ 2. , 2.4, 2. , 2.1, 2.8],
[ 1. , 1.5, 0. , 1.5, 1.1],
[ 0. , 2.3, 1. , 1.7, 3.2],
[ 1. , 1.1, 2. , 2.3, 1.1],
[ 2. , 2.6, 0. , 1.7, 1.1],
[ 0. , 1.5, 2. , 1.4, 1.1]])
self.expected_arities = [3,-1,3,-1,-1]
class TestSelectiveDataDiscretization2(TestDataDiscretization):
def setUp(self):
self.data = data.fromfile(testfile('testdata5.txt'))
self.data.discretize(excludevars=[0,1])
self.expected_original = \
N.array([[ 1.2, 1.4, 2.1, 2.2, 1.1],
[ 2.3, 1.1, 2.1, 3.2, 1.3],
[ 3.2, 0. , 1.2, 2.5, 1.6],
[ 4.2, 2.4, 3.2, 2.1, 2.8],
[ 2.7, 1.5, 0. , 1.5, 1.1],
[ 1.1, 2.3, 2.1, 1.7, 3.2],
[ 2.3, 1.1, 4.3, 2.3, 1.1],
[ 3.2, 2.6, 1.9, 1.7, 1.1],
[ 2.1, 1.5, 3. , 1.4, 1.1]])
self.expected_discretized = \
N.array([[ 1.2, 1.4, 1. , 1. , 0. ],
[ 2.3, 1.1, 1. , 2. , 1. ],
[ 3.2, 0. , 0. , 2. , 2. ],
[ 4.2, 2.4, 2. , 1. , 2. ],
[ 2.7, 1.5, 0. , 0. , 0. ],
[ 1.1, 2.3, 1. , 0. , 2. ],
[ 2.3, 1.1, 2. , 2. , 0. ],
[ 3.2, 2.6, 0. , 0. , 0. ],
[ 2.1, 1.5, 2. , 0. , 0. ]])
self.expected_arities = [-1,-1,3,3,3]
def test_arity_checking():
try:
# arity specified is less than number of unique values!!
dataset = data.fromfile(testfile('testdata6.txt'))
except data.IncorrectArityError:
assert True
else:
assert False
def test_arity_checking2():
try:
# arity specified is MORE than number of unique values. this is ok.
dataset = data.fromfile(testfile('testdata7.txt'))
except:
assert False
assert [v.arity for v in dataset.variables] == [3,4,3,6]
|