Skip to content

Add text sampling - #304

Merged
MicPie merged 108 commits into
mainfrom
text_sampling
Aug 17, 2023
Merged

MicPie merged 108 commits into
mainfrom
text_sampling

Conversation

@MicPie

@MicPie MicPie commented Jun 15, 2023 •

Copy link
Copy Markdown
Contributor

Setup to sample text templates with the data.

Done:

  • An examples of the template structure in the yaml file, see data/ames_mutagenicity/meta.yaml.
  • Basic code that runs the sampling for the standard templates and the benchmarking templates, see text_sampling/text_sampling.py.
  • Add knowledge graph (KG) sampling. The KG data is run through text_sampling/preprocess_kg.py and the resulting data can be consumed by text_sampling/text_sampling.py w/o further modifications.
  • Restructure data dir, e.g., data/tabular and data/kg.
  • Update docs/CONTRIBUTING.md for the new template setup
  • Type annotations and docs
  • LM eval harness export
  • Add canonical, inchi, tucan, and iupac_name addition and sampling
  • Add dependencies from extend_tabular.py to the dev env.
  • Adapt prompt template according to "Can Large Language Models Transform Computational Social Science?", Table 1 for the blood_brain_barrier_martins_et_al dataset that serves as a template.
  • add rounding options to meta.yaml and templating insertion.
  • fix issues from the dataset meta files (missing nouns, etc.)

To do (can be moved to smaller PRs too):

  • SMILES augmentation (check example with @kjappelbaum)
  • chem bench export
  • xyz data support
  • move exclude_from_standard_tabular_text_templates (in data/text_sampling/text_sampling.py) information to corresponding meta.yaml files.
  • Optional: TUCAN library needs python 3.10 or change to typing annotations in order to be used

Description of the new text template setup for the sampling

With the current setup you can:

  • use all the data from the meta.yaml file, and
  • recode categorical data.

Example text template 1 (mainly used for tabular data)

The molecule with the {SMILES__description} representation of {SMILES#} exhibits {mutagenic#no &NULL}{mutagenic__names__adjective} properties.

  • SMILES__description gets you the text from the description field of the SMILES identifier. The __ dunder (double underscore) is used to indicate the levels in the meta.yaml file.
  • SMILES# gets you the data of the sampled SMILES row. The # is used to get the corresponding data.
  • mutagenic#no &NULL gets you the data with # and recodes it. The recoding options are separated with a &. In this example the binary variable mutagenic that can be 0 or 1 gets recoded to no and NULL. NULL is a "reserved word" an indicates no value. Thus, the no gets added in front of the mutagenic__names__adjective if mutagenic# == 0.
  • mutagenic__names__adjective gets you from the id mutagenic the adjective names.

Example text template 2 (mainly used for KG data)

The {node1_type#} {node1_name#|node1_smiles#} {rel1_type#} the {node2_type#} {node2_protein_names#} which {rel2_type#} the {node3_type#} {node3_name#}.

  • node1_name#|node1_smiles# chains together two columns with | so they are jointly sampled for this position. In this case we sample from the name or the SMILES representation.
  • A similar setup can be used in a single column: For node2_protein_names the column row can include several protein names also separated by a |, e.g., Pyruvate dehydrogenase E1 component subunit beta, mitochondrial|PDHE1-B which then samples from Pyruvate dehydrogenase E1 component subunit beta, mitochondrial or PDHE1-B.

Example text templates 3 for multiple choice setups

Multiple choice setups are also supported. For this we need three components:

  • %multiple_choice_enum%2%aA1 can be used to list the multiple choice enumerations, i.e., 1, 2, or 3, A or B, etc., The second % starts the multiple choice number sequence. Single integers and a range consisting of two integers separated by a - are supported to set the lower and higher number, e.g., 2-5 will sample a value between those values including the boundaries for the answer options. The third % is used to subselect multiple choice enumerations, i.e., a for lower case alphabetical enumerations, A for upper case alphabetical, and 1 for numerical enumerations.
  • mutagenic% is used to list the multiple choice enumerations with the corresponding possible answer options after the multiple choice enumerations, and
  • %multiple_choice_result is used to get the multiple choice enumeration of the answer, i.e., 1, c.
    Please pay attention to the % symbol and its position as this is used to parse the different control elements from the text template.
    The sampling procedure incorporates a range of different multiple choice enumerations that are sampled randomly:
  • numerical (1, 2, 3, ...) and alphabetical (a, b, c, ... or A, B, C, ...) enumerations combined with
  • different suffixes, i.e., (no suffix), ., .), ), and :, to create a range of different multiple choice enumerations.
    If only the choices 0 or 1 are available they will be recoded with False and True.

Standard template

Task: Please answer the multiple choice question below with {%multiple_choice_enum%2%aA1}.
Question: Is the molecule with the {SMILES__description} representation of {SMILES#} {mutagenic__names__adjective}?
Options:
{mutagenic%}
Answer: {%multiple_choice_result}

Example output:

Task: Please answer the multiple choice question below with A or B.
Question: Is the molecule with the SMILES representation of CC(C)NCC(O)c1ccc2ccccc2c1 Ames mutagenic?
Options:
A) False
B) True
Answer: A

Template for benchmarking

Task: Please answer the multiple choice question below with {%multiple_choice_enum%2%aA1}.
Question: Is the molecule with the {SMILES__description} representation of {SMILES#} {mutagenic__names__adjective}?
Options:
{mutagenic%}
Answer:<EOI> {%multiple_choice_result}

The benchmarking setup exports additional fields for the benchmarking setup, see the example below:
{"input":"Task: Please answer the multiple choice question below with 1 or 2.\nQuestion: Is the molecule with the SMILES representation of BrCBr Ames mutagenic?\nOptions:\n1.) False\n2.) True\nAnswer:","output":" 2","output_choices":["1","2"],"correct_output_index":"1"}
Please have a look at the following section below about the general benchmarking template setup.

Example text templates 4 for flexible multiple choice setups

More flexible multiple choice setups are also supported. The standard multiple choice setup from "Example text templates 3 for multiple choice setups" is intended for features of molecules as those are deduplicated during the sampling process. In contrast, this flexible multiple choice setup also lets you use the molecule identifiers, e.g., SMILES, in the multiple choice options.

For this we only need to add one component to the previously outlined multiple choice format:

  • In order to let the model predict which SMILES has or has not the boolean variable penetrate_BBB we simply add SMILES%penetrate_BBB% as an enumeration placeholder for the possible options. With that the list of the multiple choice enumerations shows the SMILES data. Note that the penetrate_BBB#not &NULL is needed because the sampling is based on the individual row and depending on if penetrate_BBB is True or False we look for a different label because in the code we compare the sampled options to the penetrate_BBB value of the row.
Task: Please answer the multiple choice question.
Question: Which molecules are {penetrate_BBB#not &NULL}{penetrate_BBB__names__adjective}?
Constraint: You must select none, one or more options from {%multiple_choice_enum%2-5%aA1} without using any other words.
Options:
{SMILES%penetrate_BBB%}
Answer: {%multiple_choice_result}
Task: Please answer the multiple choice question.
Question: Which molecules are not penetrating the blood brain barrier?
Constraint: You must select none, one or more options from A, B, or C without using any other words.
Options:
A. Cc1ccsc1C(=CCCN1CCC[C@@H](C(=O)O)C1)c1sccc1C
B. CC(=O)N1CCN(c2ccc(OC[C@H]3CO[C@](Cn4ccnc4)(c4ccc(Cl)cc4Cl)O3)cc2)CC1
C. CCCC(C)C1(CC)C(=O)NC(=S)NC1=O
Answer: B, C

Benchmarking text templates

There are two versions of text templates, i.e., one without the end-of-input token <EOI> and those with:

The {SMILES__description} {SMILES#} is {mutagenic#no &NULL}{mutagenic__names__adjective}.
Is the {SMILES__description} {SMILES#} {mutagenic__names__adjective}:<EOI>{mutagenic# yes& no}

The <EOI> token indicates the splitting position for the benchmarking export, i.e., everything before it will be written to the input field and everything afterwards to the output field. Without <EOI> everything will be in the text field.
In the current setup, you can switch with the benchmarking_templates flag of the TemplateSampler class between text templates with and without <EOI>.

The filename scheme uses the split information for the export, i.e., train.jsonl, test.jsonl, etc., and if no split information is available this column will be set to full and exported to full.jsonl. With <EOI> the filename ends with _benchmark.jsonl instead of .jsonl.

Have a look at the meta.yaml file to see the corresponding structure there.

@jackapbutler

Copy link
Copy Markdown
Collaborator

this looks great!

@bethanyconnolly

Copy link
Copy Markdown
Collaborator

Yes this looks awesome!
I think we will have to upload the jsons from this to hf and add processing scripts for making huggingface datasets (see our periodic table example where I have uploaded a pd.dataframe with 3 columns: question, answer choices and index of correct answer) because this is where lm-eval-harness extracts the tasks data from. But if we have all the data in the same format then this should be easy. Also I guess all of these tabular tasks can be converted into multi-choice questions using the lm-eval-harness multichoice task template which again will be simple. The only question is what the multiple choice answers for each multiple choice task will be over. In this case it is easy (there are only 2 possible answers for all examples, yes or no) but we might have templates with more complicated choices of answer so we can think about this as it comes up. One thing which might help a little is if we use a similar template to the PeriodicTable task because this is easy to load into the multichoice task template. So instead of saving just["input", "output"] for question answer, we could have ["input", "output choices", "correct output index"] e.g. one row would be ["Is the {SMILES__description} {SMILES#} {mutagenic__names__adjective}?", [yes, no], 0]. Let me know what you think!

Comment thread text_sampling/text_sampling.py Resolved
Comment thread text_sampling/text_sampling.py Resolved
Comment thread data/text_sampling/extend_tabular.py Resolved
Comment thread data/tabular/flashpoint/meta.yaml Resolved
Comment on lines +215 to +217
def str_presenter(dumper, data):
"""configures yaml for dumping multiline strings
Ref: https://stackoverflow.com/questions/8640959/how-can-i-control-what-scalar-form-pyyaml-uses-for-my-data

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we import this from utils?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure we have that in a few tabular data setup. I'll check that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know a good setup to handle that?
I didn't find any nice solution except adding the additional import path or moving it to the chemnlp library.

Comment thread data/text_sampling/text_sampling.py Resolved
Comment on lines +33 to +40
exclude_from_standard_tabular_text_templates = [
"ames_mutagenicity", # because it is boolean target data
"bioavailability_ma_et_al", # because it is boolean target data
"blood_brain_barrier_martins_et_al", # because it is boolean target data
"chembl_v29", # text only, no SMILES
"moses", # SMILES only, has no target
"zinc", # SMILES only, has no target
]

@kjappelbaum kjappelbaum Aug 14, 2023 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we put this info into the meta.yaml files and not maintain it in two places? i.e. a meta.yaml file might tell itself if it is "ready" for the standard setup

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to the to dos above!

Comment thread data/text_sampling/text_sampling.py Resolved
MicPie and others added 2 commits August 17, 2023 14:29

@kjappelbaum kjappelbaum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 💯

@MicPie
MicPie merged commit 12ca346 into main Aug 17, 2023
@MicPie
MicPie deleted the text_sampling branch August 17, 2023 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants