【问题标题】:Filtering two py2store stores with the same set of keys使用相同的键集过滤两个 py2store 存储
【发布时间】:2020-08-28 18:54:27
【问题描述】:

在下面的代码中,基于我使用py2store 找到的示例,我使用with_key_filt 制作了两个 dacc(一个带有训练数据,另一个带有测试数据)。我确实得到了一个过滤的annots 商店,但wfs 商店没有被过滤。 我做错了什么?

from py2store import cached_keys

class Dacc:
    """Waveform and annotation data access"""
    def __init__(self, wfs, annots, annot_to_tag=lambda x: x['tag']):
        self.wfs = wfs  # waveform store  (keys: filepaths, values: numpy arrays)
        self.annots = annots  # annotation store (keys: filepaths, values: dicts or pandas series)
        self.annot_to_tag = annot_to_tag  # function to compute a tag from an annotation item

    @classmethod
    def with_key_filt(cls, key_filt, wfs, annots, annot_to_tag, chunker):
        """
        Make an instance of the dacc class where the data is filtered out.
        You could also filter out externaly, but this can be convenient
        """
        filtered_annots = cached_keys(annots, keys_cache=key_filt)
        return cls(wfs, filtered_annots, annot_to_tag)

    def wf_tag_gen(self):
        """Generator of (wf, tag) tuples"""
        for k in self.annots:
            try:
                wf = self.wfs[k]
                annot = self.annots[k]
                yield wf, self.annot_to_tag(annot)
            except KeyError:
                pass

【问题讨论】:

    标签: python key store filtered


    【解决方案1】:

    似乎with_key_filt 的意图似乎是过滤annots,它本身被用作wg_tag_gen 生成器的种子(可能还有您未发布的其他生成器)。因此,它确实过滤了所有内容。

    但我同意您的期望,即 wfs 也应该被过滤。为此,您只需添加一行来过滤wfs

    class TheDaccYouWant(Dacc):
        @classmethod
        def with_key_filt(cls, key_filt, wfs, annots, annot_to_tag, chunker):
            filtered_annots = cached_keys(annots, keys_cache=key_filt)
            wfs = cached_keys(wfs, keys_cache=key_filt)  # here's what was added
            return cls(wfs, filtered_annots, annot_to_tag, chunker)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 2019-02-27
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多