Package: geopolrisk-py
The geopolrisk-py library documentationGetting startedInstalling the GeoPolRisk ModuleThe geopolrisk-py is a python library that allows the assessment of a geopolitical related supply risk of a resource from the perspective of a country/region/trade bloc/company.
An assessment would result in two main values;GPRS - The probability of supply risk of importing a resource (0 - 1) usefull for comparative risk assessment,Characterization Factors for GSRP (Geopolitical supply risk potential indicator (Midpoint))
The library is available to download using pip package manager.pip install -i https://test.pypi.org/simple/Geopolrisk==2.5After installationA folder in the Documents directory of the installed user is created after importing the library for the first time named "geopolrisk". Three sub folders are created:'database','logs', and'output'. These are important for the working of the library. The'database'folder must contain the library database. Copy the library database to the directory created in the documents folder to use the library (https://github.com/akoyamp/geopolrisk-py/tree/main/geopolrisk-py/lib/library.db). Or you can provide the location of the database in the prompt asked when importing the library for the first time.
The'logs'folder stores the logs of the assessment. The'output'folder stores a database containing the results of the assessment and also all the exports of the results.Modules and MethodsThe library has following five modules preceded by an init that performs some actions required to smoothly perform an assessment.initmodule:--> Reads static databases : resource price data, normalized governance indicator data, commodity hs codes, country iso codes--> Creates a directory in documents folder (only for windows users): Output folder for database to record calculations and exporting results as csv, Log folder for storing logs--> Loads several functions such as logging, sql and some global variables--> The module has a class called instance. Variables in the instance class can be manipulated or used in specific applications. Important variables such as regionslist, price and wgi are manipulated in specific applications.core moduleContains functions that calculate the components of the geopolrisk methodsettradepath(path):Function to declare the path of the trade data as an excel or csv file.
To declare the path of the company trade data as an excel file. Must provide path an abosolute trade path. ex. c:/Users/UBx/Documents/tradepath.xlsxFormat for company trade dataFollow the format listed below:ReporterptCodeptTitleTradeQuantityGermany76Brazil53399700Germany156China73139615ptCode, ptTitle and TradeQuantity are mandatory data that needs to be in the file.codeDescriptionptCodeISO 3 digit code of the country from where a resource is imported.ptTitleThe name of the country from where a resource is imported.TradeQuantityQuantity of resource imported in kilograms.regions(*args):Define additional regions of assessment. By default all the countries and EU is included in the database.
To define a new region, a dictionary must be provided with key as the name of the region and values is a list of countries in the region.
All the values must be in string and must be exactly as in the ISO.{"West Europe": ["France", "Germany", "Italy", "Spain", "Portugal", "Belgium"]}worldtrade(year = "2010", country = "276", commodity = "2602"):Function to call the UN COMTRADE api for fetching the trade data.Mandatory arguments:year: The year of assessment (integer)country: The area of assessment (integer*: ISO code of the area [annexe])*commodity: The HS code of the commodity under assessment (integer)specifictrade(sheetname = None):Function to calculate the trade data using specific company data.
Function settradepath must be called to define the path of the company data.weightedtrade(period, TradeData = None, PIData = None, scenario = 0, recyclingrate = 0.00):Function to calculate the second component of the GeoPolRisk method.
The trade data from either the COMTRADE function or InputTrade function is required as an argument. PIData is the world governance indicator that is stored in a variable by the init module.
However, the argument is provided in case of use of other indicators.Note: The scenario does not affect the assessment if the value for recyclingrate is '0'.ProductionData(Resource, EconomicUnit):Function to calculate the HHI (first component of the GeoPolRisk Method) and local production quantity.
Arguments required are the name of the resource (not HS code) and the economic unit (country/existing or defined regions/defined economic blocs)GeoPolRisk(ProductionData, WTAData, Year, AVGPrice):Function to calculate the values of the GeoPolRisk method. The ProductionData is a list of HHi and local production quantity (result from theProductionDatafunction).
The WTAData is a list of the calculation involving trade. AVGPrice is the yearly average price of the resource. It provides a list of four values.
[HHI, WTA, GPRS, CF]Example to use the core modulefrom geopolrisk.assessment.core import *
from geopolrisk.assessment.__init__ import instance #Optional
_price = instance.price
_wgi = instance.wgi
Resource = "Nickel"
HS = 2604
Country = "Germany"
ISO = 276
Year = "2016"

regions()
TradeData = worldtrade(year = "2016", country = "276", commodity = "2604")
ProductionData = ProductionData(Resource, Country)
WTAData = weightedtrade(Year, TradeData = TradeData, PIData = _wgi, scenario = 0, recyclingrate = 0.00)


YearlyAveragePrice = 10203.98
YearlyAveragePrice =  _price[Year].tolist()[_price.HS.to_list().index(HS)] #Optional - A database already exists that can be used to fetch the price data.

result = GeoPolRisk(ProductionData, WTAData, Year, YearlyAveragePrice)main module:Contains aggregate functions to simplify assessment.main_complete (resourcelist, yearlist, countrylist, recyclingrate=0.0, scenario=0,sheetname=None,PIindicator=None):An aggregate function for a complete geopolitical related supply risk assessment. Instead of using the functions from the core module, a list of all the data is provided to get an output file with the results. This function records all the assessment into a database in the documents directory. This is to prevent repeated API calls to the COMTRADE as there is a limit to use the free API.
Its a complete function that acknowledges, regional and company level assessment. Two functions from the core module must be called before this function is called. If a custom region is considered for the assessment such as example below, the regions function must be called and the new custom region must be declared.CustomRegion = {"West Europe": ["France", "Germany", "Italy", "Spain", "Portugal", "Belgium"]}
from geopolrisk.assessment.core import regions
regions(CustomRegion)In case the data for the custom region is provided using an excel or csv file, the path to the file should be declared using settradepath function from the core module. (Must use the template provided in the tests folder:https://github.com/akoyamp/geopolrisk-py/tree/main/geopolrisk-py/tests). The sheetname should be provided in the sheetname argument in the function.{"West Europe": ["France", "Germany", "Italy", "Spain", "Portugal", "Belgium"]}
from geopolrisk.assessment.core import regions
regions(CustomRegion)
locationtotheexcelfile = "user/documents/folder/tradedata.xlsx"
settradepath(locationtotheexcelfile)Arguments:resourcelist: A list of all the resources for the assessment as HS commodity codes.countrylist: A list of all the countries under assessment as 3 digit ISO codes.yearlist: A list of all the years in integers.recyclingrate: The ratio of domestic recycling of the resource (typically 0 to 1) also can provide from 0 to 100.scenario: Refer tohttps://doi.org/10.1016/j.resconrec.2020.105108for more data. In this function, three scenarios are considered: 0 for no scenario, 1 for best case scenario, 2 for worst case scenariosheetname: In case data is provided through excel sheet, provide the sheetname (mandatory argument)PIindicator: In case of using other political instability indicator, provide the json or dataframe containing normalized values.Example to demonstrate the use of main_complete function.ListofMetals = [2602, 2601, 2603, 2846, 2614,]
ListofCountries = [36, 124, 97, 251, 276, 392, 826, 842,] 
ListofYear = [2017, 2018, 2019, 2020]

from geopolrisk.assessment.main import main_complete

main_complete(ListofMetals, ListofYear, ListofCountries, 0, 0, sheetname= None, PIindicator = None)Example to demonstrate the use of main_complete function using custom region and specific trade data.ListofMetals = [2602, 2601, 2603, 2846, 2614,]
ListofYear = [2017, 2018, 2019, 2020]
CustomRegion = {"West Europe": ["France", "Germany", "Italy", "Spain", "Portugal", "Belgium"]}
locationtotheexcelfile = "user/documents/folder/tradedata.xlsx" #Avoid if using COMTRADE
ListofCountries = ["West Europe"] 

from geopolrisk.assessment.main import main_complete
from geopolrisk.assessment.core import regions

regions(CustomRegion)

settradepath(locationtotheexcelfile) #Do not use if using COMTRADE

main_complete(ListofMetals, ListofYear, ListofCountries, 0, 0, sheetname= "test", PIindicator = None)startmain (resourcelist, yearlist, countrylist, recyclingrate=0.0, scenario=0,sheetname=None,PIindicator=None):Function to calculate the GeoPolRisk but doesnt have the ability to aggregate data for regional calculation. In case of company level assessment, declare the path to the trade data as above before calling this function.endmain():Exports the results of the assessment from thestartmainfunction to the output folder.Example to demonstrate the use of main_complete function using custom region and specific trade data.ListofMetals = [2602, 2601, 2603, 2846, 2614,]
ListofCountries = [36, 124, 97, 251, 276, 392, 826, 842,] 
ListofYear = [2017, 2018, 2019, 2020]
locationtotheexcelfile = "user/documents/folder/tradedata.xlsx" #Avoid if using COMTRADE


from geopolrisk.assessment.main import main_complete
from geopolrisk.assessment.core import regions

regions()
settradepath(locationtotheexcelfile) #Do not use if using COMTRADE

startmain(ListofMetals, ListofYear, ListofCountries, 0, 0, sheetname= "test", PIindicator = None)
endmain()udpate_cf, updateprice are functions to automatically update null data stored as a result of a failed API request and changes in price data (or updates).Other Modulesutils module has some functions that are useful for the working of library. It has functions that record data, convert data and exports data.generateCF(exportType="csv", orient=""):Usefull function to export all calculated data from the database to one of three formats: csv, excel, json.The console module has one method guided for a console input based assessment.The functions in gprsplots module read the output file. An assessment must be done before using this module. There are three ways of plotting the results.
trendplot for plotting the evolution of supply risk. Must have more than three years assessed.
indplot for comparing each resource or country for ONE particular year.
compareplot for stacked graphs for comparison for one year.ANNEXEidhsAluminium2606Antimony261710Asbestos2524Barytes2511Bismuth8106Cadmium8107Chromium2610Coal2701Cobalt810520Copper2603Gold7108Graphite2504Iron2601Lead2607Lithium283691Magnesite251910Magnesium251910Manganese2602Mercury280540Molybdenum2613Natural gas271111Nickel2604Petroleum2709Rare earth2846Silver261610Tin2609Titanium2614Tungsten2611Uranium261210Zinc2608Zirconium261510Country ISO codesISO CODES:https://www.iso.org/standard/63546.html