Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_summary.py 4.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. # MIT License
  2. #
  3. # Copyright (c) 2018 Dafiti OpenSource
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. """
  23. Tests for summary.py module.
  24. """
  25. import os
  26. import pandas as pd
  27. import pytest
  28. from causalimpact.summary import Summary
  29. @pytest.fixture
  30. def summary_data():
  31. data = [
  32. [5.343, 10.343],
  33. [4.343, 9.343],
  34. [3.343, 8.343],
  35. [6.343, 9.343],
  36. [3.343, 10.343],
  37. [2.343, 4.343],
  38. [6.343, 9.343],
  39. [0.123, 0.233],
  40. [0.143, 0.133],
  41. [0.343, 0.333]
  42. ]
  43. data = pd.DataFrame(
  44. data,
  45. columns=['average', 'cumulative'],
  46. index=[
  47. 'actual',
  48. 'predicted',
  49. 'predicted_lower',
  50. 'predicted_upper',
  51. 'abs_effect',
  52. 'abs_effect_lower',
  53. 'abs_effect_upper',
  54. 'rel_effect',
  55. 'rel_effect_lower',
  56. 'rel_effect_upper'
  57. ]
  58. )
  59. return data
  60. @pytest.fixture
  61. def summarizer():
  62. return Summary()
  63. def test_summary_raises(summarizer):
  64. summarizer = Summary()
  65. with pytest.raises(RuntimeError):
  66. summarizer.summary()
  67. with pytest.raises(ValueError):
  68. summarizer.summary_data = 'test'
  69. summarizer.summary('test')
  70. def test_output_summary_1(summary_data, fix_path, summarizer):
  71. summarizer.summary_data = summary_data
  72. summarizer.alpha = 0.1
  73. summarizer.p_value = 0.459329
  74. result = summarizer.summary()
  75. expected = open(os.path.join(fix_path, 'test_summary_output_1')).read().strip()
  76. assert result == expected
  77. def test_summary_1(summary_data, fix_path, summarizer):
  78. # detected positive signal but with no significance.
  79. summarizer.summary_data = summary_data
  80. summarizer.alpha = 0.1
  81. summarizer.p_value = 0.5
  82. summary_data['average']['rel_effect'] = 0.41
  83. summary_data['average']['rel_effect_lower'] = -0.30
  84. summary_data['average']['rel_effect_upper'] = 0.30
  85. result = summarizer.summary(output='report')
  86. expected = open(os.path.join(fix_path, 'test_summary_1')).read().strip()
  87. assert result == expected
  88. def test_summary_2(summary_data, fix_path, summarizer):
  89. # detected positive signal with significance.
  90. summarizer.summary_data = summary_data
  91. summarizer.alpha = 0.1
  92. summarizer.p_value = 0.05
  93. summary_data['average']['rel_effect'] = 0.41
  94. summary_data['average']['rel_effect_lower'] = 0.434
  95. summary_data['average']['rel_effect_upper'] = 0.234
  96. result = summarizer.summary(output='report')
  97. expected = open(os.path.join(fix_path, 'test_summary_2')).read().strip()
  98. assert result == expected
  99. def test_summary_3(summary_data, fix_path, summarizer):
  100. # detected negative signal but with no significance.
  101. summary_data['average']['rel_effect'] = -0.343
  102. summary_data['average']['rel_effect_lower'] = -0.434
  103. summary_data['average']['rel_effect_upper'] = 0.234
  104. summarizer.summary_data = summary_data
  105. summarizer.alpha = 0.1
  106. summarizer.p_value = 0.5
  107. result = summarizer.summary(output='report')
  108. expected = open(os.path.join(fix_path, 'test_summary_3')).read().strip()
  109. assert result == expected
  110. def test_summary_4(summary_data, fix_path, summarizer):
  111. # detected negative signal with significance.
  112. summary_data['average']['rel_effect'] = -0.343
  113. summary_data['average']['rel_effect_lower'] = -0.434
  114. summary_data['average']['rel_effect_upper'] = -0.234
  115. summarizer.summary_data = summary_data
  116. summarizer.alpha = 0.1
  117. summarizer.p_value = 0.05
  118. result = summarizer.summary(output='report')
  119. expected = open(os.path.join(fix_path, 'test_summary_4')).read().strip()
  120. assert result == expected
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...