Skip to content

Commit

Permalink
Merge pull request #450 from pepkit/449_to_dict_orient_arg
Browse files Browse the repository at this point in the history
v0.35.6
  • Loading branch information
khoroshevskyi authored Jun 30, 2023
2 parents 8449635 + a6c743e commit f9845b1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
10 changes: 10 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format.

## [0.35.6] -- 2023-06-27
### Added
- `orient` argument to `to_dict` method

### Fixed
- The name of the raw subsample object to match the actual name (list). Commit: #442

### Changed
- Reduced the number of items returned in the to_dict(extended=True) method to 3, with the name and description now stored in the config key.

## [0.35.5] -- 2023-03-27
### Fixed
- A [bug](https://github.com/pepkit/peppy/issues/435) with custom sample ids
Expand Down
2 changes: 1 addition & 1 deletion peppy/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.35.5"
__version__ = "0.35.6"
2 changes: 1 addition & 1 deletion peppy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@

PEP_LATEST_VERSION = "2.1.0"
SAMPLE_RAW_DICT_KEY = "_sample_dict"
SUBSAMPLE_RAW_DICT_KEY = "_subsample_dict"
SUBSAMPLE_RAW_LIST_KEY = "_subsample_list"

__all__ = PROJECT_CONSTANTS + SAMPLE_CONSTANTS + OTHER_CONSTANTS
42 changes: 26 additions & 16 deletions peppy/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections.abc import Mapping
from contextlib import suppress
from logging import getLogger
from typing import Dict, Iterable, List, Tuple, Union
from typing import Dict, Iterable, List, Tuple, Union, Literal

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -57,7 +57,7 @@
SAMPLE_TABLE_INDEX_KEY,
SUBSAMPLE_DF_KEY,
SUBSAMPLE_NAME_ATTR,
SUBSAMPLE_RAW_DICT_KEY,
SUBSAMPLE_RAW_LIST_KEY,
SUBSAMPLE_TABLE_INDEX_KEY,
SUBSAMPLE_TABLES_FILE_KEY,
)
Expand Down Expand Up @@ -208,17 +208,17 @@ def from_dict(self, pep_dictionary: dict) -> "Project":
self[SAMPLE_DF_KEY] = pd.DataFrame(pep_dictionary[SAMPLE_RAW_DICT_KEY])
self[CONFIG_KEY] = pep_dictionary[CONFIG_KEY]

if SUBSAMPLE_RAW_DICT_KEY in pep_dictionary:
if pep_dictionary[SUBSAMPLE_RAW_DICT_KEY]:
if SUBSAMPLE_RAW_LIST_KEY in pep_dictionary:
if pep_dictionary[SUBSAMPLE_RAW_LIST_KEY]:
self[SUBSAMPLE_DF_KEY] = [
pd.DataFrame(sub_a)
for sub_a in pep_dictionary[SUBSAMPLE_RAW_DICT_KEY]
for sub_a in pep_dictionary[SUBSAMPLE_RAW_LIST_KEY]
]
if NAME_KEY in pep_dictionary:
self[NAME_KEY] = pep_dictionary[NAME_KEY]
if NAME_KEY in self[CONFIG_KEY]:
self[NAME_KEY] = self[CONFIG_KEY][NAME_KEY]

if DESC_KEY in pep_dictionary:
self[DESC_KEY] = pep_dictionary[DESC_KEY]
if DESC_KEY in self[CONFIG_KEY]:
self[DESC_KEY] = self[CONFIG_KEY][DESC_KEY]

self.create_samples(modify=False if self[SAMPLE_TABLE_FILE_KEY] else True)
self._sample_table = self._get_table_from_samples(
Expand All @@ -227,25 +227,35 @@ def from_dict(self, pep_dictionary: dict) -> "Project":

return self

def to_dict(self, expand: bool = False, extended: bool = False) -> dict:
def to_dict(
self,
expand: bool = False,
extended: bool = False,
orient: Literal[
"dict", "list", "series", "split", "tight", "records", "index"
] = "dict",
) -> dict:
"""
Convert the Project object to a dictionary.
:param bool expand: whether to expand the paths
:param bool extended: whether to produce complete project dict (used to reinit the project)
:param Literal orient: orientation of the returned df
:return dict: a dictionary representation of the Project object
"""
if extended:
if self[SUBSAMPLE_DF_KEY] is not None:
sub_df = [sub_a.to_dict() for sub_a in self[SUBSAMPLE_DF_KEY]]
sub_df = [
sub_a.to_dict(orient=orient) for sub_a in self[SUBSAMPLE_DF_KEY]
]
else:
sub_df = None
self[CONFIG_KEY][NAME_KEY] = self[NAME_KEY]
self[CONFIG_KEY][DESC_KEY] = self[DESC_KEY]
p_dict = {
SAMPLE_RAW_DICT_KEY: self[SAMPLE_DF_KEY].to_dict(),
CONFIG_KEY: self[CONFIG_KEY],
SUBSAMPLE_RAW_DICT_KEY: sub_df,
NAME_KEY: self[NAME_KEY],
DESC_KEY: self[DESC_KEY],
SAMPLE_RAW_DICT_KEY: self[SAMPLE_DF_KEY].to_dict(orient=orient),
CONFIG_KEY: self[CONFIG_KEY].to_dict(expand=expand),
SUBSAMPLE_RAW_LIST_KEY: sub_df,
}
else:
p_dict = self.config.to_dict(expand=expand)
Expand Down
9 changes: 6 additions & 3 deletions tests/test_Project.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,16 @@ def test_unequality(self, example_pep_cfg_path, example_pep_csv_path):
p2 = Project(cfg=example_pep_csv_path)
assert not p1 == p2

@pytest.mark.parametrize("example_pep_cfg_path", ["append"], indirect=True)
def test_from_dict(self, example_pep_cfg_path):
@pytest.mark.parametrize(
"example_pep_cfg_path", ["append", "subtable2"], indirect=True
)
@pytest.mark.parametrize("orient", ["dict", "records"])
def test_from_dict(self, example_pep_cfg_path, orient):
"""
Test initializing project from dict
"""
p1 = Project(cfg=example_pep_cfg_path)
p1_dict = p1.to_dict(extended=True)
p1_dict = p1.to_dict(extended=True, orient=orient)
del p1_dict["_config"]["sample_table"]
p2 = Project().from_dict(p1_dict)
assert p1 == p2
Expand Down

0 comments on commit f9845b1

Please sign in to comment.