Skip to content
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

Remove pandas and update dependencies #20

Merged
merged 2 commits into from
Mar 8, 2024
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
29 changes: 19 additions & 10 deletions allms/utils/io_utils.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
import csv
import logging
from pathlib import Path
from typing import List, Optional, Union
from typing import Any, Dict, List, Optional, Union, OrderedDict

import fsspec
import pandas as pd

from allms.constants.input_data import IODataConstants
from allms.domain.input_data import InputData

logger = logging.getLogger(__name__)


def load_data(
def load_csv(
path: str,
limit: Optional[int] = None
) -> List[InputData]:
) -> List[OrderedDict[Any, Any]]:
logger.info(f"Loading test data from {path}")
input_df = pd.read_csv(path)
input_df = input_df.head(limit) if limit else input_df
return load_input_data(input_df)
with open(path, mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
data = list(csv_reader)
return data[:limit] if limit else data


def load_input_data(input_df: pd.DataFrame) -> List[InputData]:
def load_csv_to_input_data(path: str, limit: Optional[int] = None) -> List[InputData]:
csv_data = load_csv(path, limit=limit)
return list(
map(
lambda row: InputData(input_mappings=row[1].drop(IODataConstants.ID).to_dict(), id=str(row[1].id)),
input_df.iterrows()
lambda row: InputData(input_mappings=drop_dict_key(row, IODataConstants.ID),
id=str(row[IODataConstants.ID])),
csv_data
)
)


def drop_dict_key(dictionary: Dict[Any, Any], key: Any) -> Dict[Any, Any]:
dict_copy = dictionary.copy()
dict_copy.pop(key)
return dict_copy


def load_credentials(path: Union[str, Path]) -> str:
with fsspec.open(path, "r") as credentials_file:
return credentials_file.readline()
Loading
Loading