Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPJ: adding path for loading and saving results for synthetic data #180

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 49 additions & 48 deletions leakpro/synthetic_data_attacks/inference_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,82 +35,81 @@ class InferenceResults(BaseModel):
secrets: List[str]
worst_case_flag: bool

def save(self:Self,
path: str = "../leakpro_output/results/",
name: str = "inference",
case_flag:str = "base", # noqa: ARG002
config: dict = None # noqa: ARG002
) -> None:
def save(self: Self,
path: str = "../leakpro_output/results/",
name: str = "inference",
case_flag: str = "base", # noqa: ARG002
config: dict = None # noqa: ARG002
) -> None:
"""Save method for InferenceResults."""

# Data to be saved
data = {
"resulttype": self.__class__.__name__,
"resultname": name,
"res": self.model_dump(),
}

# Check if path exists, otherwise create it.
for _ in range(3):
if os.path.exists(path):
break
path = "../" + path

# If no result folder can be found
if not os.path.exists(path):
os.makedirs("../../leakpro_output/results/")

# Save the results to a file
if not os.path.exists(f"{path}/inference_risk/{name}"):
os.makedirs(f"{path}/inference_risk/{name}")

with open(f"{path}/inference_risk/{name}/data.json", "w") as f:
json.dump(data, f)


self.plot(worst_case_flag=self.worst_case_flag,
show=False,
save=True,
save_path=f"{path}/inference_risk/{name}",
save_name=name
)
self.plot(
worst_case_flag = self.worst_case_flag,
show = False,
save = True,
save_path = f"{path}/inference_risk/{name}",
save_name = name
)

@staticmethod
def load(data: dict) -> "InferenceResults":
"""Load method for InferenceResults."""
return InferenceResults(res=data["res"]["res"],
res_cols=data["res"]["res_cols"],
aux_cols=data["res"]["aux_cols"],
secrets=data["res"]["secrets"],
worst_case_flag=data["res"]["worst_case_flag"])
return InferenceResults(
res = data["res"]["res"],
res_cols = data["res"]["res_cols"],
aux_cols = data["res"]["aux_cols"],
secrets = data["res"]["secrets"],
worst_case_flag = data["res"]["worst_case_flag"]
)

def plot(self:Self,
high_res_flag: bool = False,
worst_case_flag: bool = False,
show: bool = True,
save: bool = False,
save_path: str = "./",
save_name: str = "fig.png"
) -> None:
def plot(
self:Self,
high_res_flag: bool = False,
worst_case_flag: bool = False,
show: bool = True,
save: bool = False,
save_path: str = "./",
save_name: str = "fig.png"
) -> None:
"""Plot method for InferenceResults."""
from leakpro.synthetic_data_attacks.plots import plot_ir_base_case, plot_ir_worst_case

plot_inference = plot_ir_worst_case if worst_case_flag else plot_ir_base_case
plot_inference(inf_res=InferenceResults(res=self.res,
res_cols=self.res_cols,
aux_cols=self.aux_cols,
secrets=self.secrets,
worst_case_flag=self.worst_case_flag),
high_res_flag=high_res_flag,
show=show,
save=save,
save_name=f"{save_path}/{save_name}")
plot_inference(
inf_res = InferenceResults(
res = self.res,
res_cols = self.res_cols,
aux_cols = self.aux_cols,
secrets = self.secrets,
worst_case_flag = self.worst_case_flag
),
high_res_flag = high_res_flag,
show = show,
save = save,
save_name = f"{save_path}/{save_name}"
)

@staticmethod
def create_results(results: list, save_dir: str = "./") -> str:
"""Result method for InferenceResults."""
latex = ""

def _latex(save_dir: str, save_name: str) -> str:
"""Latex method for InferenceResults."""
filename = f"{save_dir}/{save_name}.png"
Expand All @@ -121,10 +120,8 @@ def _latex(save_dir: str, save_name: str) -> str:
\\caption{{Original}}
\\end{{figure}}
"""

for res in results:
name = "inference_"+("worst_case" if res.worst_case_flag else "base_case")

res.plot(show=False, save=True, save_path=save_dir, save_name=name)
latex += _latex(save_dir=save_dir, save_name=name)
return latex
Expand Down Expand Up @@ -182,6 +179,7 @@ def inference_risk_evaluation(
dataset: str = "test",
verbose: bool = False,
save_results_json: bool = False,
path: str = None,
**kwargs: dict
) -> InferenceResults:
"""Perform a full inference risk evaluation.
Expand All @@ -208,6 +206,8 @@ def inference_risk_evaluation(
If True, prints progress of evaluation.
save_results_json: bool, default is False
If True, saves results and combinations to json file.
path: str, default is None
Path where to save json results file.
kwargs: dict
Other keyword arguments for InferenceEvaluator.

Expand Down Expand Up @@ -271,11 +271,12 @@ def inference_risk_evaluation(
save_res_json_file(
prefix = prefix,
dataset = dataset,
res = inf_res.model_dump()
res = inf_res.model_dump(),
path = path
)
return inf_res

def load_inference_results(*, dataset: str, worst_case_flag: bool = True) -> InferenceResults:
def load_inference_results(*, dataset: str, worst_case_flag: bool = True, path: str = None) -> InferenceResults:
"""Function to load and return inference results from given dataset."""
prefix = get_inference_prefix(worst_case_flag=worst_case_flag)
return InferenceResults(**load_res_json_file(prefix=prefix, dataset=dataset))
return InferenceResults(**load_res_json_file(prefix=prefix, dataset=dataset, path=path))
62 changes: 37 additions & 25 deletions leakpro/synthetic_data_attacks/linkability_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,36 +195,34 @@ class LinkabilityResults(BaseModel):
res: List[List[Union[int,float]]]
aux_cols: List[List[List[str]]]

def save(self:Self, path: str = "../leakpro_output/results/", name: str = "linkability", config: dict = None) -> None: # noqa: ARG002
def save(
self: Self,
path: str = "../leakpro_output/results/",
name: str = "linkability",
config: dict = None # noqa: ARG002
) -> None:
"""Save method for LinkabilityResults."""

id = "linkability"

# Data to be saved
data = {
"resulttype": self.__class__.__name__,
"resultname": name,
"res": self.model_dump(),
"id": id,
}

# Check if path exists, otherwise create it.
for _ in range(3):
if os.path.exists(path):
break
path = "../" + path

# If no result folder can be found
if not os.path.exists(path):
os.makedirs("../../leakpro_output/results/")

# Save the results to a file
if not os.path.exists(f"{path}/{name}/{id}"):
os.makedirs(f"{path}/{name}/{id}")

with open(f"{path}/{name}/{id}/data.json", "w") as f:
json.dump(data, f)

self.plot(show=False,
save=True,
save_path=f"{path}",
Expand All @@ -233,27 +231,38 @@ def save(self:Self, path: str = "../leakpro_output/results/", name: str = "linka
@staticmethod
def load(data: dict) -> "LinkabilityResults":
"""Load method for LinkabilityResults."""
return LinkabilityResults(res=data["res"]["res"],
res_cols=data["res"]["res_cols"],
aux_cols=data["res"]["aux_cols"])
return LinkabilityResults(
res = data["res"]["res"],
res_cols = data["res"]["res_cols"],
aux_cols = data["res"]["aux_cols"]
)

def plot(self:Self, high_res_flag: bool = False, show: bool = True, save: bool = False,
save_path: str = "./", save_name: str = "fig.png") -> None:
def plot(
self: Self,
high_res_flag: bool = False,
show: bool = True,
save: bool = False,
save_path: str = "./",
save_name: str = "fig.png"
) -> None:
"""Plot method for LinkabilityResults."""
from leakpro.synthetic_data_attacks.plots import plot_linkability
plot_linkability(link_res=LinkabilityResults(res=self.res,
res_cols=self.res_cols,
aux_cols=self.aux_cols),
high_res_flag=high_res_flag,
show=show,
save=save,
save_name=f"{save_path}/{save_name}")
plot_linkability(
link_res = LinkabilityResults(
res = self.res,
res_cols = self.res_cols,
aux_cols = self.aux_cols
),
high_res_flag = high_res_flag,
show = show,
save = save,
save_name = f"{save_path}/{save_name}"
)

@staticmethod
def create_results(results: list, save_dir: str = "./") -> str:
"""Result method for LinkabilityResults."""
latex = ""

def _latex(save_dir: str, save_name: str) -> str:
"""Latex method for LinkabilityResults."""
filename = f"{save_dir}/{save_name}.png"
Expand All @@ -264,7 +273,6 @@ def _latex(save_dir: str, save_name: str) -> str:
\\caption{{Original}}
\\end{{figure}}
"""

for res in results:
res.plot(show=False, save=True, save_path=save_dir, save_name="linkability")
latex += _latex(save_dir=save_dir, save_name="linkability")
Expand All @@ -277,6 +285,7 @@ def linkability_risk_evaluation(
dataset: str = "test",
verbose: bool = False,
save_results_json: bool = False,
path: str = None,
**kwargs: dict
) -> LinkabilityResults:
"""Perform a full linkability risk evaluation.
Expand All @@ -299,6 +308,8 @@ def linkability_risk_evaluation(
If True, prints progress of evaluation.
save_results_json: bool, default is False
If True, saves results and combinations to json file.
path: str, default is None
Path where to save json results file.
kwargs: dict
Other keyword arguments for LinkabilityEvaluator.

Expand Down Expand Up @@ -344,10 +355,11 @@ def linkability_risk_evaluation(
save_res_json_file(
prefix = "linkability",
dataset = dataset,
res = link_full_res.model_dump()
res = link_full_res.model_dump(),
path = path
)
return link_full_res

def load_linkability_results(*, dataset: str) -> LinkabilityResults:
def load_linkability_results(*, dataset: str, path: str = None) -> LinkabilityResults:
"""Function to load and return linkability results from given dataset."""
return LinkabilityResults(**load_res_json_file(prefix="linkability", dataset=dataset))
return LinkabilityResults(**load_res_json_file(prefix="linkability", dataset=dataset, path=path))
Original file line number Diff line number Diff line change
Expand Up @@ -143976,5 +143976,6 @@
"income",
"income",
"income"
]
],
"worst_case_flag": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,6 @@
"hr_per_week",
"country",
"income"
]
],
"worst_case_flag": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
0.11556078021516172,
1
]
]
],
"prefix": "singling_out_n_cols_1",
"dataset": "adults"
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,7 @@
0.0,
14
]
]
],
"prefix": "singling_out_n_cols_all",
"dataset": "adults"
}
Loading
Loading