Skip to content

Commit

Permalink
change format of particleModifier to correctly parse default_fluid/ph…
Browse files Browse the repository at this point in the history
…ysical/visual_conditions
  • Loading branch information
ChengshuLi committed Feb 7, 2024
1 parent 94458e4 commit 36ce577
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
39 changes: 39 additions & 0 deletions bddl/data_generation/get_explicit_transition_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
OBJECT_CAT_AND_INST_RE = r"[A-Za-z-_]+\.n\.[0-9]+"


def parse_conditions_entry(unparsed_conditions):
# print(f"Parsing: {unparsed_conditions}")
if unparsed_conditions.isnumeric():
always_true = bool(int(unparsed_conditions))
conditions = [] if always_true else None
else:
conditions = unparsed_conditions.lower().split(" or ")
return conditions


def sheet_to_json(submap):
params = TM_SUBMAPS_TO_PARAMS[submap]
raw_data = pd.read_csv(os.path.join(SHEETS_DIR, submap + ".csv"))[params].to_json(orient="records")
Expand Down Expand Up @@ -68,10 +78,39 @@ def sheet_to_json(submap):
json.dump(reformatted_data, f, indent=4)


def sheet_to_json_washer():
records = []
with open(os.path.join(SHEETS_DIR, "washer.csv")) as f:
reader = csv.DictReader(f)
for row in reader:
records.append(row)
assert len(records) == 1
record = records[0]

default_fluid_conditions = parse_conditions_entry(record["other liquids"])
default_visual_conditions = parse_conditions_entry(record["other visualSubstances"])
default_physical_conditions = parse_conditions_entry(record["other physicalSubstances"])
remover_kwargs = {
"conditions": {},
"default_fluid_conditions": default_fluid_conditions,
"default_visual_conditions": default_visual_conditions,
"default_physical_conditions": default_physical_conditions,
}

# Iterate through all the columns headed by a substance, in no particular order since their ultimate location is a dict
for dirtiness_substance_synset in [key for key in record if re.match(OBJECT_CAT_AND_INST_RE, key) is not None]:
conditions = parse_conditions_entry(record[dirtiness_substance_synset])
remover_kwargs["conditions"][dirtiness_substance_synset] = conditions

with open(os.path.join(JSONS_DIR, "washer.json"), "w") as f:
json.dump(remover_kwargs, f, indent=4)

def create_save_explicit_transition_rules():
print("Creating explicit transition rule jsons...")
for submap in TM_SUBMAPS_TO_PARAMS:
sheet_to_json(submap)

sheet_to_json_washer()
print("Created and saved explicit transition rule jsons.")


Expand Down
11 changes: 7 additions & 4 deletions bddl/data_generation/process_prop_param_annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def get_synsets_to_particle_remover_params():

# Skip washer and dryer
if "not particleremover" in record["synset"].lower(): continue


default_fluid_conditions = parse_conditions_entry(record["other liquids"])
default_visual_conditions = parse_conditions_entry(record["other visualSubstances"])
default_physical_conditions = parse_conditions_entry(record["other physicalSubstances"])
if record["method"] not in {"projection", "adjacency"}:
Expand All @@ -85,15 +86,15 @@ def get_synsets_to_particle_remover_params():
"conditions": {},
"default_visual_conditions": default_visual_conditions,
"default_physical_conditions": default_physical_conditions,
"default_fluid_conditions": default_fluid_conditions,
"method": record["method"],
}

# Iterate through all the columns headed by a substance, in no particular order since their ultimate location is a dict
for dirtiness_substance_synset in [key for key in record if re.match(OBJECT_CAT_AND_INST_RE, key) is not None]:
conditions = parse_conditions_entry(record[dirtiness_substance_synset])
if conditions is not None:
remover_kwargs["conditions"][dirtiness_substance_synset] = conditions

remover_kwargs["conditions"][dirtiness_substance_synset] = conditions

synset_cleaning_mapping[synset] = remover_kwargs

return synset_cleaning_mapping
Expand Down Expand Up @@ -155,6 +156,8 @@ def create_get_save_propagated_annots_params(syns_to_props):
elif prop == "particleSink":
if param_name == "conditions":
formatted_param_value = {}
elif param_name == "default_fluid_conditions":
formatted_param_value = []
elif param_name == "default_physical_conditions":
formatted_param_value = []
elif param_name == "default_visual_conditions":
Expand Down
19 changes: 16 additions & 3 deletions bddl/data_generation/sanity_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ def sanity_check_object_hierarchy(object_taxonomy):
ability_params = abilities[ability]
for substance_synset in ability_params["conditions"]:
assert substance_synset in leaf_substance_synsets, f"In ParticleModifier annotation, {substance_synset} is not a leaf substance synset."
for condition in ability_params["conditions"][substance_synset]:
if condition[0] == "saturated":
assert condition[1] in leaf_substance_synsets, f"In ParticleModifier annotation, {condition[1]} is not a leaf substance synset."
conditions = ability_params["conditions"][substance_synset]
if conditions is not None:
for condition in conditions:
if condition[0] == "saturated":
assert condition[1] in leaf_substance_synsets, f"In ParticleModifier annotation, {condition[1]} is not a leaf substance synset."

def sanity_check_transition_rules(object_taxonomy):
leaf_synsets = object_taxonomy.get_leaf_descendants("entity.n.01")
Expand All @@ -50,12 +52,23 @@ def sanity_check_transition_rules(object_taxonomy):
for s in val:
assert object_taxonomy.has_ability(s, "fillable"), f"In transition rule file {json_file}, rule {rule}, {val} is not a fillable container."

def sanity_check_transition_rules_washer(object_taxonomy):
leaf_synsets = object_taxonomy.get_leaf_descendants("entity.n.01")
with open(TRANSITION_RULE_FOLDER / "washer.json", "r") as f:
transition_rule = json.load(f)
for system_synset, conditions in transition_rule["conditions"].items():
assert system_synset in leaf_synsets, f"In washer transition rule, {system_synset} is not a leaf synset."
if conditions is not None:
for cleanser_synset in conditions:
assert cleanser_synset in leaf_synsets, f"In washer transition rule, {cleanser_synset} is not a leaf synset."

def sanity_check():
# Lazy import so that it can use the latest version of output_hierarchy_properties.json
from bddl.object_taxonomy import ObjectTaxonomy
object_taxonomy = ObjectTaxonomy()
sanity_check_object_hierarchy(object_taxonomy)
sanity_check_transition_rules(object_taxonomy)
sanity_check_transition_rules_washer(object_taxonomy)

if __name__ == '__main__':
sanity_check()

0 comments on commit 36ce577

Please sign in to comment.