Skip to content

Return indices #21

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

Merged
merged 3 commits into from
Sep 10, 2022
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
15 changes: 15 additions & 0 deletions refinery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ def get_project_details(self) -> Dict[str, str]:
api_response = api_calls.get_request(url, self.session_token)
return api_response

def get_primary_keys(self) -> List[str]:
"""Fetches the primary keys of your current project.

Returns:
List[str]: Containing the primary keys of your project.
"""
project_details = self.get_project_details()
project_attributes = project_details["attributes"]

primary_keys = []
for attribute in project_attributes:
if attribute["is_primary_key"]:
primary_keys.append(attribute["name"])
return primary_keys

def get_lookup_list(self, list_id: str) -> Dict[str, str]:
"""Fetches a lookup list of your current project.

Expand Down
9 changes: 7 additions & 2 deletions refinery/adapter/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def build_classification_dataset(
Dict[str, Dict[str, Any]]: Containing the train and test datasets, with embedded inputs.
"""

df_train, df_test, _ = split_train_test_on_weak_supervision(
df_train, df_test, _, primary_keys = split_train_test_on_weak_supervision(
client, sentence_input, classification_label, num_train
)

Expand All @@ -40,7 +40,12 @@ def build_classification_dataset(
return {
"train": {
"inputs": inputs_train,
"index": df_train[primary_keys].to_dict("records"),
"labels": df_train["label"],
},
"test": {"inputs": inputs_test, "labels": df_test["label"]},
"test": {
"inputs": inputs_test,
"index": df_test[primary_keys].to_dict("records"),
"labels": df_test["label"],
},
}
11 changes: 9 additions & 2 deletions refinery/adapter/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ def build_classification_dataset(
_type_: HuggingFace dataset
"""

df_train, df_test, label_options = split_train_test_on_weak_supervision(
(
df_train,
df_test,
label_options,
primary_keys,
) = split_train_test_on_weak_supervision(
client, sentence_input, classification_label
)

Expand All @@ -44,4 +49,6 @@ def build_classification_dataset(
if os.path.exists(test_file_path):
os.remove(test_file_path)

return dataset, mapping
index = {"train": df_train[primary_keys], "test": df_test[primary_keys]}

return dataset, mapping, index
14 changes: 11 additions & 3 deletions refinery/adapter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@ def split_train_test_on_weak_supervision(
Tuple[pd.DataFrame, pd.DataFrame, List[str]]: Containing the train and test dataframes and the label name options.
"""

primary_keys = client.get_primary_keys()

label_attribute_train = f"{_label}__WEAK_SUPERVISION"
label_attribute_test = f"{_label}__MANUAL"

df_test = client.get_record_export(
tokenize=False,
keep_attributes=[_input, label_attribute_test],
keep_attributes=primary_keys + [_input, label_attribute_test],
dropna=True,
).rename(columns={label_attribute_test: "label"})

if num_train is not None:
num_samples = num_train + len(df_test)
else:
num_samples = None

df_train = client.get_record_export(
tokenize=False,
keep_attributes=[_input, label_attribute_train],
keep_attributes=primary_keys + [_input, label_attribute_train],
dropna=True,
num_samples=num_train + len(df_test),
num_samples=num_samples,
).rename(columns={label_attribute_train: "label"})

# Remove overlapping data
Expand All @@ -47,4 +54,5 @@ def split_train_test_on_weak_supervision(
df_train.reset_index(drop=True),
df_test.reset_index(drop=True),
label_options,
primary_keys,
)