Datasets:
Delete prep_labels.py
Browse files- prep_labels.py +0 -71
prep_labels.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import argparse
|
| 3 |
-
|
| 4 |
-
def process_to_multiclass(file_path):
|
| 5 |
-
# Load the TSV file into a pandas DataFrame, treating "NA" as a valid string
|
| 6 |
-
df = pd.read_csv(file_path, sep='\t', keep_default_na=False)
|
| 7 |
-
|
| 8 |
-
# Rename the 'label' column to 'register'
|
| 9 |
-
if 'label' in df.columns:
|
| 10 |
-
df.rename(columns={'label': 'register'}, inplace=True)
|
| 11 |
-
|
| 12 |
-
# Define the allowed register labels
|
| 13 |
-
allowed_labels = {"IN", "OP", "NA", "IP", "ID", "HI", "LY", "SP", "OTHER"}
|
| 14 |
-
|
| 15 |
-
# Create the new 'label' column by filtering the allowed labels from the 'register' column
|
| 16 |
-
def extract_labels(register_value, index):
|
| 17 |
-
if pd.isna(register_value) or register_value == "":
|
| 18 |
-
print(f"Problematic row {index}: Missing 'register' value")
|
| 19 |
-
return "" # Return an empty string or handle missing data as desired
|
| 20 |
-
try:
|
| 21 |
-
return ' '.join([label for label in register_value.split() if label in allowed_labels])
|
| 22 |
-
except Exception as e:
|
| 23 |
-
print(f"Problematic row {index}: {register_value} caused error: {e}")
|
| 24 |
-
return "" # Handle invalid data gracefully
|
| 25 |
-
|
| 26 |
-
# Apply the extraction function with index tracking for debugging
|
| 27 |
-
df['label'] = df.apply(lambda row: extract_labels(row['register'], row.name), axis=1)
|
| 28 |
-
|
| 29 |
-
# Save the DataFrame back to a TSV file (overwrites the original)
|
| 30 |
-
output_file = 'output_' + file_path
|
| 31 |
-
df.to_csv(output_file, sep='\t', index=False)
|
| 32 |
-
print(f"File processed successfully. Output saved to {output_file}")
|
| 33 |
-
|
| 34 |
-
def process_to_multilabel(file_path):
|
| 35 |
-
# Load the TSV file into a pandas DataFrame, treating "NA" as a valid string
|
| 36 |
-
df = pd.read_csv(file_path, sep='\t', keep_default_na=False)
|
| 37 |
-
|
| 38 |
-
# Rename the 'label' column to 'register'
|
| 39 |
-
if 'label' in df.columns:
|
| 40 |
-
df.rename(columns={'label': 'register'}, inplace=True)
|
| 41 |
-
|
| 42 |
-
# Create the new 'label' column by ws splitting the 'register' column and saving a list
|
| 43 |
-
def extract_labels(register_value, index):
|
| 44 |
-
try:
|
| 45 |
-
return register_value.split()
|
| 46 |
-
except Exception as e:
|
| 47 |
-
print(f"Problematic row {index}: {register_value} caused error: {e}")
|
| 48 |
-
return ""
|
| 49 |
-
|
| 50 |
-
# Apply the extraction function with index tracking for debugging
|
| 51 |
-
df['full_label'] = df.apply(lambda row: extract_labels(row['register'], row.name), axis=1)
|
| 52 |
-
|
| 53 |
-
# only keep major labels for the 'label' column
|
| 54 |
-
allowed_labels = {"IN", "OP", "NA", "IP", "ID", "HI", "LY", "SP", "OTHER"}
|
| 55 |
-
df['label'] = df['full_label'].apply(lambda x: [label for label in x if label in allowed_labels])
|
| 56 |
-
|
| 57 |
-
# Save the DataFrame back to a TSV file
|
| 58 |
-
output_file = 'output_' + file_path
|
| 59 |
-
df.to_csv(output_file, sep='\t', index=False)
|
| 60 |
-
print(f"File processed successfully. Output saved to {output_file}")
|
| 61 |
-
|
| 62 |
-
if __name__ == "__main__":
|
| 63 |
-
# Set up argument parser to take file name from the command line
|
| 64 |
-
parser = argparse.ArgumentParser(description="Process a TSV file by renaming the label column and filtering register labels.")
|
| 65 |
-
parser.add_argument('file_path', type=str, help="The path to the input TSV file")
|
| 66 |
-
|
| 67 |
-
# Parse the arguments
|
| 68 |
-
args = parser.parse_args()
|
| 69 |
-
|
| 70 |
-
# Call the function with the provided file path
|
| 71 |
-
process_to_multilabel(args.file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|