Skip to content

Commit

Permalink
Pn/new cta23 (#293)
Browse files Browse the repository at this point in the history
* First commit for new CTA file

* Updated filtering

* Suppress warning in file reading during test phase

* Update in poetry to P3.9

* Updated CTA filtering and LEI, ISIN code check

* Fixed error in notebook 4b

* Update dependencies

* Update to released URL

* fix logical error in sbti.py

* Final cleanup 1.0.7

* More cleanup

* docs update

* Update getting_started.rst

* Cleanup sbti.py

* Update install in NB#1
  • Loading branch information
mountainrambler committed May 12, 2023
1 parent 8c74dbd commit 15d65f3
Show file tree
Hide file tree
Showing 10 changed files with 800 additions and 710 deletions.
17 changes: 13 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@
"jupyter.notebookFileRoot": "${workspaceFolder}",
"python.testing.cwd": "${cwd}",
"python.defaultInterpreterPath": ".venv/bin/python",
"python.formatting.provider": "black",
"python.testing.unittestArgs": ["-v", "-s", "./test", "-p", "*test*.py"],
"python.formatting.provider": "none",
"python.testing.unittestArgs": [
"-v",
"-s",
"./test",
"-p",
"*test*.py"
],
"restructuredtext.confPath": "${workspaceFolder}/docs",
"esbonio.sphinx.confDir": "${workspaceFolder}/docs"
}
"esbonio.sphinx.confDir": "${workspaceFolder}/docs",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}
15 changes: 9 additions & 6 deletions SBTi/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,26 @@ class PortfolioCoverageTVPConfig(PortfolioAggregationConfig):
FILE_TARGETS = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"inputs",
"current-Companies-Taking-Action-191.xlsx",
"current-Companies-Taking-Action.xlsx",
)
CTA_FILE_URL = "https://sciencebasedtargets.org/download/excel"
# Temporary URL until the SBTi website is updated
CTA_FILE_URL = "https://sciencebasedtargets.org/download/target-dashboard"
OUTPUT_TARGET_STATUS = "sbti_target_status"
OUTPUT_WEIGHTED_TARGET_STATUS = "weighted_sbti_target_status"
VALUE_TARGET_NO = "No target"
VALUE_TARGET_COMMITTED = "Committed"
VALUE_TARGET_SET = "Targets Set"
VALUE_TARGET_SET = "Near-term"
VALUE_ACTION_COMMITTED = "Commitment"
VALUE_ACTION_TARGET = "Target"

TARGET_SCORE_MAP = {
VALUE_TARGET_NO: 0,
VALUE_TARGET_COMMITTED: 0,
VALUE_ACTION_COMMITTED: 0,
VALUE_TARGET_SET: 100,
}

# SBTi targets overview (TVP coverage)
COL_COMPANY_NAME = "Company Name"
COL_COMPANY_ISIN = "ISIN"
COL_COMPANY_LEI = "LEI"
COL_TARGET_STATUS = "Near term - Target Status"
COL_ACTION = "Action"
COL_TARGET = "Target"
77 changes: 55 additions & 22 deletions SBTi/data/sbti.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import List, Type
import requests
import pandas as pd
import warnings


from SBTi.configs import PortfolioCoverageTVPConfig
from SBTi.interfaces import IDataProviderCompany
Expand All @@ -22,30 +24,56 @@ def __init__(
output.write(resp.content)
print(f'Status code from fetching the CTA file: {resp.status_code}, 200 = OK')
# Read CTA file into pandas dataframe
# Suppress warning about openpyxl - check if this is still needed in the released version.
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
self.targets = pd.read_excel(self.c.FILE_TARGETS)


def filter_cta_file(self, targets):
"""
Filter the CTA file to create a datafram that has on row per company
with the columns "Action" and "Target".
If Action = Target then only keep the rows where Target = Near-term.
"""

# def get_sbti_targets(
# self, companies: List[IDataProviderCompany], isin_map: dict
# ) -> List[IDataProviderCompany]:
# """
# Check for each company if they have an SBTi validated target.

# :param companies: A list of IDataProviderCompany instances
# :param isin_map: A map from company id to ISIN
# :return: A list of IDataProviderCompany instances, supplemented with the SBTi information
# """
# for company in companies:
# targets = self.targets[
# self.targets[self.c.COL_COMPANY_ISIN]
# == isin_map.get(company.company_id)
# ]
# if len(targets) > 0:
# company.sbti_validated = (
# self.c.VALUE_TARGET_SET in targets[self.c.COL_TARGET_STATUS].values
# )

# return companies
# Create a new dataframe with only the columns "Action" and "Target"
# and the columns that are needed for identifying the company
targets = targets[
[
self.c.COL_COMPANY_NAME,
self.c.COL_COMPANY_ISIN,
self.c.COL_COMPANY_LEI,
self.c.COL_ACTION,
self.c.COL_TARGET
]
]

# Keep rows where Action = Target and Target = Near-term
df_nt_targets = targets[
(targets[self.c.COL_ACTION] == self.c.VALUE_ACTION_TARGET) &
(targets[self.c.COL_TARGET] == self.c.VALUE_TARGET_SET)]

# Drop duplicates in the dataframe by waterfall.
# Do company name last due to risk of misspelled names
# First drop duplicates on LEI, then on ISIN, then on company name
df_nt_targets = pd.concat([
df_nt_targets[~df_nt_targets[self.c.COL_COMPANY_LEI].isnull()].drop_duplicates(
subset=self.c.COL_COMPANY_LEI, keep='first'
),
df_nt_targets[df_nt_targets[self.c.COL_COMPANY_LEI].isnull()]
])

df_nt_targets = pd.concat([
df_nt_targets[~df_nt_targets[self.c.COL_COMPANY_ISIN].isnull()].drop_duplicates(
subset=self.c.COL_COMPANY_ISIN, keep='first'
),
df_nt_targets[df_nt_targets[self.c.COL_COMPANY_ISIN].isnull()]
])

df_nt_targets.drop_duplicates(subset=self.c.COL_COMPANY_NAME, inplace=True)

return df_nt_targets

def get_sbti_targets(
self, companies: List[IDataProviderCompany], id_map: dict
) -> List[IDataProviderCompany]:
Expand All @@ -57,6 +85,9 @@ def get_sbti_targets(
:param id_map: A map from company id to a tuple of (ISIN, LEI)
:return: A list of IDataProviderCompany instances, supplemented with the SBTi information
"""
# Filter out information about targets
self.targets = self.filter_cta_file(self.targets)

for company in companies:
isin, lei = id_map.get(company.company_id)
# Check lei and length of lei to avoid zeros
Expand All @@ -72,6 +103,8 @@ def get_sbti_targets(
continue
if len(targets) > 0:
company.sbti_validated = (
self.c.VALUE_TARGET_SET in targets[self.c.COL_TARGET_STATUS].values
self.c.VALUE_TARGET_SET in targets[self.c.COL_TARGET].values
)
return companies


8 changes: 5 additions & 3 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ methodology <https://sciencebasedtargets.org/wp-content/uploads/2020/09/Temperat
1. `Analysis example (with abbreviated methodology) <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/1_analysis_example.ipynb>`__
2. `Quick temperature calculation <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/2_quick_temp_score_calculation.ipynb>`__
3. `What-if analysis <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/3_what-if-analysis.ipynb>`__
4. `Portfolio aggregation <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/4_portfolio_aggregations.ipynb>`__
5. `Aggregation of ready made scores <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/4b_portfolio_agg_readymade_TS.ipynb>`__
6. `Reporting <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/5_reporting.ipynb>`__
4. Portfolio aggregation
- 4a `Portfolio aggregation of scores generated by user <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/4_portfolio_aggregations.ipynb>`__
- 4b `Aggregation of ready made scores from data providers <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/4b_portfolio_agg_readymade_TS.ipynb>`__
5. `Reporting <https://colab.research.google.com/github/ScienceBasedTargets/SBTi-finance-tool/blob/main/examples/5_reporting.ipynb>`__


Jupyter Notebooks
-----------------
Expand Down
8 changes: 2 additions & 6 deletions examples/1_analysis_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,7 @@
},
"outputs": [],
"source": [
"%pip install sbti-finance-tool\n",
"\n",
"# Install other Python modules required to use this notebook\n",
"%pip install matplotlib==3.2.2\n",
"%pip install openpyxl==3.0.9"
"%pip install sbti-finance-tool "
]
},
{
Expand Down Expand Up @@ -1144,7 +1140,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.9.6"
},
"pycharm": {
"stem_cell": {
Expand Down
2 changes: 1 addition & 1 deletion examples/4b_portfolio_agg_readymade_TS.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"outputs": [],
"source": [
"# Create a temperature score instance with the desired time frames and scopes\n",
"ts = TemperatureScore(time_frames=list(ETimeFrames.SHORT, ETimeFrames.MID, ETimeFrames.LONG), \n",
"ts = TemperatureScore(time_frames=[ETimeFrames.SHORT, ETimeFrames.MID, ETimeFrames.LONG], \n",
" scopes=[EScope.S1S2, EScope.S3, EScope.S1S2S3])"
]
},
Expand Down
Loading

0 comments on commit 15d65f3

Please sign in to comment.