Skip to content

Commit

Permalink
Merge pull request #13 from yasirroni/to_xlsx_prefix_suffix
Browse files Browse the repository at this point in the history
add prefix suffix support to sheet name
  • Loading branch information
yasirroni authored Sep 30, 2024
2 parents 5af2479 + a60cb0c commit 023d889
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ dmypy.json

# Editor and workspace
.vscode

# Mac
.DS_Store
24 changes: 16 additions & 8 deletions matpowercaseframes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,14 @@ def _infer_numpy(df):
df[columns] = df[columns].astype(bool)
return df

def to_excel(self, path):
def to_excel(self, path, prefix="", suffix=""):
"""
Save the CaseFrames data into a single Excel file.
Args:
path (str): File path for the Excel file.
prefix (str): Sheet prefix for each attribute for the Excel file.
suffix (str): Sheet suffix for each attribute for the Excel file.
"""

# make dir
Expand All @@ -342,23 +344,27 @@ def to_excel(self, path):
"baseMVA": getattr(self, "baseMVA", None),
}
}
).to_excel(writer, sheet_name="info")
).to_excel(writer, sheet_name=f"{prefix}info{suffix}")
for attribute in self._attributes:
if attribute == "version" or attribute == "baseMVA":
continue
elif attribute in ["bus_name", "branch_name", "gen_name"]:
pd.DataFrame(data={attribute: getattr(self, attribute)}).to_excel(
writer, sheet_name=attribute
writer, sheet_name=f"{prefix}{attribute}{suffix}"
)
else:
getattr(self, attribute).to_excel(writer, sheet_name=attribute)
getattr(self, attribute).to_excel(
writer, sheet_name=f"{prefix}{attribute}{suffix}"
)

def to_csv(self, path):
def to_csv(self, path, prefix="", suffix=""):
"""
Save the CaseFrames data into multiple CSV files.
Args:
path (str): Directory path where the CSV files will be saved.
prefix (str): Sheet prefix for each attribute for the CSV files.
suffix (str): Sheet suffix for each attribute for the CSV files.
"""
# make dir
os.makedirs(path, exist_ok=True)
Expand All @@ -370,17 +376,19 @@ def to_csv(self, path):
"baseMVA": getattr(self, "baseMVA", None),
}
}
).to_csv(os.path.join(path, "info.csv"))
).to_csv(os.path.join(path, f"{prefix}info{suffix}.csv"))

for attribute in self._attributes:
if attribute == "version" or attribute == "baseMVA":
continue
elif attribute in ["bus_name", "branch_name", "gen_name"]:
pd.DataFrame(data={attribute: getattr(self, attribute)}).to_csv(
os.path.join(path, f"{attribute}.csv")
os.path.join(path, f"{prefix}{attribute}{suffix}.csv")
)
else:
getattr(self, attribute).to_csv(os.path.join(path, f"{attribute}.csv"))
getattr(self, attribute).to_csv(
os.path.join(path, f"{prefix}{attribute}{suffix}.csv")
)

def to_dict(self):
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/results/mpc.branch_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
,F_BUS,T_BUS,BR_R,BR_X,BR_B,RATE_A,RATE_B,RATE_C,TAP,SHIFT,BR_STATUS,ANGMIN,ANGMAX
1,1,4,0.0,0.0576,0.0,250,250,250,0,0,1,-360,360
2,4,5,0.017,0.092,0.158,250,250,250,0,0,1,-360,360
3,5,6,0.039,0.17,0.358,150,150,150,0,0,1,-360,360
4,3,6,0.0,0.0586,0.0,300,300,300,0,0,1,-360,360
5,6,7,0.0119,0.1008,0.209,150,150,150,0,0,1,-360,360
6,7,8,0.0085,0.072,0.149,250,250,250,0,0,1,-360,360
7,8,2,0.0,0.0625,0.0,250,250,250,0,0,1,-360,360
8,8,9,0.032,0.161,0.306,250,250,250,0,0,1,-360,360
9,9,4,0.01,0.085,0.176,250,250,250,0,0,1,-360,360
10 changes: 10 additions & 0 deletions tests/results/mpc.bus_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
,BUS_I,BUS_TYPE,PD,QD,GS,BS,BUS_AREA,VM,VA,BASE_KV,ZONE,VMAX,VMIN
1,1,3,0,0,0,0,1,1,0,345,1,1.1,0.9
2,2,2,0,0,0,0,1,1,0,345,1,1.1,0.9
3,3,2,0,0,0,0,1,1,0,345,1,1.1,0.9
4,4,1,0,0,0,0,1,1,0,345,1,1.1,0.9
5,5,1,90,30,0,0,1,1,0,345,1,1.1,0.9
6,6,1,0,0,0,0,1,1,0,345,1,1.1,0.9
7,7,1,100,35,0,0,1,1,0,345,1,1.1,0.9
8,8,1,0,0,0,0,1,1,0,345,1,1.1,0.9
9,9,1,125,50,0,0,1,1,0,345,1,1.1,0.9
4 changes: 4 additions & 0 deletions tests/results/mpc.gen_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
,GEN_BUS,PG,QG,QMAX,QMIN,VG,MBASE,GEN_STATUS,PMAX,PMIN,PC1,PC2,QC1MIN,QC1MAX,QC2MIN,QC2MAX,RAMP_AGC,RAMP_10,RAMP_30,RAMP_Q,APF
1,1,72.3,27.03,300,-300,1.04,100,1,250,10,0,0,0,0,0,0,0,0,0,0,0
2,2,163.0,6.54,300,-300,1.025,100,1,300,10,0,0,0,0,0,0,0,0,0,0,0
3,3,85.0,-10.95,300,-300,1.025,100,1,270,10,0,0,0,0,0,0,0,0,0,0,0
4 changes: 4 additions & 0 deletions tests/results/mpc.gencost_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
,MODEL,STARTUP,SHUTDOWN,NCOST,COST_2,COST_1,COST_0
1,2,1500,0,3,0.11,5.0,150
2,2,2000,0,3,0.085,1.2,600
3,2,3000,0,3,0.1225,1.0,335
3 changes: 3 additions & 0 deletions tests/results/mpc.info_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,INFO
version,2
baseMVA,100
10 changes: 10 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,24 @@ def test_get_attributes():
# print(cf.attributes)


# !WARNING: Refactor to fixture to read file is proven to be slower
# pytest -n auto --durations=0


def test_to_xlsx():
cf = CaseFrames(CASE_PATH)
cf.to_excel("tests/results/test_to_xlsx.xlsx")
cf.to_excel(
"tests/results/test_to_xlsx_prefix_suffix.xlsx",
prefix="mpc.",
suffix="_test",
)


def test_to_csv():
cf = CaseFrames(CASE_PATH)
cf.to_csv("tests/results")
cf.to_csv("tests/results", prefix="mpc.", suffix="_test")


def test_to_dict():
Expand Down

0 comments on commit 023d889

Please sign in to comment.