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_misc.py 2.2 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
  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. """Tests for module misc.py"""
  23. import numpy as np
  24. import pandas as pd
  25. import pytest
  26. from pandas.util.testing import assert_almost_equal
  27. from causalimpact.misc import get_z_score, standardize, unstandardize
  28. def test_basic_standardize():
  29. data = {
  30. 'c1': [1, 4, 8, 9, 10],
  31. 'c2': [4, 8, 12, 16, 20]
  32. }
  33. data = pd.DataFrame(data)
  34. result, (mu, sig) = standardize(data)
  35. assert_almost_equal(
  36. np.zeros(data.shape[1]),
  37. result.mean().values
  38. )
  39. assert_almost_equal(
  40. np.ones(data.shape[1]),
  41. result.std(ddof=0).values
  42. )
  43. def test_standardize_w_various_distinct_inputs():
  44. test_data = [[1, 2, 1], [1, np.nan, 3], [10, 20, 30]]
  45. test_data = [pd.DataFrame(data, dtype="float") for data in test_data]
  46. for data in test_data:
  47. result, (mu, sig) = standardize(data)
  48. pd.util.testing.assert_frame_equal(unstandardize(result, (mu, sig)), data)
  49. def test_standardize_raises_single_input():
  50. with pytest.raises(ValueError):
  51. standardize(pd.DataFrame([1]))
  52. def test_get_z_score():
  53. assert get_z_score(0.5) == 0.
  54. assert round(get_z_score(0.9177), 2) == 1.39
Tip!

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

Comments

Loading...