File size: 2,526 Bytes
89c0b51 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# Copyright 2024 ByteDance and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import gzip
import json
import pickle
from pathlib import Path
from typing import Any, Union
import pandas as pd
from protenix.utils.torch_utils import map_values_to_list
PANDAS_NA_VALUES = [
"",
"#N/A",
"#N/A N/A",
"#NA",
"-1.#IND",
"-1.#QNAN",
"-NaN",
"-nan",
"1.#IND",
"1.#QNAN",
"<NA>",
"N/A",
# "NA",
"NULL",
"NaN",
"n/a",
"nan",
"null",
]
def read_indices_csv(csv: Union[str, Path]) -> pd.DataFrame:
"""
Read a csv file without the content changing.
Args:
csv (Union[str, Path]): A csv file path.
Returns:
pd.DataFrame : A pandas DataFrame.
"""
df = pd.read_csv(csv, na_values=PANDAS_NA_VALUES, keep_default_na=False, dtype=str)
return df
def load_gzip_pickle(pkl: Union[str, Path]) -> Any:
"""
Load a gzip pickle file.
Args:
pkl (Union[str, Path]): A gzip pickle file path.
Returns:
Any: The loaded data.
"""
with gzip.open(pkl, "rb") as f:
data = pickle.load(f)
return data
def dump_gzip_pickle(data: Any, pkl: Union[str, Path]):
"""
Dump a gzip pickle file.
Args:
data (Any): The data to be dumped.
pkl (Union[str, Path]): A gzip pickle file path.
"""
with gzip.open(pkl, "wb") as f:
pickle.dump(data, f)
def save_json(data: dict, output_fpath: Union[str, Path], indent: int = 4):
"""
Save a dictionary to a JSON file.
Args:
data (dict): The dictionary to be saved.
output_fpath (Union[str, Path]): The output file path.
indent (int, optional): The indentation level for the JSON file. Defaults to 4.
"""
data_json = data.copy()
data_json = map_values_to_list(data_json)
with open(output_fpath, "w") as f:
if indent is not None:
json.dump(data_json, f, indent=indent)
else:
json.dump(data_json, f)
|