Repository: pepkit/peppy

Issue:
from_dict method does not set correctly sample and subsample indexes
Related issue: https://github.com/pepkit/pephub/issues/207

Discussion:

Patch:
diff --git a/docs/changelog.md b/docs/changelog.md
index 3ecc382..1351854 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -2,6 +2,11 @@
 
 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.7] -- 2023-07-19
+### Fixed 
+- incorrect setting of sample and subsample indexes using from_dict function (#452)
+- clarified debug messages
+
 ## [0.35.6] -- 2023-06-27
 ### Added
 - `orient` argument to `to_dict` method
diff --git a/peppy/_version.py b/peppy/_version.py
index a51f1e9..c83377e 100644
--- a/peppy/_version.py
+++ b/peppy/_version.py
@@ -1,1 +1,1 @@
-__version__ = "0.35.6"
+__version__ = "0.35.7"
diff --git a/peppy/project.py b/peppy/project.py
index 9e21364..665d4b2 100644
--- a/peppy/project.py
+++ b/peppy/project.py
@@ -6,7 +6,7 @@ import os, sys
 from collections.abc import Mapping
 from contextlib import suppress
 from logging import getLogger
-from typing import Dict, Iterable, List, Tuple, Union, Literal
+from typing import Dict, Iterable, List, Tuple, Union, Literal, NoReturn
 
 import numpy as np
 import pandas as pd
@@ -220,6 +220,8 @@ class Project(PathExAttMap):
         if DESC_KEY in self[CONFIG_KEY]:
             self[DESC_KEY] = self[CONFIG_KEY][DESC_KEY]
 
+        self._set_indexes(self[CONFIG_KEY])
+
         self.create_samples(modify=False if self[SAMPLE_TABLE_FILE_KEY] else True)
         self._sample_table = self._get_table_from_samples(
             index=self.st_index, initial=True
@@ -268,6 +270,9 @@ class Project(PathExAttMap):
         Populate Project with Sample objects
         """
         self._samples: List[Sample] = self.load_samples()
+        if self.samples is None:
+            _LOGGER.info("No samples found in the project.")
+
         if modify:
             self.modify_samples()
         else:
@@ -336,14 +341,7 @@ class Project(PathExAttMap):
 
         _LOGGER.debug("Raw ({}) config data: {}".format(cfg_path, config))
 
-        self.st_index = (
-            config[SAMPLE_TABLE_INDEX_KEY] if SAMPLE_TABLE_INDEX_KEY in config else None
-        )
-        self.sst_index = (
-            config[SUBSAMPLE_TABLE_INDEX_KEY]
-            if SUBSAMPLE_TABLE_INDEX_KEY in config
-            else None
-        )
+        self._set_indexes(config)
         # recursively import configs
         if (
             PROJ_MODS_KEY in config
@@ -398,6 +396,23 @@ class Project(PathExAttMap):
         relative_vars = [CFG_SAMPLE_TABLE_KEY, CFG_SUBSAMPLE_TABLE_KEY]
         _make_sections_absolute(self[CONFIG_KEY], relative_vars, cfg_path)
 
+    def _set_indexes(self, config: Mapping) -> NoReturn:
+        """
+        Set sample and subsample indexes if they are different then Default
+
+        :param config: project config
+        """
+        self.st_index = (
+            config[SAMPLE_TABLE_INDEX_KEY]
+            if SAMPLE_TABLE_INDEX_KEY in config
+            else SAMPLE_NAME_ATTR
+        )
+        self.sst_index = (
+            config[SUBSAMPLE_TABLE_INDEX_KEY]
+            if SUBSAMPLE_TABLE_INDEX_KEY in config
+            else SUBSAMPLE_NAME_ATTR
+        )
+
     def load_samples(self):
         """
         Read the sample_table and subsample_tables into dataframes


Test patch:
diff --git a/tests/test_Project.py b/tests/test_Project.py
index 85a1c48..751c4ef 100644
--- a/tests/test_Project.py
+++ b/tests/test_Project.py
@@ -554,7 +554,9 @@ class TestPostInitSampleCreation:
         assert not p1 == p2
 
     @pytest.mark.parametrize(
-        "example_pep_cfg_path", ["append", "subtable2"], indirect=True
+        "example_pep_cfg_path",
+        ["append", "custom_index", "imply", "subtables"],
+        indirect=True,
     )
     @pytest.mark.parametrize("orient", ["dict", "records"])
     def test_from_dict(self, example_pep_cfg_path, orient):