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

parent/child grouping #134

Closed
swharden opened this issue Jun 29, 2023 · 0 comments
Closed

parent/child grouping #134

swharden opened this issue Jun 29, 2023 · 0 comments

Comments

@swharden
Copy link
Owner

It's often convenient to have a simple way to group related ABFs (e.g., all recordings from a single cell) within a folder that contains a long series of consecutively-named ABF files.

I often achieve this by taking a picture of a cell and saving it with the same basename as the first ABF for that cell. This "parent" abf indicates the start of a sequence of related ABF files, and the subsequent ABF files are "children".

This may be useful to consider including in the pyabf tools submodule.

Code

import pathlib


class AbfParents:
    """
    This class helps group ABFs together using filenames.
    If an ABF file is adjacent to a non-ABF file with the same basename,
    it indicates that ABF file is a "parent" and subsequent ABFs are grouped together.
    Typically the non-abf file is a .tif or .jpg photograph of the sample being recorded.
    """

    def __init__(self):
        self.abfs_by_parent = {}

    def add_folder(self, folder: str):
        folder = pathlib.Path(folder)
        abfs = folder.glob("*.abf")
        others = [x.stem for x in folder.glob("*.*") if x.suffix != ".abf"]
        last_parent = None
        for abf in abfs:
            is_parent = abf.stem in others
            if is_parent:
                self.abfs_by_parent[abf] = []
                last_parent = abf
            self.abfs_by_parent[last_parent].append(abf)

    def add_subfolders(self, folder: str):
        folder = pathlib.Path(folder)
        sub_folders = [x for x in folder.glob("*") if x.is_dir()]
        for sub_folder in sub_folders:
            self.add_folder(sub_folder)

    def __str__(self):
        total = sum([len(x) for x in self.abfs_by_parent.values()])
        return f"{total} ABFs ({len(self.abfs_by_parent)} parents)"

Example

if __name__ == "__main__":
    parents = AbfParents()
    parents.add_subfolders("/path/to/data/")

    for parent in parents.abfs_by_parent.keys():
        print("\n" + parent.name)
        for child in parents.abfs_by_parent[parent]:
            print("  " + child.name)
2023_06_26_0000.abf
  2023_06_26_0000.abf
  2023_06_26_0001.abf

2023_06_26_0002.abf
  2023_06_26_0002.abf
  2023_06_26_0003.abf
  2023_06_26_0004.abf
  2023_06_26_0005.abf
  2023_06_26_0006.abf

2023_06_26_0007.abf
  2023_06_26_0007.abf
  2023_06_26_0008.abf
  2023_06_26_0009.abf

2023_06_26_0010.abf
  2023_06_26_0010.abf
  2023_06_26_0011.abf
  2023_06_26_0012.abf
  2023_06_26_0013.abf
  2023_06_26_0014.abf
  2023_06_26_0015.abf
  2023_06_28_0000.abf
  2023_06_28_0001.abf

2023_06_26_0000.abf
  2023_06_26_0000.abf
  2023_06_26_0001.abf
  2023_06_26_0002.abf
  2023_06_26_0003.abf

2023_06_27_0000.abf
  2023_06_27_0000.abf
  2023_06_27_0001.abf
  2023_06_27_0002.abf
  2023_06_27_0003.abf
  2023_06_27_0004.abf
  2023_06_27_0005.abf

2023_06_27_0006.abf
  2023_06_27_0006.abf
  2023_06_27_0007.abf
  2023_06_27_0008.abf
  2023_06_27_0009.abf

2023_06_27_0010.abf
  2023_06_27_0010.abf
  2023_06_27_0011.abf
  2023_06_27_0012.abf
  2023_06_27_0013.abf
  2023_06_27_0014.abf
  2023_06_27_0015.abf

2023_06_27_0016.abf
  2023_06_27_0016.abf
  2023_06_27_0017.abf
  2023_06_27_0018.abf
  2023_06_27_0019.abf
  2023_06_27_0020.abf
  2023_06_27_0021.abf

2023_06_27_0022.abf
  2023_06_27_0022.abf
  2023_06_27_0023.abf
  2023_06_27_0024.abf
  2023_06_27_0025.abf
  2023_06_27_0026.abf
  2023_06_27_0027.abf
@swharden swharden closed this as not planned Won't fix, can't repro, duplicate, stale Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant