-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work in progress: HS code fixing Aggregation of trade data
- Loading branch information
Showing
7 changed files
with
334 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
# Copyright (C) 2023 University of Bordeaux, CyVi Group & Anish Koyamparambath | ||
# This file is part of geopolrisk-py library. | ||
# | ||
# geopolrisk-py is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# geopolrisk-py is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with geopolrisk-py. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
ListofMetals = [ | ||
250410, | ||
251110, | ||
251910, | ||
252010, | ||
252400, | ||
252910, | ||
252922, | ||
260111, | ||
260200, | ||
260300, | ||
260400, | ||
260600, | ||
260700, | ||
260800, | ||
260900, | ||
261000, | ||
261100, | ||
261210, | ||
261310, | ||
261400, | ||
261510, | ||
261590, | ||
261610, | ||
261710, | ||
270111, | ||
270112, | ||
270900, | ||
271111, | ||
280450, | ||
280480, | ||
280490, | ||
280540, | ||
282200, | ||
282560, | ||
283691, | ||
284690, | ||
710811, | ||
711011, | ||
711021, | ||
711031, | ||
810710, | ||
811291, | ||
] | ||
|
||
|
||
ListofYears = list(range(2018, 2023)) | ||
|
||
ListofCountries = [ | ||
36, | ||
40, | ||
56, | ||
124, | ||
152, | ||
156, | ||
170, | ||
188, | ||
208, | ||
233, | ||
246, | ||
251, | ||
276, | ||
300, | ||
352, | ||
372, | ||
376, | ||
380, | ||
392, | ||
410, | ||
428, | ||
440, | ||
442, | ||
484, | ||
528, | ||
554, | ||
579, | ||
616, | ||
620, | ||
703, | ||
705, | ||
724, | ||
752, | ||
757, | ||
792, | ||
826, | ||
842, | ||
348, | ||
203, | ||
] | ||
|
||
ListofCountryName = [ | ||
"Australia", | ||
"Austria", | ||
"Belgium", | ||
"Canada", | ||
"Chile", | ||
"China", | ||
"Colombia", | ||
"Costa Rica", | ||
"Denmark", | ||
"Estonia", | ||
"Finland", | ||
"France", | ||
"Germany", | ||
"Greece", | ||
"Iceland", | ||
"Ireland", | ||
"Israel", | ||
"Italy", | ||
"Japan", | ||
"Rep. of Korea", | ||
"Latvia", | ||
"Lithuania", | ||
"Luxembourg", | ||
"Mexico", | ||
"Netherlands", | ||
"New Zealand", | ||
"Norway", | ||
"Poland", | ||
"Portugal", | ||
"Slovakia", | ||
"Slovenia", | ||
"Spain", | ||
"Sweden", | ||
"Switzerland", | ||
"Turkey", | ||
"United Kingdom", | ||
"USA", | ||
"Hungary", | ||
"Czechia", | ||
] | ||
|
||
ListofMetalName = [ | ||
"Graphite", | ||
"Baryte", | ||
"Magnesium", | ||
"Gypsum", | ||
"Asbestos", | ||
"Feldspar", | ||
"Fluorspar", | ||
"Iron", | ||
"Manganese", | ||
"Copper", | ||
"Nickel", | ||
"Aluminium", | ||
"Lead", | ||
"Zinc", | ||
"Tin", | ||
"Chromium", | ||
"Tungsten", | ||
"Uranium", | ||
"Molybdenum", | ||
"Titanium", | ||
"Zirconium", | ||
"Niobium", | ||
"Silver", | ||
"Antimony", | ||
"Coal", | ||
"Lignite", | ||
"Crude oil", | ||
"Natural gas", | ||
"Boron", | ||
"Arsenic", | ||
"Selenium", | ||
"Mercury", | ||
"Cobalt", | ||
"Germanium", | ||
"Lithium", | ||
"Rare earth", | ||
"Gold", | ||
"Platinum", | ||
"Palladium", | ||
"Rhodium", | ||
"Cadmium", | ||
"Gallium", | ||
] | ||
|
||
|
||
import random | ||
|
||
|
||
def random_sampler(A, n): | ||
return random.sample(A, n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Copyright (C) 2024 University of Bordeaux, CyVi Group & University of Bayreuth, | ||
# Ecological Resource Technology & Anish Koyamparambath, Christoph Helbig, Thomas Schraml | ||
# This file is part of geopolrisk-py library. | ||
# geopolrisk-py is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# geopolrisk-py is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# You should have received a copy of the GNU General Public License | ||
# along with geopolrisk-py. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import unittest | ||
from unittest.mock import patch | ||
from .defaults import * | ||
import pandas as pd | ||
from geopolrisk.assessment.utils import ( | ||
replace_func, | ||
cvtresource, | ||
cvtcountry, | ||
sumproduct, | ||
create_id, | ||
createresultsdf, | ||
writetodb, | ||
getbacidata, | ||
aggregateTrade, | ||
getProd, | ||
regions, | ||
) | ||
|
||
# Extracting samples data from a list of resources, countries and years | ||
Resources = random_sampler(ListofMetals, 5) | ||
Countries = random_sampler(ListofCountries, 5) | ||
Year = random_sampler(ListofYears, 5) | ||
listofcountryname, listofresourcename = [], [] | ||
for i, n in enumerate(Resources): | ||
listofcountryname.append(ListofCountryName[ListofCountries.index(Countries[i])]) | ||
listofresourcename.append(ListofMetalName[ListofMetals.index(n)]) | ||
|
||
|
||
class TestGeoPolRisk(unittest.TestCase): | ||
|
||
def test_cvtresource(self): | ||
self.assertEqual(cvtresource(listofresourcename[0], type="HS"), Resources[0]) | ||
self.assertRaises(ValueError, cvtresource, "Unknown", type="HS") | ||
self.assertEqual(cvtresource(Resources[0], type="Name"), listofresourcename[0]) | ||
self.assertRaises(ValueError, cvtresource, "Unknown", type="Name") | ||
|
||
def test_cvtcountry(self): | ||
self.assertEqual(cvtcountry(listofcountryname[0], type="ISO"), Countries[0]) | ||
self.assertRaises(ValueError, cvtcountry, "Unknown", type="ISO") | ||
self.assertEqual(cvtcountry(Countries[0], type="Name"), listofcountryname[0]) | ||
self.assertRaises(ValueError, cvtcountry, "Unknown", type="Name") | ||
|
||
def test_sumproduct(self): | ||
self.assertEqual(sumproduct([1, 2, 3], [4, 5, 6]), 32) | ||
self.assertEqual(sumproduct([], []), 0) | ||
|
||
def test_create_id(self): | ||
self.assertEqual(create_id("1001", "FRA", "2022"), "1001FRA2022") | ||
|
||
@patch("geopolrisk.assessment.database.execute_query") | ||
@patch("geopolrisk.assessment.database.databases") | ||
def test_createresultsdf(self, mock_databases, mock_execute_query): | ||
mock_databases.directory = "/mock/path" | ||
mock_databases.Output = "mock_output.db" | ||
df = createresultsdf() | ||
self.assertTrue(isinstance(df, pd.DataFrame)) | ||
|
||
@patch("geopolrisk.assessment.database.execute_query") | ||
@patch("geopolrisk.assessment.database.databases") | ||
def test_writetodb(self, mock_databases, mock_execute_query): | ||
mock_databases.directory = "/mock/path" | ||
mock_databases.Output = "mock_output.db" | ||
mock_df = pd.DataFrame( | ||
{ | ||
"DBID": ["1"], | ||
"Country [Economic Entity]": ["France"], | ||
"Raw Material": ["Gold"], | ||
"Year": [2022], | ||
"GeoPolRisk Score": [1.5], | ||
"GeoPolRisk Characterization Factor [eq. Kg-Cu/Kg]": [0.8], | ||
"HHI": [0.5], | ||
"Import Risk": [0.3], | ||
"Price": [1000], | ||
} | ||
) | ||
writetodb(mock_df) | ||
mock_execute_query.assert_called() | ||
|
||
@patch("geopolrisk.assessment.database.databases") | ||
def test_getbacidata(self, mock_databases): | ||
mock_databases.baci_trade = pd.DataFrame( | ||
{ | ||
"period": ["2022"], | ||
"reporterCode": ["FRA"], | ||
"cmdCode": ["1001"], | ||
"qty": [10], | ||
"cifvalue": [100], | ||
} | ||
) | ||
df = getbacidata("2022", "FRA", "1001", data=mock_databases.baci_trade) | ||
self.assertFalse(df.empty) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
from geopolrisk.assessment.main import gprs_calc | ||
from geopolrisk.assessment.utils import regions | ||
|
||
NewRegions = { | ||
"EFTA": ["Iceland", "Norway", "Switzerland"], | ||
"NAFTA": ["Canada", "Mexico", "USA"], | ||
} | ||
|
||
ListofMetals = [ | ||
260400, | ||
] | ||
ListofCountries = ["EU"] | ||
ListofYear = [2022] | ||
regions(NewRegions) | ||
gprs_calc(ListofYear, ListofCountries, ListofMetals) | ||
import unittest, os | ||
from geopolrisk.tests.test_case_utils import TestGeoPolRisk | ||
|
||
|
||
def main(): | ||
suite = unittest.TestSuite() | ||
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestGeoPolRisk)) | ||
unittest.TextTestRunner(verbosity=3).run(suite) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |