test_prodcom_bulk_total.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python3
  2. import pytest
  3. from numpy import isnan
  4. from hashlib import sha256
  5. from rdflib import Namespace
  6. from probs_runner import PROBS, QUANTITYKIND
  7. from numpy.testing import assert_allclose
  8. PRODCOM2016 = Namespace("http://w3id.org/probs-lab/data/prodcom/2016/")
  9. PRODCOM2017 = Namespace("http://w3id.org/probs-lab/data/prodcom/2017/")
  10. GEONAMES = Namespace("https://sws.geonames.org/")
  11. def assert_exact_results(result, expected_value):
  12. assert len(result) == 1
  13. assert result[0].bound == PROBS.ExactBound
  14. assert_allclose(result[0].measurement, expected_value, rtol=1e-3)
  15. def assert_no_measurement(result):
  16. assert len(result) == 1
  17. assert isnan(result[0].measurement)
  18. def assert_no_result(result):
  19. assert len(result) == 0
  20. def obj_id(obj_code):
  21. return ("Object-" + sha256(obj_code.encode('utf-8')).hexdigest())
  22. # Do most tests for 2016; then spot check a few values for the other years
  23. class TestProdcom2016Data:
  24. """Test PRODCOM2016 DATA conversion"""
  25. endpoint_data = ["outputs/total_production/DS-056121_006-2016.nt.gz"]
  26. @pytest.mark.parametrize("code,metric,expected_value", [
  27. ("13103100", QUANTITYKIND.Mass, 224299),
  28. ("13911910", QUANTITYKIND.Mass, 12566190),
  29. ("15115100", QUANTITYKIND.Mass, 0),
  30. ("20132455", QUANTITYKIND.MassAmountOfSubstance, 1369740),
  31. ("20141290", QUANTITYKIND.Mass, 208123),
  32. ])
  33. def test_expected_measurements(self, rdfox, code, metric, expected_value):
  34. result = rdfox.get_observations(
  35. PROBS.TimePeriod_YearOf2016,
  36. GEONAMES["2635167"],
  37. metric,
  38. PROBS.TotalProduction,
  39. object_=PRODCOM2017["Object-" + code]
  40. )
  41. assert_exact_results(result, expected_value)
  42. @pytest.mark.parametrize("code,metric", [
  43. ("13202042", QUANTITYKIND.Area), # No PRDQNT, :C for PQNTFLAG
  44. ("20141325", QUANTITYKIND.Mass), # No PRDQNT, :C for PQNTFLAG
  45. ])
  46. def test_missing_measurements(self, rdfox, code, metric):
  47. # these codes have no data for PRDQNT
  48. result = rdfox.get_observations(
  49. PROBS.TimePeriod_YearOf2016,
  50. GEONAMES["2635167"] ,
  51. metric,
  52. PROBS.TotalProduction,
  53. object_=PRODCOM2017["Object-" + code],
  54. )
  55. assert_no_result(result)
  56. print(PRODCOM2017[obj_id(code)])
  57. @pytest.mark.parametrize("code,metric", [
  58. ("13201230", QUANTITYKIND.Mass),
  59. ("20144370", QUANTITYKIND.AmountOfSubstance),
  60. ])
  61. def test_no_result_with_wrong_metric(self, rdfox, code, metric):
  62. # same as above, but with the wrong metric
  63. result = rdfox.get_observations(
  64. PROBS.TimePeriod_YearOf2016,
  65. GEONAMES["2635167"] ,
  66. metric,
  67. PROBS.TotalProduction,
  68. object_=PRODCOM2017["Object-" + code],
  69. )
  70. assert_no_result(result)
  71. class TestProdcom2017Data:
  72. """Test PRODCOM2017 DATA conversion.
  73. Just include a few expected values, since other behaviour is tested for the 2016 data.
  74. """
  75. endpoint_data = ["outputs/total_production/DS-056121_006-2017.nt.gz"]
  76. @pytest.mark.parametrize("code,metric,expected_value", [
  77. ("08111250", QUANTITYKIND.Mass, 604132000),
  78. ("20111120", QUANTITYKIND.Volume, 387410000),
  79. ("13201230", QUANTITYKIND.Area, 8038713),
  80. ("20122415", QUANTITYKIND.MassAmountOfSubstance, 31291132),
  81. ])
  82. def test_expected_measurements(self, rdfox, code, metric, expected_value):
  83. result = rdfox.get_observations(
  84. PROBS.TimePeriod_YearOf2017,
  85. GEONAMES["2635167"],
  86. metric,
  87. PROBS.TotalProduction,
  88. object_=PRODCOM2017["Object-" + code]
  89. )
  90. assert_exact_results(result, expected_value)
  91. class TestProdcom2018Data:
  92. """Test PRODCOM2018 DATA conversion.
  93. Just include a few expected values, since other behaviour is tested for the 2016 data.
  94. """
  95. endpoint_data = ["outputs/total_production/DS-056121_006-2018.nt.gz"]
  96. @pytest.mark.parametrize("code,metric,expected_value", [
  97. ("08114000", QUANTITYKIND.Mass, 41455000),
  98. ("13106135", QUANTITYKIND.Mass, 9193633),
  99. ("13203210", QUANTITYKIND.Area, 4300488),
  100. ("15112200", QUANTITYKIND.Area, 0),
  101. ])
  102. def test_expected_measurements(self, rdfox, code, metric, expected_value):
  103. result = rdfox.get_observations(
  104. PROBS.TimePeriod_YearOf2018,
  105. GEONAMES["2635167"],
  106. metric,
  107. PROBS.TotalProduction,
  108. object_=PRODCOM2017["Object-" + code]
  109. )
  110. assert_exact_results(result, expected_value)