| import numpy as np |
| import os |
| import pandas as pd |
| import os |
| import joblib |
| import torch |
|
|
|
|
| def load_data(data_path: str) -> pd.DataFrame: |
| """ |
| Loads data from a specified file path into a pandas DataFrame, |
| handling various file extensions. |
| |
| Args: |
| data_path (str): The full path to the data file. |
| |
| Returns: |
| pd.DataFrame: A pandas DataFrame containing the loaded data. |
| |
| Raises: |
| FileNotFoundError: If the file does not exist at the specified path. |
| ValueError: If the file extension is unsupported. |
| Exception: For other potential pandas reading errors. |
| """ |
| |
| if not os.path.exists(data_path): |
| raise FileNotFoundError(f"Error: The file was not found at '{data_path}'") |
|
|
|
|
| |
| _, file_extension = os.path.splitext(data_path) |
| file_extension = file_extension.lower() |
|
|
| |
| print(f"Attempting to load file with extension: '{file_extension}'...{data_path}") |
|
|
| try: |
| if file_extension == '.csv': |
| return pd.read_csv(data_path) |
| |
| elif file_extension == '.txt': |
| return pd.read_csv(data_path, delimiter=r'\s+') |
|
|
| elif file_extension in ['.xls', '.xlsx']: |
| |
| return pd.read_excel(data_path) |
|
|
| elif file_extension == '.json': |
| |
| return pd.read_json(data_path) |
| |
| elif file_extension == '.parquet': |
| |
| return pd.read_parquet(data_path) |
| |
| elif file_extension == '.joblib': |
| |
| return joblib.load(data_path) |
| |
| elif file_extension == '.tab': |
| return pd.read_csv(data_path, sep='\t') |
| |
| elif file_extension == '.pt': |
| return torch.load(data_path) |
| |
| elif file_extension == '.npz': |
| return np.load(data_path, allow_pickle=True) |
| |
| else: |
| |
| raise ValueError(f"Unsupported file extension: '{file_extension}'. " |
| "Please use one of: .csv, .txt, .xlsx, .xls, .json, .parquet") |
| |
| except Exception as e: |
| print(f"An error occurred while reading the file: {e}") |
| |
| raise |
|
|
|
|
|
|
|
|
| def load_rdkit(filename): |
| """ |
| Returns loaded rdkit data |
| |
| Args: |
| filename (str): filename and path to load OR |
| chunk_id (int or None): Optional chunk ID to load specific file slice (e.g. data_chunk_0.csv) |
| num_chunks: number of chunks |
| |
| NOTE: |
| rdkit data saved as array of one sample of a dictionary of 200 features |
| with feature1: feature1 value, feature2:feature2value, for every sample. |
| |
| |
| Returns: |
| array of rdkit descriptors |
| """ |
|
|
| mydata = filename |
|
|
| |
| rdkit_dict = load_data(mydata)['arr'] |
|
|
| |
| rdkit_df = pd.DataFrame(list(rdkit_dict)) |
|
|
| |
| feature_matrix = rdkit_df.to_numpy() |
| feature_names = rdkit_df.columns.to_numpy() |
|
|
| |
| if np.isnan(feature_matrix).any(): |
| feature_matrix = np.nan_to_num(feature_matrix, nan=0.0) |
| print('NANS found in rdkit, setting them to 0') |
|
|
| |
| feature_matrix = np.sign(feature_matrix) * np.log1p(np.abs(feature_matrix)) |
|
|
| |
| feature_matrix = np.clip(feature_matrix, -10.0, 10.0) |
|
|
|
|
| return feature_matrix |
|
|