Download preprocessing.py from chowdhury-lab/constrastiveML: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/datasets/chowdhury-lab/constrastiveML/resolve/main/preprocessing.py
- Command line
-
hf download hf://datasets/chowdhury-lab/constrastiveML/preprocessing.py
-
curl -L -o preprocessing.py https://huggingface.co/datasets/chowdhury-lab/constrastiveML/resolve/main/preprocessing.py
2.64 kB
| import pandas as pd | |
| import torch | |
| import csv | |
| import numpy as np | |
| from rdkit import Chem | |
| from torch_geometric.data import Data | |
| from torch_geometric.nn import GCNConv | |
| import torch.nn.functional as F | |
| import pickle | |
| from transformers import AutoTokenizer, AutoModel | |
| from utils import * | |
| def load_csv_data(filename): | |
| mylist = [] | |
| with open(filename) as data: | |
| csv_data = csv.reader(data, delimiter=',') | |
| next(csv_data) # Skip header row | |
| for row in csv_data: | |
| if any(row): # Check if any element in the row is non-empty | |
| mylist.append(row) | |
| return mylist | |
| new_list = load_csv_data('Raw_data/Bovine_cleaned_data_with_permeability.csv') | |
| print(f'Total number of sample in .csv file: {len(new_list)}') | |
| # Directory for output | |
| embeddings_output_folder = "Embedded_data_ChemBERTa_with_permeability/Bovine_data" | |
| check_dir(embeddings_output_folder) | |
| # Initialize progress tracker file | |
| progress_tracker_file = f"{embeddings_output_folder}/run_progress_tracker" | |
| data_list = [] | |
| # Restart from a specified line number | |
| restart_line = 1 # Change this to the line number from which to restart | |
| # Load previously saved data if restarting | |
| embedded_file_path = f"{embeddings_output_folder}/Embedded_file_upto_{restart_line-1}_restart.pkl" | |
| if restart_line > 1 and os.path.exists(embedded_file_path): | |
| with open(embedded_file_path, 'rb') as file: | |
| data_list = pickle.load(file) | |
| print(f"Resuming from line {restart_line}") | |
| with open(progress_tracker_file, 'w') as file: | |
| file.write(f"Starting my work from line {restart_line}\n") | |
| for index, item in enumerate(new_list, start=1): # input list is changed from new_list to unique_row | |
| if index < restart_line: | |
| continue # Skip lines before the restart line | |
| with open(progress_tracker_file, 'a') as file: | |
| file.write(f"Working on line {index}/{len(new_list)}\n") | |
| if 0 < len(item[1]): | |
| smiles = [item[1]] | |
| chemBERTa_embedding = smiles_ChemBERTa_embedding(smiles) | |
| data_list.append([item[0], item[1], item[3], item[4], chemBERTa_embedding, item[5]]) | |
| if index % 500 == 0: | |
| with open(f"{embeddings_output_folder}/Embedded_file_upto_{index}_restart.pkl", 'wb') as file: | |
| pickle.dump(data_list, file) | |
| print(f"The line {index}/{len(new_list)} is completed") | |
| with open(progress_tracker_file, 'a') as file: | |
| file.write(f"The line {index}/{len(new_list)} is completed.\n") | |
| with open(f"{embeddings_output_folder}/Embedded_file_complete_ChemBERTa.pkl", 'wb') as file: | |
| pickle.dump(data_list, file) | |
| print("Process completed without an error") | |