Skip to content

Commit

Permalink
fix memory issue in retrieval_eval.py script
Browse files Browse the repository at this point in the history
  • Loading branch information
LckyLke committed Nov 14, 2024
1 parent 77fae24 commit 537f786
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions examples/retrieval_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
from tqdm import tqdm
import random
import itertools
import ast

# Set pandas options to ensure full output
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.colheader_justify', 'left')
pd.set_option('display.expand_frame_repr', False)

def execute(args):
# (1) Initialize knowledge base.
Expand All @@ -84,33 +89,25 @@ def execute(args):
# GENERATE DL CONCEPTS TO EVALUATE RETRIEVAL PERFORMANCES
# (3) R: Extract object properties.
object_properties = sorted({i for i in symbolic_kb.get_object_properties()})

# (3.1) Subsample if required.
if args.ratio_sample_object_prop:
object_properties = {i for i in random.sample(population=list(object_properties),
k=max(0, int(len(object_properties) * args.ratio_sample_object_prop)))}

object_properties = set(object_properties)

# (4) R⁻: Inverse of object properties.
object_properties_inverse = {i.get_inverse_property() for i in object_properties}

# (5) R*: R UNION R⁻.
object_properties_and_inverse = object_properties.union(object_properties_inverse)
# (6) NC: Named owl concepts.
nc = sorted({i for i in symbolic_kb.get_concepts()})




if args.ratio_sample_nc:
# (6.1) Subsample if required.
nc = {i for i in random.sample(population=list(nc), k=max(0, int(len(nc) * args.ratio_sample_nc)))}

nc = set(nc) # return to a set
# (7) NC⁻: Complement of NC.
nnc = {i.get_object_complement_of() for i in nc}

# (8) NC*: NC UNION NC⁻.
nc_star = nc.union(nnc)
# (9) Retrieve 10 random Nominals.
Expand All @@ -120,7 +117,6 @@ def execute(args):
nominals = symbolic_kb.all_individuals_set()
# (10) All combinations of 3 for Nominals, e.g. {martin, heinz, markus}
nominal_combinations = set( OWLObjectOneOf(combination)for combination in itertools.combinations(nominals, 3))

# (11) NC UNION NC.
unions = concept_reducer(nc, opt=OWLObjectUnionOf)
# (12) NC INTERSECTION NC.
Expand Down Expand Up @@ -233,8 +229,6 @@ def concept_retrieval(retriever_func, c) -> Tuple[Set[str], float]:
"F1": f1_sim,
"Runtime Benefits": runtime_y - runtime_neural_y,
"Runtime Neural": runtime_neural_y,
"Symbolic_Retrieval": retrieval_y,
"Symbolic_Retrieval_Neural": retrieval_neural_y,
}
)
# () Update the progress bar.
Expand All @@ -248,12 +242,7 @@ def concept_retrieval(retriever_func, c) -> Tuple[Set[str], float]:
df.to_csv(args.path_report)
del df
# () Load the saved CSV file.
df = pd.read_csv(args.path_report, index_col=0, converters={'Symbolic_Retrieval': lambda x: ast.literal_eval(x),
'Symbolic_Retrieval_Neural': lambda x: ast.literal_eval(
x)})
# () A retrieval result can be parsed into set of instances to python object.
x = df["Symbolic_Retrieval_Neural"].iloc[0]
assert isinstance(x, set)
df = pd.read_csv(args.path_report, index_col=0)
# () Extract the numerical features.
numerical_df = df.select_dtypes(include=["number"])
# () Extract the type of owl concepts
Expand Down

0 comments on commit 537f786

Please sign in to comment.