Skip to content

Commit

Permalink
Merge pull request #65 from it-is-me-mario/dev
Browse files Browse the repository at this point in the history
Limiting pandas version to avoid problem with pandas 2.0.0
  • Loading branch information
mohammadamint committed May 30, 2023
2 parents b13a2a5 + 5a58f44 commit 0517539
Show file tree
Hide file tree
Showing 14 changed files with 966 additions and 1,010 deletions.
92 changes: 46 additions & 46 deletions mario/core/AttrData.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ def to_iot(
self._indeces = indeces
self.units = units


self.meta.table = "IOT"
self.meta._add_history(
"Transformation of the database from SUT to IOT via method {}".format(
Expand Down Expand Up @@ -420,7 +419,6 @@ def read_aggregated_index(
f"Following item are not acceptable for level {level} \n {difference}"
)


index.columns = ["Aggregation"]

if index.isnull().values.any():
Expand Down Expand Up @@ -505,8 +503,9 @@ def aggregate(
"""

# insure of Y,E,V,Z,EY exist
self.calc_all(["E", "V", "Z"])
# ensure of Y,E,V,Z,EY exist
for scenario in self.scenarios:
self.calc_all(["E", "V", "Z"],scenario=scenario)

if not inplace:
new = self.copy()
Expand Down Expand Up @@ -669,7 +668,14 @@ def add_extensions(
if not inplace:
new = self.copy()
new.add_extensions(
io=io, matrix=matrix, backup=backup, inplace=True, units=units,calc_all=calc_all,notes=notes,EY=EY
io=io,
matrix=matrix,
backup=backup,
inplace=True,
units=units,
calc_all=calc_all,
notes=notes,
EY=EY,
)

return new
Expand Down Expand Up @@ -760,7 +766,7 @@ def add_extensions(

units = info["units"]
del info["units"]

matrices = {"baseline": {**info}}

for scenario in self.scenarios:
Expand Down Expand Up @@ -957,8 +963,6 @@ def to_single_region(self, region, backup=True, inplace=True):
"Transformation: The Final Demand emissions are considered only for 'Local Final Demand.'"
)



def calc_linkages(
self, scenario="baseline", normalized=True, cut_diag=True, multi_mode=True,
):
Expand Down Expand Up @@ -1115,7 +1119,7 @@ def to_excel(
coefficients=False,
units=True,
scenario="baseline",
include_meta = False
include_meta=False,
):

"""Saves the database into an Excel file
Expand Down Expand Up @@ -1164,7 +1168,6 @@ def to_excel(
)
)


database_excel(
self,
flows,
Expand All @@ -1175,8 +1178,8 @@ def to_excel(
)
if include_meta:
meta = self.meta._to_dict()
with open(self._getdir(path, "Database", "")+"/metadata.json","w") as fp:
json.dump(meta,fp)
with open(self._getdir(path, "Database", "") + "/metadata.json", "w") as fp:
json.dump(meta, fp)

def to_txt(
self,
Expand All @@ -1187,7 +1190,8 @@ def to_txt(
scenario="baseline",
_format="txt",
include_meta = False,
sep = ','
sep = ',',

):

"""Saves the database multiple text file based on given inputs
Expand Down Expand Up @@ -1247,8 +1251,8 @@ def to_txt(

if include_meta:
meta = self.meta._to_dict()
with open(self._getdir(path, "Database", "")+"/metadata.json","w") as fp:
json.dump(meta,fp)
with open(self._getdir(path, "Database", "") + "/metadata.json", "w") as fp:
json.dump(meta, fp)

def to_pymrio(
self,
Expand Down Expand Up @@ -1512,11 +1516,7 @@ def add_sectors(
self.meta._add_history(f"User note: {note}")

def query(
self,
matrices,
scenarios = ["baseline"],
base_scenario = None,
type="absolute",
self, matrices, scenarios=["baseline"], base_scenario=None, type="absolute",
):
""" Requests a specific data from the database
Expand Down Expand Up @@ -1582,30 +1582,29 @@ def query(
indeces=False,
format="dict",
scenarios=scenarios,
base_scenario = base_scenario,
type = type
base_scenario=base_scenario,
type=type,
)


if len(matrices) == 1:
for scenario in scenarios:
data[scenario]= data[scenario][matrices[0]]
data[scenario] = data[scenario][matrices[0]]

if len(scenarios) == 1:
data = data[scenarios[0]]

return data

def get_data(
self,
matrices,
units= True,
indeces= True,
auto_calc= True,
format= "object",
scenarios= ["baseline"],
base_scenario= None,
type= "absolute",
units=True,
indeces=True,
auto_calc=True,
format="object",
scenarios=["baseline"],
base_scenario=None,
type="absolute",
):

"""Returns specific data and calculating them or the changes for scenarios in a database
Expand Down Expand Up @@ -1887,9 +1886,7 @@ def shock_calc(
raise WrongInput("baseline scenario can not be overwritten.")

check_clusters(
index_dict = self.get_index('all'),
table = self.table_type,
clusters = clusters
index_dict=self.get_index("all"), table=self.table_type, clusters=clusters
)

# have the test for the existence of the database
Expand Down Expand Up @@ -1949,9 +1946,7 @@ def get_shock_excel(
"""

check_clusters(
index_dict = self.get_index('all'),
table = self.table_type,
clusters = clusters
index_dict=self.get_index("all"), table=self.table_type, clusters=clusters
)

_sh_excel(self, num_shock, self._getdir(path, "Excels", "shock.xlsx"), clusters)
Expand Down Expand Up @@ -2095,7 +2090,7 @@ def plot_gdp(
extension_value="relative",
auto_open=True,
drop_reg=None,
title=None
title=None,
):

"""Plots sectoral GDP with additional info
Expand Down Expand Up @@ -2129,7 +2124,12 @@ def plot_gdp(
"""

plots = ["treemap", "sunburst"]
extension_values = ["relative", "absolute","specific footprint","absolute footprint"]
extension_values = [
"relative",
"absolute",
"specific footprint",
"absolute footprint",
]

if plot not in plots:
raise WrongInput(f"Acceptable plots are {plots}")
Expand All @@ -2153,15 +2153,15 @@ def plot_gdp(
if extension_value == "relative":
matrix = "e"
color = "{} [{}]/ Production"

elif extension_value == "specific footprint":
matrix = "f"
color = "{} [{}]/ Production"

elif extension_value == "absolute footprint":
matrix = "F"
color = "{} [{}]"

else:
matrix = "E"
color = "{} [{}]"
Expand Down Expand Up @@ -2196,17 +2196,17 @@ def plot_gdp(
values = "GDP"

if drop_reg == None:
data_frame=data_frame
data_frame = data_frame
else:
data_frame=data_frame.loc[data_frame.Region!=drop_reg]
data_frame = data_frame.loc[data_frame.Region != drop_reg]

fig = getattr(px, plot)(
data_frame=data_frame,
path=_path,
values=values,
color=color,
color_continuous_scale=px.colors.diverging.RdBu[::-1],
title=title
title=title,
)

path = r"{}".format(self._getdir(path, "Plots", f"GDP_{scenario}_{plot}.html"))
Expand Down
39 changes: 9 additions & 30 deletions mario/core/CoreIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from mario.tools.tableparser import dataframe_parser



from mario.tools.iomath import (
calc_X,
calc_Z,
Expand Down Expand Up @@ -153,7 +152,6 @@ def __init__(

log_time(logger, "Metadata: initialized by dataframes.")


# Adding notes if passed by user or the parsers
if kwargs.get("notes"):
for note in kwargs["notes"]:
Expand Down Expand Up @@ -314,9 +312,7 @@ def update_scenarios(self, scenario, **matrices):
self.matrices[scenario][matrix] = value

def clone_scenario(
self,
scenario,
name,
self, scenario, name,
):
"""Creates a new scenario by cloning an existing scenario
Expand Down Expand Up @@ -451,11 +447,7 @@ def get_index(self, index, level="main"):
return copy.deepcopy(self._indeces[_LEVELS[self.table_type][index]][level])

def is_balanced(
self,
method,
data_set="baseline",
margin=0.05,
as_dataframe=False,
self, method, data_set="baseline", margin=0.05, as_dataframe=False,
):

"""Checks if a specific data_set in the database is balance or not
Expand Down Expand Up @@ -739,11 +731,7 @@ def __repr__(self):
return self.__str__()

def GDP(
self,
exclude=[],
scenario="baseline",
total=True,
share=False,
self, exclude=[], scenario="baseline", total=True, share=False,
):

"""Return the value of the GDP based scenario.
Expand Down Expand Up @@ -805,22 +793,16 @@ def GDP(
)

if total:
return GDP.groupby(
level=0,
sort=False,
).sum()
return GDP.groupby(level=0, sort=False,).sum()

if share:
region_gdp = GDP.groupby(
level=0,
sort=False,
).sum()
region_gdp = GDP.groupby(level=0, sort=False,).sum()
share = GDP.div(region_gdp) * 100
GDP["Share of sector by region"] = share["GDP"]

return GDP

def search(self, item, search,ignore_case=True):
def search(self, item, search, ignore_case=True):
"""Searches for specific keywords in a given item
Parameters
Expand All @@ -846,7 +828,7 @@ def search(self, item, search,ignore_case=True):
items = self.get_index(item)

if ignore_case:
r = re.compile(f".*{search}",re.IGNORECASE)
r = re.compile(f".*{search}", re.IGNORECASE)
else:
r = re.compile(f".*{search}")

Expand Down Expand Up @@ -1004,7 +986,6 @@ def __getitem__(self, key):

return self.matrices[key]


def __iter__(self):
self.__it__ = self.scenarios
return self
Expand Down Expand Up @@ -1042,7 +1023,7 @@ def __getstate__(self):
def __setstate__(self, value):
self.__dict__ = value

def __eq__(self,other):
def __eq__(self, other):
""" Checks the equality if two databases
"""
main_sets = sorted(self.sets)
Expand All @@ -1060,8 +1041,6 @@ def __eq__(self,other):

return True



def backup(self):

"""The function creates a backup of the last configuration of database
Expand All @@ -1071,4 +1050,4 @@ def backup(self):
copy.deepcopy(self.matrices),
copy.deepcopy(self._indeces),
copy.deepcopy(self.units),
)
)
Loading

0 comments on commit 0517539

Please sign in to comment.