OpenMPCD
test_OnTheFlyStatisticsDDDA.py
1 from __future__ import division
2 
3 import pytest
4 
5 from MPCDAnalysis.OnTheFlyStatisticsDDDA import OnTheFlyStatisticsDDDA
6 
7 def test_emptyInstance():
8  ddda = OnTheFlyStatisticsDDDA()
9 
10  assert ddda.getSampleSize() == 0
11 
12  with pytest.raises(Exception):
13  ddda.getSampleMean()
14 
15  with pytest.raises(Exception):
16  ddda.getMaximumBlockSize()
17 
18  assert ddda.getMaximumBlockID() == 0
19 
20 
21 def test_oneDatum():
22  ddda = OnTheFlyStatisticsDDDA()
23 
24  ddda.addDatum(5)
25 
26  assert ddda.getSampleSize() == 1
27  assert ddda.getSampleMean() == 5
28  assert ddda.getMaximumBlockSize() == 1
29  assert ddda.getMaximumBlockID() == 0
30 
31 
32 def test_twoData_int():
33  ddda = OnTheFlyStatisticsDDDA()
34 
35  data = [5, 2]
36 
37  for datum in data:
38  ddda.addDatum(datum)
39 
40  assert ddda.getSampleSize() == 2
41  assert ddda.getSampleMean() == sum(data) / len(data)
42  assert ddda.getMaximumBlockSize() == 2
43  assert ddda.getMaximumBlockID() == 1
44 
45 
46 def test_twoData_float():
47  ddda = OnTheFlyStatisticsDDDA()
48 
49  data = [5, 2.0]
50 
51  for datum in data:
52  ddda.addDatum(datum)
53 
54  assert ddda.getSampleSize() == 2
55  assert ddda.getSampleMean() == sum(data) / len(data)
56  assert ddda.getMaximumBlockSize() == 2
57  assert ddda.getMaximumBlockID() == 1
58 
59 
60 def test_threeData():
61  ddda = OnTheFlyStatisticsDDDA()
62 
63  data = [5, 2.0, -1.2]
64 
65  for datum in data:
66  ddda.addDatum(datum)
67 
68  assert ddda.getSampleSize() == 3
69  assert ddda.getSampleMean() == sum(data) / len(data)
70  assert ddda.getMaximumBlockSize() == 2
71  assert ddda.getMaximumBlockID() == 1
72 
73 
74 def test_fourData():
75  ddda = OnTheFlyStatisticsDDDA()
76 
77  data = [5, 2.0, -1.2, 40.0]
78 
79  for datum in data:
80  ddda.addDatum(datum)
81 
82  from pytest import approx
83 
84  assert ddda.getSampleSize() == 4
85  assert ddda.getSampleMean() == approx(sum(data) / len(data))
86  assert ddda.getMaximumBlockSize() == 4
87  assert ddda.getMaximumBlockID() == 2
88 
89 
90 def test_dynamicData():
91  dataSizes = \
92  [
93  1,
94  2, 3,
95  4, 5,
96  7, 8, 9,
97  15, 16, 17,
98  31, 32, 33,
99  63, 64, 65, 100
100  ]
101 
102  for dataSize in dataSizes:
103  import math
104  import random
105  data = [random.random() for i in range(0, dataSize)]
106 
107  ddda = OnTheFlyStatisticsDDDA()
108  for datum in data:
109  ddda.addDatum(datum)
110 
111  blockSizeCount = 1
112  maximumBlockSize = 1
113  while True:
114  if maximumBlockSize * 2 > dataSize:
115  break
116  blockSizeCount += 1
117  maximumBlockSize *= 2
118 
119  assert ddda.getSampleSize() == len(data)
120  assert ddda.getSampleMean() == pytest.approx(sum(data) / len(data))
121  assert ddda.getMaximumBlockSize() == maximumBlockSize
122  assert ddda.getMaximumBlockID() == blockSizeCount - 1
123 
124 
125 def test_merge():
126  dataSizes = \
127  [
128  1,
129  2, 3,
130  4, 5,
131  7, 8, 9,
132  15, 16, 17,
133  31, 32, 33,
134  63, 64, 65, 100,
135  1000,
136  ]
137 
138  ddda = OnTheFlyStatisticsDDDA()
139  with pytest.raises(TypeError):
140  ddda.merge(None)
141  with pytest.raises(TypeError):
142  ddda.merge([ddda])
143  with pytest.raises(TypeError):
144  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
145  ddda.merge(OnTheFlyStatistics())
146 
147 
148  def approximatelyEquivalent(lhs, rhs):
149  assert lhs is not rhs
150 
151  if lhs.getSampleSize() != rhs.getSampleSize():
152  return False
153  if lhs.getSampleMean() != pytest.approx(rhs.getSampleMean()):
154  return False
155  if lhs.getMaximumBlockSize() != rhs.getMaximumBlockSize():
156  return False
157  if lhs.getMaximumBlockID() != rhs.getMaximumBlockID():
158  return False
159  for i in range(0, lhs.getMaximumBlockID() + 1):
160  if lhs.hasBlockVariance(i) != rhs.hasBlockVariance(i):
161  return False
162  if not lhs.hasBlockVariance(i):
163  continue
164 
165  blockVarianceLHS = lhs.getBlockVariance(i)
166  blockVarianceRHS = rhs.getBlockVariance(i)
167 
168  if i == 0:
169  if blockVarianceLHS != pytest.approx(blockVarianceRHS):
170  return False
171  else:
172  # For blocked data, the way in which the data are blocked may
173  # differ in the arguments provided to this function, and in case
174  # some blocks are not complete, the data points that are waiting
175  # may differ between the arguments.
176  # A meaningful criterion is therefore hard to formulate.
177  pass
178 
179  return True
180 
181  import random
182  for dataSize1 in dataSizes:
183  for dataSize2 in dataSizes:
184  data1 = [random.random() for _ in range(0, dataSize1)]
185  data2 = [random.random() for _ in range(0, dataSize2)]
186 
187  ddda1 = OnTheFlyStatisticsDDDA()
188  ddda2 = OnTheFlyStatisticsDDDA()
189 
190  ddda11 = OnTheFlyStatisticsDDDA()
191  ddda12 = OnTheFlyStatisticsDDDA()
192 
193  for datum in data1:
194  ddda1.addDatum(datum)
195  ddda11.addDatum(datum)
196  ddda12.addDatum(datum)
197 
198  for datum in data1:
199  ddda11.addDatum(datum)
200 
201  for datum in data2:
202  ddda2.addDatum(datum)
203  ddda12.addDatum(datum)
204 
205  import copy
206  copied = copy.deepcopy(ddda1)
207  assert ddda1 == copied
208  assert not ddda1 is copied
209 
210  assert not approximatelyEquivalent(ddda1, ddda12)
211  ddda1.merge(ddda2)
212  assert approximatelyEquivalent(ddda1, ddda12)
213 
214  #merge self:
215  assert not approximatelyEquivalent(copied, ddda11)
216  copied.merge(copied)
217  assert approximatelyEquivalent(copied, ddda11)
218 
219 
220 
221 def test_hasBlockVariance_getBlockVariance():
222  dataSizes = \
223  [
224  0, 1,
225  2, 3,
226  4, 5,
227  7, 8, 9,
228  15, 16, 17,
229  31, 32, 33,
230  63, 64, 65, 100
231  ]
232 
233  for dataSize in dataSizes:
234  import random
235  data = [random.random() for i in range(0, dataSize)]
236 
237  ddda = OnTheFlyStatisticsDDDA()
238  for datum in data:
239  ddda.addDatum(datum)
240 
241  with pytest.raises(TypeError):
242  ddda.hasBlockVariance(0.0)
243 
244  with pytest.raises(TypeError):
245  ddda.getBlockVariance(0.0)
246 
247  with pytest.raises(ValueError):
248  ddda.getBlockVariance(-1)
249 
250  with pytest.raises(ValueError):
251  ddda.hasBlockVariance(-1)
252 
253  with pytest.raises(ValueError):
254  ddda.hasBlockVariance(ddda.getMaximumBlockID() + 1)
255 
256  with pytest.raises(ValueError):
257  ddda.getBlockVariance(ddda.getMaximumBlockID() + 1)
258 
259 
260  for blockID in range(0, ddda.getMaximumBlockID() + 1):
261  blockSize = 2 ** blockID
262 
263  if dataSize / blockSize < 2:
264  assert not ddda.hasBlockVariance(blockID)
265  with pytest.raises(RuntimeError):
266  ddda.getBlockVariance(blockID)
267  else:
268  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
269  stat = OnTheFlyStatistics()
270  tmp = OnTheFlyStatistics()
271  for datum in data:
272  tmp.addDatum(datum)
273 
274  if tmp.getSampleSize() == blockSize:
275  stat.addDatum(tmp.getSampleMean())
276  tmp = OnTheFlyStatistics()
277 
278  expected = stat.getSampleVariance()
279 
280  assert ddda.hasBlockVariance(blockID)
281  assert ddda.getBlockVariance(blockID) == pytest.approx(expected)
282 
283 
284 def test_getBlockStandardDeviation():
285  dataSizes = \
286  [
287  0, 1,
288  2, 3,
289  4, 5,
290  7, 8, 9,
291  15, 16, 17,
292  31, 32, 33,
293  63, 64, 65, 100
294  ]
295 
296  for dataSize in dataSizes:
297  import random
298  data = [random.random() for i in range(0, dataSize)]
299 
300  ddda = OnTheFlyStatisticsDDDA()
301  for datum in data:
302  ddda.addDatum(datum)
303 
304  with pytest.raises(TypeError):
305  ddda.getBlockStandardDeviation(0.0)
306 
307  with pytest.raises(ValueError):
308  ddda.getBlockStandardDeviation(-1)
309 
310  with pytest.raises(ValueError):
311  ddda.getBlockStandardDeviation(ddda.getMaximumBlockID() + 1)
312 
313 
314  for blockID in range(0, ddda.getMaximumBlockID() + 1):
315  if not ddda.hasBlockVariance(blockID):
316  with pytest.raises(RuntimeError):
317  ddda.getBlockStandardDeviation(blockID)
318 
319  continue
320 
321  import math
322  expected = math.sqrt(ddda.getBlockVariance(blockID))
323  assert ddda.getBlockStandardDeviation(blockID) == expected
324 
325 
326 def test_getSampleStandardDeviation():
327  dataSizes = \
328  [
329  0, 1,
330  2, 3,
331  4, 5,
332  7, 8, 9,
333  15, 16, 17,
334  31, 32, 33,
335  63, 64, 65, 100
336  ]
337 
338  for dataSize in dataSizes:
339  import random
340  data = [random.random() for i in range(0, dataSize)]
341 
342  ddda = OnTheFlyStatisticsDDDA()
343  for datum in data:
344  ddda.addDatum(datum)
345 
346  if not ddda.hasBlockVariance(0):
347  with pytest.raises(RuntimeError):
348  ddda.getSampleStandardDeviation()
349 
350  continue
351 
352  expected = ddda.getBlockStandardDeviation(0)
353  assert ddda.getSampleStandardDeviation() == expected
354 
355 
356 
357 def test_getBlockStandardErrorOfTheMean():
358  dataSizes = \
359  [
360  0, 1,
361  2, 3,
362  4, 5,
363  7, 8, 9,
364  15, 16, 17,
365  31, 32, 33,
366  63, 64, 65, 100
367  ]
368 
369  for dataSize in dataSizes:
370  import random
371  data = [random.random() for i in range(0, dataSize)]
372 
373  ddda = OnTheFlyStatisticsDDDA()
374  for datum in data:
375  ddda.addDatum(datum)
376 
377 
378  with pytest.raises(TypeError):
379  ddda.getBlockStandardErrorOfTheMean(0.0)
380 
381  with pytest.raises(ValueError):
382  ddda.getBlockStandardErrorOfTheMean(-1)
383 
384  with pytest.raises(ValueError):
385  ddda.getBlockStandardErrorOfTheMean(ddda.getMaximumBlockID() + 1)
386 
387 
388  for blockID in range(0, ddda.getMaximumBlockID() + 1):
389  blockSize = 2 ** blockID
390 
391  if ddda.hasBlockVariance(blockID):
392  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
393  stat = OnTheFlyStatistics()
394  tmp = OnTheFlyStatistics()
395  for datum in data:
396  tmp.addDatum(datum)
397 
398  if tmp.getSampleSize() == blockSize:
399  stat.addDatum(tmp.getSampleMean())
400  tmp = OnTheFlyStatistics()
401 
402  result = ddda.getBlockStandardErrorOfTheMean(blockID)
403  expected = stat.getStandardErrorOfTheMean()
404 
405  assert result == pytest.approx(expected)
406  else:
407  with pytest.raises(RuntimeError):
408  ddda.getBlockStandardErrorOfTheMean(blockID)
409 
410 
411 def test_getEstimatedStandardDeviationOfBlockStandardErrorOfTheMean():
412  dataSizes = \
413  [
414  0, 1,
415  2, 3,
416  4, 5,
417  7, 8, 9,
418  15, 16, 17,
419  31, 32, 33,
420  63, 64, 65, 100
421  ]
422 
423  for dataSize in dataSizes:
424  import random
425  data = [random.random() for i in range(0, dataSize)]
426 
427  ddda = OnTheFlyStatisticsDDDA()
428 
429  DDDA = OnTheFlyStatisticsDDDA
430  f = DDDA.getEstimatedStandardDeviationOfBlockStandardErrorOfTheMean
431 
432  for datum in data:
433  ddda.addDatum(datum)
434 
435 
436  with pytest.raises(TypeError):
437  ddda.getEstimatedStandardDeviationOfBlockStandardErrorOfTheMean(0.0)
438 
439  with pytest.raises(ValueError):
440  ddda.getEstimatedStandardDeviationOfBlockStandardErrorOfTheMean(-1)
441 
442  with pytest.raises(ValueError):
443  ddda.getEstimatedStandardDeviationOfBlockStandardErrorOfTheMean(
444  ddda.getMaximumBlockID() + 1)
445 
446 
447  for blockID in range(0, ddda.getMaximumBlockID() + 1):
448  if not ddda.hasBlockVariance(blockID):
449  with pytest.raises(RuntimeError):
450  f(ddda, blockID)
451  continue
452 
453  esd = f(ddda, blockID)
454  sem = ddda.getBlockStandardErrorOfTheMean(blockID)
455 
456  blockSize = 2 ** blockID
457  reducedSampleSize = dataSize // blockSize
458 
459  import math
460  assert esd == sem / math.sqrt(2 * reducedSampleSize)
461 
462 
463 def test_optimal_standard_error():
464  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
465 
466  dataSizes = \
467  [
468  0, 1,
469  2, 3,
470  4, 5,
471  7, 8, 9,
472  15, 16, 17,
473  31, 32, 33,
474  63, 64, 65, 100
475  ]
476 
477  for dataSize in dataSizes:
478  import random
479  data = [random.random() for i in range(0, dataSize)]
480 
481  ddda = OnTheFlyStatisticsDDDA()
482  stat = OnTheFlyStatistics()
483  for datum in data:
484  ddda.addDatum(datum)
485  stat.addDatum(datum)
486 
487 
488  if dataSize < 2:
489  with pytest.raises(RuntimeError):
490  ddda.getOptimalBlockIDForStandardErrorOfTheMean()
491  with pytest.raises(RuntimeError):
492  ddda.optimalStandardErrorOfTheMeanEstimateIsReliable()
493  with pytest.raises(RuntimeError):
494  ddda.getOptimalStandardErrorOfTheMean()
495 
496  continue
497 
498 
499 
500  optimalBlockID = ddda.getOptimalBlockIDForStandardErrorOfTheMean()
501  optimalBlockSize = 2 ** optimalBlockID
502 
503  rawSE = stat.getStandardErrorOfTheMean()
504 
505  blocks = []
506  for block in range(0, ddda.getMaximumBlockID()):
507  blockSize = 2 ** block
508 
509  blockStat = OnTheFlyStatistics()
510  tmp = OnTheFlyStatistics()
511  for datum in data:
512  tmp.addDatum(datum)
513 
514  if tmp.getSampleSize() == blockSize:
515  blockStat.addDatum(tmp.getSampleMean())
516  tmp = OnTheFlyStatistics()
517  blocks.append(blockStat)
518 
519  expectedSE = blocks[optimalBlockID].getStandardErrorOfTheMean()
520 
521  _resultSE = ddda.getOptimalStandardErrorOfTheMean()
522  assert _resultSE == pytest.approx(expectedSE)
523 
524 
525  if ddda.optimalStandardErrorOfTheMeanEstimateIsReliable():
526  assert optimalBlockSize < dataSize / 50.0
527  else:
528  assert optimalBlockSize >= dataSize / 50.0
529 
530  criteria = []
531  for blockID in range(0, ddda.getMaximumBlockID() + 1):
532  if not ddda.hasBlockVariance(blockID):
533  criteria.append(False)
534  continue
535 
536  currentSE = blocks[blockID].getStandardErrorOfTheMean()
537  quotient = currentSE / rawSE
538  criterion = 2 ** (blockID * 3) > 2 * dataSize * quotient ** 4
539 
540  criteria.append(criterion)
541 
542  assert len(criteria) == ddda.getMaximumBlockID() + 1
543  for blockID, criterion in enumerate(criteria):
544  if blockID < optimalBlockID - 1:
545  continue
546 
547  if blockID == optimalBlockID - 1:
548  assert criterion == False
549  continue
550 
551  if blockID == ddda.getMaximumBlockID() - 1:
552  if not ddda.hasBlockVariance(ddda.getMaximumBlockID()):
553  if optimalBlockID == blockID:
554  continue
555 
556  if blockID == ddda.getMaximumBlockID():
557  if optimalBlockID == ddda.getMaximumBlockID():
558  continue
559  else:
560  if not ddda.hasBlockVariance(ddda.getMaximumBlockID()):
561  continue
562 
563  assert criterion == True
564 
565 
566 def test_getOptimalBlockIDForStandardErrorOfTheMean_zero_variance():
567  dataSizes = \
568  [
569  2, 3,
570  4, 5,
571  7, 8, 9,
572  15, 16, 17,
573  31, 32, 33,
574  63, 64, 65, 100
575  ]
576 
577  for dataSize in dataSizes:
578  import random
579  datum = random.random()
580  data = [datum for i in range(0, dataSize)]
581 
582  ddda = OnTheFlyStatisticsDDDA()
583  for datum in data:
584  ddda.addDatum(datum)
585 
586  assert ddda.getOptimalBlockIDForStandardErrorOfTheMean() == 0
587 
588 
589 def test_serializeToString():
590  dataSizes = \
591  [
592  0, 1,
593  2, 3,
594  4, 5,
595  7, 8, 9,
596  15, 16, 17,
597  31, 32, 33,
598  63, 64, 65, 100
599  ]
600 
601  for dataSize in dataSizes:
602  import random
603  data = [random.random() for _ in range(0, dataSize)]
604 
605  stat = OnTheFlyStatisticsDDDA()
606  assert isinstance(stat.serializeToString(), str)
607  for datum in data:
608  stat.addDatum(datum)
609  assert isinstance(stat.serializeToString(), str)
610 
611 
612 def test_unserializeFromString():
613  stat = OnTheFlyStatisticsDDDA()
614 
615  with pytest.raises(TypeError):
616  stat.unserializeFromString(["foo"])
617  with pytest.raises(TypeError):
618  stat.unserializeFromString(stat)
619 
620  with pytest.raises(ValueError):
621  stat.unserializeFromString("")
622  with pytest.raises(ValueError):
623  stat.unserializeFromString("foo")
624  with pytest.raises(ValueError):
625  stat.unserializeFromString("123|0")
626  with pytest.raises(ValueError):
627  stat.unserializeFromString("1;0;0;0")
628  with pytest.raises(ValueError):
629  stat.unserializeFromString("1|1|1;0;0;1|")
630  with pytest.raises(ValueError):
631  stat.unserializeFromString("1|-1")
632 
633 
634  stat.unserializeFromString("1|0")
635  assert stat.getSampleSize() == 0
636 
637  stat = OnTheFlyStatisticsDDDA()
638  unserialized = OnTheFlyStatisticsDDDA()
639  unserialized.unserializeFromString(stat.serializeToString())
640  assert unserialized == stat
641  unserialized.unserializeFromString(stat.serializeToString())
642  assert unserialized == stat
643 
644 
645  def approximatelyEqual(lhs, rhs):
646  if lhs.getSampleSize() != rhs.getSampleSize():
647  return False
648 
649  if lhs.getSampleSize() == 0:
650  return True
651 
652  if lhs.getSampleMean() != pytest.approx(rhs.getSampleMean()):
653  return False
654 
655  if lhs.getMaximumBlockID() != rhs.getMaximumBlockID():
656  return False
657 
658  for i in range(0, lhs.getMaximumBlockID() + 1):
659  if lhs.hasBlockVariance(i) != rhs.hasBlockVariance(i):
660  return False
661  if not lhs.hasBlockVariance(i):
662  continue
663 
664  expected = pytest.approx(rhs.getBlockVariance(i))
665  if lhs.getBlockVariance(i) != expected:
666  return False
667 
668  return True
669 
670 
671  import random
672  for _ in range(0, 50):
673  for _ in range(0, random.randint(1, 5)):
674  stat.addDatum(random.random())
675 
676  assert not approximatelyEqual(unserialized, stat)
677  unserialized.unserializeFromString(stat.serializeToString())
678  assert approximatelyEqual(unserialized, stat)
679  unserialized.unserializeFromString(stat.serializeToString())
680  assert approximatelyEqual(unserialized, stat)
681 
682  import copy
683  statCopy = copy.deepcopy(stat)
684  unserializedCopy = copy.deepcopy(unserialized)
685  for _ in range(0, 50):
686  #test that the waiting samples are (un)serialized correctly
687  datum = random.random()
688  statCopy.addDatum(datum)
689  unserializedCopy.addDatum(datum)
690  assert approximatelyEqual(unserializedCopy, statCopy)
691 
692 
693  stat = OnTheFlyStatisticsDDDA()
694  stat.addDatum(1)
695  stat.addDatum(2)
696  stat.addDatum(3)
697  stat.addDatum(4)
698  stat.addDatum(5)
699 
700  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
701  block1 = OnTheFlyStatistics()
702  block2 = OnTheFlyStatistics()
703  block3 = OnTheFlyStatistics()
704  block1.addDatum(1)
705  block1.addDatum(2)
706  block1.addDatum(3)
707  block1.addDatum(4)
708  block1.addDatum(5)
709  block2.addDatum((1 + 2) / 2.0)
710  block2.addDatum((3 + 4) / 2.0)
711  block3.addDatum(((1 + 2) / 2.0) + ((3 + 4) / 2.0))
712 
713  myState = "1|" + "3"
714  myState += "|" + block1.serializeToString()
715  myState += "|" + block2.serializeToString()
716  myState += "|" + block3.serializeToString()
717  myState += "|" + str(5)
718  myState += "|"
719  myState += "|"
720 
721  unserialized = OnTheFlyStatisticsDDDA()
722  unserialized.unserializeFromString(myState)
723  assert approximatelyEqual(unserialized, stat)
724 
725 
726 def test_getMPLAxes():
727  dataSizes = \
728  [
729  0, 1,
730  2, 3,
731  4, 5,
732  7, 8, 9,
733  15, 16, 17,
734  31, 32, 33,
735  63, 64, 65, 100
736  ]
737 
738  for dataSize in dataSizes:
739  import random
740  data = [random.random() for _ in range(0, dataSize)]
741 
742  ddda = OnTheFlyStatisticsDDDA()
743  for datum in data:
744  ddda.addDatum(datum)
745 
746  with pytest.raises(TypeError):
747  ddda.getMPLAxes(0)
748  with pytest.raises(TypeError):
749  ddda.getMPLAxes(1)
750 
751  import matplotlib.axes
752  assert isinstance(ddda.getMPLAxes(False), matplotlib.axes.Axes)
753  assert isinstance(ddda.getMPLAxes(True), matplotlib.axes.Axes)
754 
755 
756 def test___eq_____ne__():
757  dataSizes = \
758  [
759  0, 1,
760  2, 3,
761  4, 5,
762  7, 8, 9,
763  15, 16, 17,
764  31, 32, 33,
765  63, 64, 65, 100
766  ]
767 
768 
769  ddda = OnTheFlyStatisticsDDDA()
770  with pytest.raises(TypeError):
771  ddda.__eq__(None)
772  with pytest.raises(TypeError):
773  ddda.__eq__([ddda])
774  with pytest.raises(TypeError):
775  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
776  ddda.__eq__(OnTheFlyStatistics())
777 
778  with pytest.raises(TypeError):
779  ddda == None
780  with pytest.raises(TypeError):
781  ddda == [ddda]
782  with pytest.raises(TypeError):
783  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
784  ddda == OnTheFlyStatistics()
785 
786  with pytest.raises(TypeError):
787  ddda.__ne__(None)
788  with pytest.raises(TypeError):
789  ddda.__ne__([ddda])
790  with pytest.raises(TypeError):
791  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
792  ddda.__ne__(OnTheFlyStatistics())
793 
794  with pytest.raises(TypeError):
795  ddda != None
796  with pytest.raises(TypeError):
797  ddda != [ddda]
798  with pytest.raises(TypeError):
799  from MPCDAnalysis.OnTheFlyStatistics import OnTheFlyStatistics
800  ddda != OnTheFlyStatistics()
801 
802 
803  for dataSize in dataSizes:
804  import random
805 
806  original = OnTheFlyStatisticsDDDA()
807  same = OnTheFlyStatisticsDDDA()
808  missingBeginning = OnTheFlyStatisticsDDDA()
809  missingEnd = OnTheFlyStatisticsDDDA()
810  modifiedBeginning = OnTheFlyStatisticsDDDA()
811  modifiedEnd = OnTheFlyStatisticsDDDA()
812  for i in range(0, dataSize):
813  datum = random.random()
814 
815  original.addDatum(datum)
816  same.addDatum(datum)
817 
818  if i == 0:
819  modifiedBeginning.addDatum(datum + 1)
820  else:
821  missingBeginning.addDatum(datum)
822  modifiedBeginning.addDatum(datum)
823 
824  if i == dataSize - 1:
825  modifiedEnd.addDatum(datum - 1)
826  else:
827  missingEnd.addDatum(datum)
828  modifiedEnd.addDatum(datum)
829 
830  import copy
831  copied = copy.deepcopy(original)
832  instances = \
833  [
834  original, same, copied,
835  missingBeginning, missingEnd,
836  modifiedBeginning, modifiedEnd
837  ]
838 
839  for ddda in instances:
840  assert ddda == ddda
841  assert ddda.__eq__(ddda)
842  assert not ddda != ddda
843  assert not ddda.__ne__(ddda)
844 
845  for ddda1 in instances:
846  for ddda2 in instances:
847  shouldCompareEqual1 = False
848  if ddda1 is original:
849  shouldCompareEqual1 = True
850  elif ddda1 is same:
851  shouldCompareEqual1 = True
852  elif ddda1 is copied:
853  shouldCompareEqual1 = True
854 
855  shouldCompareEqual2 = False
856  if ddda2 is original:
857  shouldCompareEqual2 = True
858  elif ddda2 is same:
859  shouldCompareEqual2 = True
860  elif ddda2 is copied:
861  shouldCompareEqual2 = True
862 
863  shouldCompareEqual = shouldCompareEqual1 and shouldCompareEqual2
864 
865  if dataSize == 0:
866  shouldCompareEqual = True
867  elif dataSize == 1:
868  tmp1 = ddda1 is missingBeginning or ddda1 is missingEnd
869  tmp2 = ddda2 is missingBeginning or ddda2 is missingEnd
870  if tmp1 and tmp2:
871  shouldCompareEqual = True
872 
873  if ddda1 is ddda2:
874  shouldCompareEqual = True
875 
876  if shouldCompareEqual:
877  assert ddda1 == ddda2
878  assert ddda1.__eq__(ddda2)
879  assert not ddda1 != ddda2
880  assert not ddda1.__ne__(ddda2)
881  else:
882  assert not ddda1 == ddda2
883  assert not ddda1.__eq__(ddda2)
884  assert ddda1 != ddda2
885  assert ddda1.__ne__(ddda2)
886 
887 
888 
889  ddda1 = OnTheFlyStatisticsDDDA()
890  ddda2 = OnTheFlyStatisticsDDDA()
891 
892  ddda1.addDatum(1)
893  ddda1.addDatum(2)
894 
895  ddda2.addDatum(2)
896  ddda2.addDatum(1)
897 
898  assert ddda1 == ddda2
899  assert ddda1.__eq__(ddda2)
900  assert not ddda1 != ddda2
901  assert not ddda1.__ne__(ddda2)
902 
903 
904 
905 def test___repr__():
906  dataSizes = \
907  [
908  0, 1,
909  2, 3,
910  4, 5,
911  7, 8, 9,
912  15, 16, 17,
913  31, 32, 33,
914  63, 64, 65, 100
915  ]
916 
917  for dataSize in dataSizes:
918  import random
919  data = [random.random() for _ in range(0, dataSize)]
920 
921  ddda = OnTheFlyStatisticsDDDA()
922  for datum in data:
923  ddda.addDatum(datum)
924 
925  assert isinstance(ddda.__repr__(), str)
MPCDAnalysis.OnTheFlyStatistics
Definition: OnTheFlyStatistics.py:1
MPCDAnalysis.OnTheFlyStatisticsDDDA
Definition: OnTheFlyStatisticsDDDA.py:1