Skip to content

Commit

Permalink
Merge pull request #29 from YerevaNN/minor_changes_for_run
Browse files Browse the repository at this point in the history
add try except for whole text formatting
  • Loading branch information
MenuaB authored May 19, 2024
2 parents 595e029 + b6ed503 commit 79bbd5d
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions chemlactica/utils/text_format_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,38 +105,41 @@ def generate_formatted_string(compound_json, rng, model_config):


def format_key_value(key, value, rng):
if key == "CID":
try:
if key == "CID":
return ""
formatted_string = ""
if key == "related":
if len(value) > 10:
# value = random.sample(value, 5)
value = rng.choice(value, size=10, replace=False, shuffle=False)
for pair in value:
rounded_sim = "{:.2f}".format(float(pair["similarity"]))
formatted_string += f"{SPECIAL_TAGS['similarity']['start']}{pair['SMILES']} {rounded_sim}{SPECIAL_TAGS['similarity']['end']}" # noqa
elif key == "experimental":
for pair in value:
formatted_string += f"[PROPERTY]{pair['PROPERTY_NAME']} {pair['PROPERTY_VALUE']}[/PROPERTY]" # noqa
elif key == "synonyms":
for val in value:
formatted_string += f"{SPECIAL_TAGS['synonym']['start']}{val['name']}{SPECIAL_TAGS['synonym']['end']}" # noqa
else:
try:
if SPECIAL_TAGS[key].get("type") is float:
value = "{:.2f}".format(float(value))
assert len(value.split(".")[-1]) == 2
start = SPECIAL_TAGS[key]["start"]
end = SPECIAL_TAGS[key]["end"]
except Exception as e:
print(e)
print("Failed to parse: ", key, value)
start = value = end = ""
return f"{start}{value}{end}"

return formatted_string
except Exception as e:
print(e)
print("Failed to parse: ", key, value)
return ""
formatted_string = ""
if key == "related":
if len(value) > 10:
# value = random.sample(value, 5)
value = rng.choice(value, size=10, replace=False, shuffle=False)
for pair in value:
rounded_sim = "{:.2f}".format(float(pair["similarity"]))
formatted_string += f"{SPECIAL_TAGS['similarity']['start']}{pair['SMILES']} {rounded_sim}{SPECIAL_TAGS['similarity']['end']}" # noqa
elif key == "experimental":
for pair in value:
formatted_string += (
f"[PROPERTY]{pair['PROPERTY_NAME']} {pair['PROPERTY_VALUE']}[/PROPERTY]"
)
elif key == "synonyms":
for val in value:
formatted_string += f"{SPECIAL_TAGS['synonym']['start']}{val['name']}{SPECIAL_TAGS['synonym']['end']}" # noqa
else:
try:
if SPECIAL_TAGS[key].get("type") is float:
value = "{:.2f}".format(float(value))
assert len(value.split(".")[-1]) == 2
start = SPECIAL_TAGS[key]["start"]
end = SPECIAL_TAGS[key]["end"]
except Exception as e:
print(e)
print("Failed to parse: ", key, value)
start = value = end = ""
return f"{start}{value}{end}"

return formatted_string


def main():
Expand Down

0 comments on commit 79bbd5d

Please sign in to comment.