Skip to content

detectionParams module¤

The bDetection class provides an interface to get and set detection paramers.

Example
import sanpy

# grab the presets for 'SA Node' cells
dDict = sanpy.bDetection().getDetectionDict('SA Node')
ba.spikeDetect(dDict)

# tweek individual parameters
dDict['dvdtThreshold'] = 50

# load a recording
myPath = 'data/19114001.abf'
ba = sanpy.bAnalysis(myPath)

# perform spike detection
ba.spikeDetect(dDict)

# browse results

Attributes¤

Classes¤

bDetection ¤

Bases: object

Class to manage detection parameters.

Source code in sanpy/bDetection.py
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
class bDetection(object):
    """Class to manage detection parameters."""

    detectionTypes = detectionTypes_
    """ Enum with the type of spike detection, (dvdt, mv)"""

    def __init__(self, sanpy_paths: SanPyPaths | None = None) -> None:
        """Load all bundled and user detection presets.

        Args:
            sanpy_paths: Optional application path manager.
        """

        self._sanpy_paths = sanpy_paths or SanPyPaths()

        # dict with detection key and current value
        self._dDict = getDefaultDetection()

        # list of preset names including <user>SanPy/detection json files
        # use item=e[key] or item=e(value) then use item.name or item.value
        # _theDict, _userPresetsDict = self._getPresetsDict()
        self._detectionPreset = self._getPresetsDict()
        # self._detectionEnum = Enum("self.detectionEnum", self._detectionPreset)

        # dictionary of presets, each key is like 'sanode' and then value is a dict of
        #   detection param keys and their values
        # self._detectionPreset = {}
        # for item in self._detectionEnum:
            # item is like self.detectionEnum.sanode
            # logger.info(f'  loaded {item}, {item.name}, "{item.value}"')
            # was this
            # presetValues = self._getPresetValues(item)
            # if presetValues:
            #     # got value of built in preset
            #     self._detectionPreset[item.name] = presetValues
            # else:
            #     # find in user loaded presets
            #     _userKey = item.name
            #     try:
            #         self._detectionPreset[item.name] = _userPresetsDict[_userKey]
            #     except:
            #         logger.error(
            #             f"did not find item.name:{item.name} in _detectionPreset {self._detectionPreset.keys()}"
            #         )

    def getDetectionPresetList(self):
        """Get list of names of detection type.

        Used to make a list in popup in interface/ and interface/plugins
        """
        return list(self._detectionPreset.keys())

        # detectionList = []
        # for detectionPreset in self._detectionEnum:
        #     # detectionList.append(detectionPreset.value['detectionName'])
        #     detectionList.append(detectionPreset.name)
        # return detectionList

    def _old_getDetectionKey(self, humanName):
        """Map human readable name like 'SA Node' back to key 'sanode'
        """
        for detectionPreset in self._detectionEnum:
            if detectionPreset.value == humanName:
                return detectionPreset.name
        logger.error(f'did not find human name {humanName} in detection presets?')
        logger.error(f'  possible names are {self.getDetectionPresetList()}')

    def _getPresetsDict(self) -> dict:
        """Load detection presets from json files in 2 different folder:
            1) sanpy/detection-presets
            2) <user>/Documents/Sanpy/detection/

        For file name like 'Fast Neuron.json' make key 'fastneuron'

        Returns:
            userPresets (dict): One item per user file
        """

        def fileNameToKey(filePath):
            """Given full path to json file, return
            Filename without extension, and a well formed key.
            """
            fileName = os.path.split(filePath)[1]
            fileName = os.path.splitext(fileName)[0]

            fileNameKey = os.path.split(filePath)[1]
            fileNameKey = os.path.splitext(fileNameKey)[0]
            # reduce preset json filename to (lower case, no spaces, no dash)
            # fileNameKey = fileNameKey.lower()
            # fileNameKey = fileNameKey.replace(" ", "")
            # fileNameKey = fileNameKey.replace(
            #     "-", ""
            # )  # in case user specifies a '-' in file name
            return fileName, fileNameKey

        theDict = {}
        # userPresets = {}

        #
        # get files in our 'detection-presets' folder
        presetsPath = self._sanpy_paths.bundled_dir / 'detection-presets' / '*.json'
        files = glob.glob(str(presetsPath))
        for filePath in files:
            fileName, fileNameKey = fileNameToKey(filePath)
            # logger.info(f'detection-presets json fileNameKey:{fileNameKey} fileName:{fileName}')
            #
            # theDict[fileNameKey] = fileName

            if D_BJ_MANUSCRIPT:
                if 'Ca Kymograph' in filePath:
                    continue
                if 'Ca Spikes' in filePath:
                    continue
                if 'Sub Threshold' in filePath:
                    continue
                if 'Wu-iPSC' in filePath:
                    continue

            # load user preset json and grab (param keys and values)
            with open(filePath, "r") as f:
                userPresetsDict = json.load(f)
                theDict[fileNameKey] = userPresetsDict

        # theDict["sanode"] = "SA Node"
        # theDict["ventricular"] = "Ventricular"
        # theDict["neuron"] = "Neuron"
        # theDict["fastneuron"] = "Fast Neuron"
        # theDict["subthreshold"] = "Sub Threshold"
        # theDict["caspikes"] = "Ca Spikes"
        # theDict["cakymograph"] = "Ca Kymograph"

        #
        # get files in <users>/Documents/SanPy/detection/ folder
        files = self._getUserFiles()
        for filePath in files:
            fileName, fileNameKey = fileNameToKey(filePath)
            # theDict[fileNameKey] = fileName

            # load user preset json and grab (param keys and values)
            with open(filePath, "r") as f:
                userPresetsDict = json.load(f)
                theDict[fileNameKey] = userPresetsDict

        # make sure what we loaded has all the needed keys
        _defaultDetection = getDefaultDetection()
        for _fileNameKey, _loadedDetectionDict in theDict.items():
            for _key, _defaultDict in _defaultDetection.items():
                if _key not in _loadedDetectionDict.keys():
                    _loadedDetectionDict[_key] = _defaultDetection[_key]['defaultValue']

        return theDict  #, userPresets

    def _getUserFiles(self) -> list[pathlib.Path]:
        """Return paths to user detection-preset files.

        Returns:
            Detection-preset JSON paths, or an empty list when unavailable.
        """
        userDetectionPath = self._sanpy_paths.detection_dir
        if userDetectionPath.is_dir():
            return list(userDetectionPath.glob("*.json"))
        return []

    def toJson(self):
        """Get key and defaultValue"""
        theDict = {}
        for k, v in self._dDict.items():
            theDict[k] = v["defaultValue"]

        # logger.info('theDict:')
        # pprint(theDict)

        # with open(savePath, 'w') as f:
        #    json.dump(self._dDict, f, indent=4)
        theJson = json.dumps(theDict, indent=4)
        # logger.info('theJson:')
        return theJson

    def old_saveAs(
        self,
        detectionType: str,
        filename: str,
        path: str | pathlib.Path | None = None,
    ) -> None:
        """Save a detection dictionary to json.

        If running in GUI, main SanPy app will specify the correct path.

        This is only for user defined sets, we never save our built in detection (hard coded in code).

        Args:
            detectionType: human readable like 'SA Node', will become 'SA Node.json'
            filename: Name of file to save (no extension, will append .json)
            path: Optional explicit output path.
        """
        # _keyName = self._detectionEnum(detectionType).name
        if path is None:
            # get <user>/Documents/SanPy/xxx folder
            savePath = self._sanpy_paths.detection_dir / f"{filename}.json"
        else:
            savePath = pathlib.Path(path)
        logger.info(str(savePath))
        with open(savePath, "w") as f:
            dDict = self.getDetectionDict(detectionType)
            dDict["detectionName"] = filename
            json.dump(dDict, f, indent=4)

    def printDict(self):
        for k in self._dDict.keys():
            v = self._dDict[k]["currentValue"]
            print(f'  {k}: "{v}" {type(v)}')

    def getMasterDict(self, detectionType: str):
        """Get the full dictionary from self._dDict getDefaultDetection()

        This is needed by sanpy/interface/plugins/detectionParams.py
        """

        retDict = copy.deepcopy(self._dDict)

        # detectionTypeKey = self._detectionEnum(detectionType).name
        # oneType = self._detectionPreset[detectionTypeKey]

        oneType = self.getDetectionDict(detectionType)
        for k, v in oneType.items():
            retDict[k]["currentValue"] = v

        return retDict

    def getDetectionDict(self, detectionType: str, allParameter=True):
        """Get a full detection dict.

        Presets like 'SA Node' only over-ride a subset of the defaults, thus need to merge.
        User saved detection parameters will have all keys.

        Args:
            detectionType : detection type key, like 'SA Node'
        """
        return self._detectionPreset[detectionType]

        try:
            if allParameter:
                dDict = {}

                # master template to get all default values
                for k, v in self._dDict.items():
                    dDict[k] = v["currentValue"]

                # use specified detection type to get over-written values
                detectionTypeKey = self._detectionEnum(detectionType).name
                oneType = self._detectionPreset[detectionTypeKey]
                for k, v in oneType.items():
                    dDict[k] = v
            return dDict
        except KeyError as e:
            logger.error(f'Did not find detectionType:"{detectionType}"')
        except ValueError as e:
            logger.error(f'Did not find detectionType:"{detectionType}"')

    def getValue(self, detectionType: str, key):
        """Get current value from key. Valid keys are defined in getDefaultDetection().

        Args:
            detectionType : string value from enum, like 'SA Node'
        """
        try:
            # return self._dDict[key]['currentValue']
            return self._detectionPreset[detectionType][key]
        except KeyError as e:
            logger.warning(
                f'Did not find detectionType "{detectionType}" or key "{key}" to get current value'
            )
            # TODO: define default when not found ???
            return None

    def setValue(self,
                    detectionType: str,
                    key: str,
                    value):
        """Set current value for key. Valid keys are defined in getDefaultDetection.

        For float values that need to take on none, value comes in as -1e9

        Args:
            detectionType : str
                Name of detection type (corresonds to json file)
            key: str
                Detection parameter name
            value:
                the value to set
        """
        try:
            valueType = type(value)
            valueIsNumber = isinstance(value, numbers.Number)
            valueIsString = isinstance(value, str)
            valueIsBool = isinstance(value, bool)
            valueIsList = isinstance(value, list)
            valueIsNone = value is None

            # from the master list
            expectedType = self._dDict[key]["type"]  # (number, string, boolean)
            allowNone = self._dDict[key][
                "allowNone"
            ]  # used to turn off a detection param

            # logger.info(f'expectedType:{expectedType} value type is {type(value)}')

            if allowNone and valueIsNone:
                pass
            elif expectedType == "number" and not valueIsNumber:
                logger.warning(
                    f'Type mismatch (number) setting key "{key}", got {valueType}, expecting {expectedType}'
                )
                return False
            elif expectedType == "string" and not valueIsString:
                logger.warning(
                    f'Type mismatch (string) setting "{key}", got {valueType}, expecting {expectedType}'
                )
                return False
            elif expectedType == "boolean" and not valueIsBool:
                logger.warning(
                    f'Type mismatch (bool) setting "{key}", got {valueType}, expecting {expectedType}'
                )
                return False
            elif expectedType == "list" and not valueIsList:
                logger.warning(
                    f'Type mismatch (list) setting "{key}", got {valueType}, expecting {expectedType}'
                )
                return False
            """
            elif expectedType=='sanpy.bDetection.detectionTypes':
                try:
                    value = sanpy.bDetection.detectionTypes[value].name
                    #print(value == sanpy.bDetection.detectionTypes.dvdt)
                    #print(value == sanpy.bDetection.detectionTypes.mv)
                except (KeyError) as e:
                    logger.error(f'sanpy.bDetection.detectionTypes does not contain value "{value}"')
            """
            #
            # set
            # self._dDict[key]['currentValue'] = value
            self._detectionPreset[detectionType][key] = value

            logger.info(
                f"now detectionType:{detectionType} key:{key}: {self._detectionPreset[detectionType][key]} {type(self._detectionPreset[detectionType][key])}"
            )

            return True

        except KeyError as e:
            logger.warning(
                f'Did not find detectionType:{detectionType}, key:"{key}" to set current value to "{value}"'
            )
            logger.warning(f'  available detectionType are: {self._detectionPreset.keys()}')
            return False

    def old_save(self, saveBase):
        """
        Save underlying dict to json file

        Args:
            save base (str): basename to append '-detection.json'
        """

        # convert

        savePath = saveBase + "-detection.json"

        with open(savePath, "w") as f:
            json.dump(self._dDict, f, indent=4)

    def _old_load(self, loadBase):
        """
        Load detection from json file.

        Fill in underlying dict
        """

        loadPath = loadBase + "-detection.json"

        if not os.path.isfile(loadPath):
            logger.error(f"Did not find file: {loadPath}")
            return

        with open(loadPath, "r") as f:
            self._dDict = json.load(f)

        # convert

    def _old_getPresetValues(self, detectionPreset):
        """Depreciated, now loaded from json


        detectionName : corresponds to key in enum self._detectionEnum
        """

        theDict = {}

        if detectionPreset == self._detectionEnum.sanode:
            theDict["detectionName"] = "SA Node"
            theDict["dvdtThreshold"] = 20
            theDict["mvThreshold"] = -20
            theDict["refractory_ms"] = 170  # max freq of 5 Hz
            theDict["peakWindow_ms"] = 100
            theDict["halfWidthWindow_ms"] = 200
            theDict["preSpikeClipWidth_ms"] = 200
            theDict["postSpikeClipWidth_ms"] = 500
        elif detectionPreset == self._detectionEnum.ventricular:
            theDict["detectionName"] = "Ventricular"
            theDict["dvdtThreshold"] = 100
            theDict["mvThreshold"] = -20
            theDict["refractory_ms"] = 200  # max freq of 5 Hz
            theDict["peakWindow_ms"] = 100
            theDict["halfWidthWindow_ms"] = 300
            theDict["preSpikeClipWidth_ms"] = 200
            theDict["postSpikeClipWidth_ms"] = 500
        elif detectionPreset == self._detectionEnum.neuron:
            theDict["detectionName"] = "Neuron"
            theDict["dvdtThreshold"] = 20
            theDict["mvThreshold"] = -40
            theDict["refractory_ms"] = 4
            theDict["peakWindow_ms"] = 5
            theDict["halfWidthWindow_ms"] = 4
            theDict["dvdtPreWindow_ms"] = 2
            theDict["dvdtPostWindow_ms"] = 2
            theDict["preSpikeClipWidth_ms"] = 2
            theDict["postSpikeClipWidth_ms"] = 2
        elif detectionPreset == self._detectionEnum.fastneuron:
            theDict["detectionName"] = "Fast Neuron"
            theDict["dvdtThreshold"] = 20
            theDict["mvThreshold"] = -40
            theDict["refractory_ms"] = 3
            theDict["peakWindow_ms"] = 2
            theDict["halfWidthWindow_ms"] = 4
            theDict["dvdtPreWindow_ms"] = 2
            theDict["dvdtPostWindow_ms"] = 2
            theDict["preSpikeClipWidth_ms"] = 2
            theDict["postSpikeClipWidth_ms"] = 2
        elif detectionPreset == self._detectionEnum.subthreshold:
            theDict["detectionName"] = "Subthreshold"
            theDict["dvdtThreshold"] = math.nan
            theDict["mvThreshold"] = -20  # user specifies
            theDict["refractory_ms"] = 100  # max freq is 10 Hz
            theDict["peakWindow_ms"] = 50
            theDict["halfWidthWindow_ms"] = 100
            theDict["preSpikeClipWidth_ms"] = 100
            theDict["postSpikeClipWidth_ms"] = 200
            theDict["onlyPeaksAbove_mV"] = None
            theDict["onlyPeaksBelow_mV"] = -20
            # todo: add onlyPeaksBelow_mV
        elif detectionPreset == self._detectionEnum.caspikes:
            theDict["detectionName"] = "Ca Spikes"
            # theDict['detectionType'] = sanpy.bDetection.detectionTypes.mv # ('dvdt', 'mv')
            theDict[
                "dvdtThreshold"
            ] = math.nan  # if None then detect only using mvThreshold
            theDict["mvThreshold"] = 0.5
            # theDict['refractory_ms'] = 200 #170 # reject spikes with instantaneous frequency
            # theDict['halfWidthWindow_ms'] = 200 #was 20
        elif detectionPreset == self._detectionEnum.cakymograph:
            theDict["detectionName"] = "Ca Kymograph"
            theDict["detectionType"] = sanpy.bDetection.detectionTypes["dvdt"].value
            # rosie, was math.nan #if None then detect only using mvThreshold
            theDict["dvdtThreshold"] = 0.05
            theDict["mvThreshold"] = 0.5
            theDict["peakWindow_ms"] = 400  # rosie, was 700
            theDict["halfWidthWindow_ms"] = 400  # rosie, was 800
            theDict["refractory_ms"] = 500
            theDict["doBackupSpikeVm"] = False
            # theDict['SavitzkyGolay_pnts'] = 5
            theDict["preSpikeClipWidth_ms"] = 200
            theDict["postSpikeClipWidth_ms"] = 1000
        else:
            logger.error(f"did not understand detectionPreset: {detectionPreset}")

        return theDict

Attributes¤

detectionTypes = detectionTypes_ class-attribute instance-attribute ¤

Enum with the type of spike detection, (dvdt, mv)

Methods:¤

__init__(sanpy_paths=None) ¤

Load all bundled and user detection presets.

Args: sanpy_paths: Optional application path manager.

Source code in sanpy/bDetection.py
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
def __init__(self, sanpy_paths: SanPyPaths | None = None) -> None:
    """Load all bundled and user detection presets.

    Args:
        sanpy_paths: Optional application path manager.
    """

    self._sanpy_paths = sanpy_paths or SanPyPaths()

    # dict with detection key and current value
    self._dDict = getDefaultDetection()

    # list of preset names including <user>SanPy/detection json files
    # use item=e[key] or item=e(value) then use item.name or item.value
    # _theDict, _userPresetsDict = self._getPresetsDict()
    self._detectionPreset = self._getPresetsDict()
getDetectionDict(detectionType, allParameter=True) ¤

Get a full detection dict.

Presets like 'SA Node' only over-ride a subset of the defaults, thus need to merge. User saved detection parameters will have all keys.

Args: detectionType : detection type key, like 'SA Node'

Source code in sanpy/bDetection.py
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
def getDetectionDict(self, detectionType: str, allParameter=True):
    """Get a full detection dict.

    Presets like 'SA Node' only over-ride a subset of the defaults, thus need to merge.
    User saved detection parameters will have all keys.

    Args:
        detectionType : detection type key, like 'SA Node'
    """
    return self._detectionPreset[detectionType]

    try:
        if allParameter:
            dDict = {}

            # master template to get all default values
            for k, v in self._dDict.items():
                dDict[k] = v["currentValue"]

            # use specified detection type to get over-written values
            detectionTypeKey = self._detectionEnum(detectionType).name
            oneType = self._detectionPreset[detectionTypeKey]
            for k, v in oneType.items():
                dDict[k] = v
        return dDict
    except KeyError as e:
        logger.error(f'Did not find detectionType:"{detectionType}"')
    except ValueError as e:
        logger.error(f'Did not find detectionType:"{detectionType}"')
getDetectionPresetList() ¤

Get list of names of detection type.

Used to make a list in popup in interface/ and interface/plugins

Source code in sanpy/bDetection.py
676
677
678
679
680
681
def getDetectionPresetList(self):
    """Get list of names of detection type.

    Used to make a list in popup in interface/ and interface/plugins
    """
    return list(self._detectionPreset.keys())
getMasterDict(detectionType) ¤

Get the full dictionary from self._dDict getDefaultDetection()

This is needed by sanpy/interface/plugins/detectionParams.py

Source code in sanpy/bDetection.py
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
def getMasterDict(self, detectionType: str):
    """Get the full dictionary from self._dDict getDefaultDetection()

    This is needed by sanpy/interface/plugins/detectionParams.py
    """

    retDict = copy.deepcopy(self._dDict)

    # detectionTypeKey = self._detectionEnum(detectionType).name
    # oneType = self._detectionPreset[detectionTypeKey]

    oneType = self.getDetectionDict(detectionType)
    for k, v in oneType.items():
        retDict[k]["currentValue"] = v

    return retDict
getValue(detectionType, key) ¤

Get current value from key. Valid keys are defined in getDefaultDetection().

Args: detectionType : string value from enum, like 'SA Node'

Source code in sanpy/bDetection.py
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
def getValue(self, detectionType: str, key):
    """Get current value from key. Valid keys are defined in getDefaultDetection().

    Args:
        detectionType : string value from enum, like 'SA Node'
    """
    try:
        # return self._dDict[key]['currentValue']
        return self._detectionPreset[detectionType][key]
    except KeyError as e:
        logger.warning(
            f'Did not find detectionType "{detectionType}" or key "{key}" to get current value'
        )
        # TODO: define default when not found ???
        return None
old_save(saveBase) ¤

Save underlying dict to json file

Args: save base (str): basename to append '-detection.json'

Source code in sanpy/bDetection.py
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
def old_save(self, saveBase):
    """
    Save underlying dict to json file

    Args:
        save base (str): basename to append '-detection.json'
    """

    # convert

    savePath = saveBase + "-detection.json"

    with open(savePath, "w") as f:
        json.dump(self._dDict, f, indent=4)
old_saveAs(detectionType, filename, path=None) ¤

Save a detection dictionary to json.

If running in GUI, main SanPy app will specify the correct path.

This is only for user defined sets, we never save our built in detection (hard coded in code).

Args: detectionType: human readable like 'SA Node', will become 'SA Node.json' filename: Name of file to save (no extension, will append .json) path: Optional explicit output path.

Source code in sanpy/bDetection.py
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
def old_saveAs(
    self,
    detectionType: str,
    filename: str,
    path: str | pathlib.Path | None = None,
) -> None:
    """Save a detection dictionary to json.

    If running in GUI, main SanPy app will specify the correct path.

    This is only for user defined sets, we never save our built in detection (hard coded in code).

    Args:
        detectionType: human readable like 'SA Node', will become 'SA Node.json'
        filename: Name of file to save (no extension, will append .json)
        path: Optional explicit output path.
    """
    # _keyName = self._detectionEnum(detectionType).name
    if path is None:
        # get <user>/Documents/SanPy/xxx folder
        savePath = self._sanpy_paths.detection_dir / f"{filename}.json"
    else:
        savePath = pathlib.Path(path)
    logger.info(str(savePath))
    with open(savePath, "w") as f:
        dDict = self.getDetectionDict(detectionType)
        dDict["detectionName"] = filename
        json.dump(dDict, f, indent=4)
setValue(detectionType, key, value) ¤

Set current value for key. Valid keys are defined in getDefaultDetection.

For float values that need to take on none, value comes in as -1e9

Args: detectionType : str Name of detection type (corresonds to json file) key: str Detection parameter name value: the value to set

Source code in sanpy/bDetection.py
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
def setValue(self,
                detectionType: str,
                key: str,
                value):
    """Set current value for key. Valid keys are defined in getDefaultDetection.

    For float values that need to take on none, value comes in as -1e9

    Args:
        detectionType : str
            Name of detection type (corresonds to json file)
        key: str
            Detection parameter name
        value:
            the value to set
    """
    try:
        valueType = type(value)
        valueIsNumber = isinstance(value, numbers.Number)
        valueIsString = isinstance(value, str)
        valueIsBool = isinstance(value, bool)
        valueIsList = isinstance(value, list)
        valueIsNone = value is None

        # from the master list
        expectedType = self._dDict[key]["type"]  # (number, string, boolean)
        allowNone = self._dDict[key][
            "allowNone"
        ]  # used to turn off a detection param

        # logger.info(f'expectedType:{expectedType} value type is {type(value)}')

        if allowNone and valueIsNone:
            pass
        elif expectedType == "number" and not valueIsNumber:
            logger.warning(
                f'Type mismatch (number) setting key "{key}", got {valueType}, expecting {expectedType}'
            )
            return False
        elif expectedType == "string" and not valueIsString:
            logger.warning(
                f'Type mismatch (string) setting "{key}", got {valueType}, expecting {expectedType}'
            )
            return False
        elif expectedType == "boolean" and not valueIsBool:
            logger.warning(
                f'Type mismatch (bool) setting "{key}", got {valueType}, expecting {expectedType}'
            )
            return False
        elif expectedType == "list" and not valueIsList:
            logger.warning(
                f'Type mismatch (list) setting "{key}", got {valueType}, expecting {expectedType}'
            )
            return False
        """
        elif expectedType=='sanpy.bDetection.detectionTypes':
            try:
                value = sanpy.bDetection.detectionTypes[value].name
                #print(value == sanpy.bDetection.detectionTypes.dvdt)
                #print(value == sanpy.bDetection.detectionTypes.mv)
            except (KeyError) as e:
                logger.error(f'sanpy.bDetection.detectionTypes does not contain value "{value}"')
        """
        #
        # set
        # self._dDict[key]['currentValue'] = value
        self._detectionPreset[detectionType][key] = value

        logger.info(
            f"now detectionType:{detectionType} key:{key}: {self._detectionPreset[detectionType][key]} {type(self._detectionPreset[detectionType][key])}"
        )

        return True

    except KeyError as e:
        logger.warning(
            f'Did not find detectionType:{detectionType}, key:"{key}" to set current value to "{value}"'
        )
        logger.warning(f'  available detectionType are: {self._detectionPreset.keys()}')
        return False
toJson() ¤

Get key and defaultValue

Source code in sanpy/bDetection.py
794
795
796
797
798
799
800
801
802
803
804
805
806
807
def toJson(self):
    """Get key and defaultValue"""
    theDict = {}
    for k, v in self._dDict.items():
        theDict[k] = v["defaultValue"]

    # logger.info('theDict:')
    # pprint(theDict)

    # with open(savePath, 'w') as f:
    #    json.dump(self._dDict, f, indent=4)
    theJson = json.dumps(theDict, indent=4)
    # logger.info('theJson:')
    return theJson

detectionTypes_ ¤

Bases: Enum

Detection type is one of (dvdt, mv).

dvdt: Search for threshold crossings in first derivative of membrane potential. mv: Search for threshold crossings in membrane potential.

Source code in sanpy/bDetection.py
55
56
57
58
59
60
61
62
63
64
class detectionTypes_(Enum):
    """
    Detection type is one of (dvdt, mv).

    dvdt: Search for threshold crossings in first derivative of membrane potential.
    mv: Search for threshold crossings in membrane potential.
    """

    dvdt = "dvdt"
    mv = "mv"

Functions:¤

getDefaultDetection() ¤

Build the authoritative detection-parameter definition dictionary.

Returns: A new ordered dictionary mapping internal parameter names to their runtime definitions, including presentation categories.

Source code in sanpy/bDetection.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
def getDefaultDetection() -> dict[str, dict[str, Any]]:
    """Build the authoritative detection-parameter definition dictionary.

    Returns:
        A new ordered dictionary mapping internal parameter names to their
        runtime definitions, including presentation categories.
    """

    theDict = OrderedDict()  # {}

    """
    key = 'include'
    theDict[key] = {}
    theDict[key]['defaultValue'] = True
    theDict[key]['type'] = 'bool'
    theDict[key]['allowNone'] = False
    theDict[key]['units'] = ''
    theDict[key]['humanName'] = 'Include'
    theDict[key]['errors'] = ('')
    theDict[key]['description'] = 'Include analysis for this file'
    """

    key = "detectionName"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.GENERAL
    theDict[key]["defaultValue"] = "default"  # detectionPreset.value # ('dvdt', 'mv')
    theDict[key]["type"] = "string"
    theDict[key][
        "allowNone"
    ] = False  # To do, have 2x entry points to bAnalysis detect, never set this to nan
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "Detection Preset Name"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "The name of detection preset"

    key = "userSaveName"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.GENERAL
    theDict[key]["defaultValue"] = ""  # detectionPreset.value # ('dvdt', 'mv')
    theDict[key]["type"] = "string"
    theDict[key][
        "allowNone"
    ] = False  # To do, have 2x entry points to bAnalysis detect, never set this to nan
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "Saved Detection Params"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "The name of saved user detection params"

    key = "detectionType"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.DETECTION
    theDict[key]["defaultValue"] = sanpy.bDetection.detectionTypes[
        "dvdt"
    ].value  # ('dvdt', 'mv')
    theDict[key]["type"] = "sanpy.bDetection.detectionTypes"
    theDict[key][
        "allowNone"
    ] = False  # To do, have 2x entry points to bAnalysis detect, never set this to nan
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "Detection Type"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "Detect using derivative (dvdt) or membrane potential (mV)"

    key = "dvdtThreshold"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.DETECTION
    theDict[key]["defaultValue"] = 20
    theDict[key]["type"] = "float"
    theDict[key][
        "allowNone"
    ] = True  # To do, have 2x entry points to bAnalysis detect, never set this to nan
    theDict[key]["units"] = "dVdt"
    theDict[key]["humanName"] = "dV/dt Threshold"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "dV/dt threshold for a spike, will be backed up to dvdt_percentOfMax and have xxx error when this fails"

    key = "mvThreshold"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.DETECTION
    theDict[key]["defaultValue"] = -20
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "mV"
    theDict[key]["humanName"] = "mV Threshold"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "mV threshold for spike AND minimum spike mV when detecting with dV/dt"

    key = "startSeconds"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = None
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = True
    theDict[key]["units"] = "s"
    theDict[key]["humanName"] = "Start(s)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Start seconds of analysis"

    key = "stopSeconds"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = None
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = True
    theDict[key]["units"] = "s"
    theDict[key]["humanName"] = "Stop(s)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Stop seconds of analysis"

    key = "cellType"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.GENERAL
    theDict[key]["defaultValue"] = ""
    theDict[key]["type"] = "string"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "Cell Type"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Cell Type"

    key = "sex"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.GENERAL
    theDict[key]["defaultValue"] = ""
    theDict[key]["type"] = "string"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "Sex"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Sex"

    key = "condition"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.GENERAL
    theDict[key]["defaultValue"] = ""
    theDict[key]["type"] = "string"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "Condition"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Condition"

    key = "userType"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.GENERAL
    theDict[key]["defaultValue"] = "0"
    theDict[key]["type"] = "int"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "User Type"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "User Type"

    key = "dvdt_percentOfMax"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.DETECTION
    theDict[key]["defaultValue"] = 0.1
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "Percent"
    theDict[key]["humanName"] = "dV/dt Percent of max"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "For dV/dt detection, the final TOP is when dV/dt drops to this percent from dV/dt AP peak"

    key = "onlyPeaksAbove_mV"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WAVEFORM
    theDict[key]["defaultValue"] = None
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = True
    theDict[key]["units"] = "mV"
    theDict[key]["humanName"] = "Accept Peaks Above (mV)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Only accept APs with peaks above this value (mV)"

    key = "onlyPeaksBelow_mV"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WAVEFORM
    theDict[key]["defaultValue"] = None
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = True
    theDict[key]["units"] = "mV"
    theDict[key]["humanName"] = "Accept Peaks Below (mV)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Only accept APs below this value (mV)"

    # TODO: get rid of this and replace with foot
    key = "doBackupSpikeVm"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.ADVANCED
    theDict[key]["defaultValue"] = False
    theDict[key]["type"] = "boolean"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "Boolean"
    theDict[key]["humanName"] = "Backup Vm Spikes"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "If true, APs detected with just mV will be backed up until Vm falls to xxx"

    key = "refractory_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 170
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "Minimum AP interval (ms)"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "APs with interval (wrt previous AP) less than this will be removed"

    key = "peakWindow_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 100
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "Peak Window (ms)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Window after TOP (ms) to seach for AP peak (mV)"

    key = "dvdtPreWindow_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 10
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "dV/dt Pre Window (ms)"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "Window (ms) to search before each TOP for real threshold crossing"

    key = "dvdtPostWindow_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 20
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "dV/dt Post Window (ms)"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "Window (ms) to search after each AP peak for minimum in dv/dt"

    key = "mdp_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.DETECTION
    theDict[key]["defaultValue"] = 250
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "Pre AP MDP window (ms)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Window (ms) before an AP to look for MDP"

    key = "avgWindow_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 5
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "MDP averaging window (ms)"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "Window (ms) to calculate MDP (mV) as a mean rather than mV at single point for MDP"

    key = "lowEddRate_warning"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.ADVANCED
    theDict[key]["defaultValue"] = 8
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "EDD slope"
    theDict[key]["humanName"] = "EDD slope warning"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "Generate warning when EED slope is lower than this value."

    key = "halfHeights"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WAVEFORM
    theDict[key]["defaultValue"] = [10, 20, 50, 80, 90]
    theDict[key]["type"] = "list"  # list of number
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "AP Durations (%)"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "AP Durations as percent of AP height (AP Peak (mV) - TOP (mV))"

    key = "halfWidthWindow_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 200
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "Half Width Window (ms)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Window (ms) after TOP to look for AP Durations"

    key = "preSpikeClipWidth_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 200
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "Pre AP Clip Width (ms)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "The pre duration of generated AP clips (Before AP)"

    key = "postSpikeClipWidth_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 500
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "Post AP Clip Width (ms)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "The post duration of generated AP clips (After AP)"

    # new 20231201, for mich lab
    key = "fastAhpWindow_ms"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.WINDOWS
    theDict[key]["defaultValue"] = 8
    theDict[key]["type"] = "float"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "ms"
    theDict[key]["humanName"] = "Fast AHP Window (ms)"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Window (ms) after peak to look for a fast AHP"

    key = "medianFilter"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.FILTERING
    theDict[key]["defaultValue"] = 0
    theDict[key]["type"] = "int"
    theDict[key]["allowNone"] = True  # 0 is no median filter (see SavitzkyGolay_pnts)
    theDict[key]["units"] = "points"
    theDict[key]["humanName"] = "Median Filter Points"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "Number of points in median filter, must be odd, 0 for no filter"

    key = "SavitzkyGolay_pnts"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.FILTERING
    theDict[key]["defaultValue"] = 5  # 20211001 was 5
    theDict[key]["type"] = "int"
    theDict[key]["allowNone"] = True  # 0 is no filter
    theDict[key]["units"] = "points"
    theDict[key]["humanName"] = "SavitzkyGolay Points"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "Number of points in SavitzkyGolay filter, must be odd, 0 for no filter"

    key = "SavitzkyGolay_poly"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.FILTERING
    theDict[key]["defaultValue"] = 2
    theDict[key]["type"] = "int"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = ""
    theDict[key]["humanName"] = "SavitzkyGolay Poly Deg"
    theDict[key]["errors"] = ""
    theDict[key][
        "description"
    ] = "The degree of the polynomial for Savitzky-Golay filter"

    # key = 'dateAnalyzed'
    # theDict[key] = {}
    # theDict[key]['defaultValue'] = ''
    # theDict[key]['type'] = 'str'
    # theDict[key]['allowNone'] = False
    # theDict[key]['units'] = ''
    # theDict[key]['humanName'] = 'Date Analyzed'
    # theDict[key]['errors'] = ('')
    # theDict[key]['description'] = 'The date of analysis (yyyymmdd)'

    key = "verbose"
    theDict[key] = {}
    theDict[key]["category"] = DetectionParameterCategory.ADVANCED
    theDict[key]["defaultValue"] = False
    theDict[key]["type"] = "boolean"
    theDict[key]["allowNone"] = False
    theDict[key]["units"] = "Boolean"
    theDict[key]["humanName"] = "Verbose"
    theDict[key]["errors"] = ""
    theDict[key]["description"] = "Verbose Detection Reporting"

    # assign each detection param current value to it default value
    for k, v in theDict.items():
        defaultValue = theDict[k]["defaultValue"]
        theDict[k]["currentValue"] = defaultValue

    """
    if detectionPreset == bDetection.detectionPresets.default:
        # these are defaults from above
        pass
    elif detectionPreset == bDetection.detectionPresets.sanode:
        # these are defaults from above
        pass
    elif detectionPreset == bDetection.detectionPresets.ventricular:
        theDict['dvdtThreshold']['defaultValue'] = 100
        theDict['mvThreshold']['defaultValue'] = -20
        theDict['refractory_ms']['defaultValue'] = 200  # max freq of 5 Hz
        theDict['peakWindow_ms']['defaultValue'] = 100
        theDict['halfWidthWindow_ms']['defaultValue'] = 300
        theDict['preSpikeClipWidth_ms']['defaultValue'] = 200
        theDict['postSpikeClipWidth_ms']['defaultValue'] = 500
    elif detectionPreset == bDetection.detectionPresets.neuron:
        theDict['dvdtThreshold']['defaultValue'] = 20
        theDict['mvThreshold']['defaultValue'] = -40
        theDict['refractory_ms']['defaultValue'] = 4
        theDict['peakWindow_ms']['defaultValue'] = 5
        theDict['halfWidthWindow_ms']['defaultValue'] = 4
        theDict['dvdtPreWindow_ms']['defaultValue'] = 2
        theDict['dvdtPostWindow_ms']['defaultValue'] = 2
        theDict['preSpikeClipWidth_ms']['defaultValue'] = 2
        theDict['postSpikeClipWidth_ms']['defaultValue'] = 2
    elif detectionPreset == bDetection.detectionPresets.fastneuron:
        theDict['dvdtThreshold']['defaultValue'] = 20
        theDict['mvThreshold']['defaultValue'] = -40
        theDict['refractory_ms']['defaultValue'] = 3
        theDict['peakWindow_ms']['defaultValue'] = 2
        theDict['halfWidthWindow_ms']['defaultValue'] = 4
        theDict['dvdtPreWindow_ms']['defaultValue'] = 2
        theDict['dvdtPostWindow_ms']['defaultValue'] = 2
        theDict['preSpikeClipWidth_ms']['defaultValue'] = 2
        theDict['postSpikeClipWidth_ms']['defaultValue'] = 2
    elif detectionPreset == bDetection.detectionPresets.subthreshold:
        theDict['dvdtThreshold']['defaultValue'] = math.nan
        theDict['mvThreshold']['defaultValue'] = -20  # user specifies
        theDict['refractory_ms']['defaultValue'] = 100  # max freq is 10 Hz
        theDict['peakWindow_ms']['defaultValue'] = 50
        theDict['halfWidthWindow_ms']['defaultValue'] = 100
        theDict['preSpikeClipWidth_ms']['defaultValue'] = 100
        theDict['postSpikeClipWidth_ms']['defaultValue'] = 200
        theDict['onlyPeaksAbove_mV']['defaultValue'] = None
        theDict['onlyPeaksBelow_mV']['defaultValue'] = -20
        # todo: add onlyPeaksBelow_mV
    elif detectionPreset == bDetection.detectionPresets.caspikes:
        #theDict['detectionType']['defaultValue'] = sanpy.bDetection.detectionTypes.mv # ('dvdt', 'mv')
        theDict['dvdtThreshold']['defaultValue'] = math.nan #if None then detect only using mvThreshold
        theDict['mvThreshold']['defaultValue'] = 0.5
        #theDict['refractory_ms']['defaultValue'] = 200 #170 # reject spikes with instantaneous frequency
        #theDict['halfWidthWindow_ms']['defaultValue'] = 200 #was 20
    elif detectionPreset == bDetection.detectionPresets.cakymograph:
        theDict['detectionType']['defaultValue'] = sanpy.bDetection.detectionTypes['mv'].value
        theDict['dvdtThreshold']['defaultValue'] = math.nan #if None then detect only using mvThreshold
        theDict['mvThreshold']['defaultValue'] = 1.2
        theDict['peakWindow_ms']['defaultValue'] = 700
        theDict['halfWidthWindow_ms']['defaultValue'] = 800
        theDict['refractory_ms']['defaultValue'] = 500
        theDict['doBackupSpikeVm']['defaultValue'] = False
        # theDict['SavitzkyGolay_pnts']['defaultValue'] = 5
        theDict['preSpikeClipWidth_ms']['defaultValue'] = 200
        theDict['postSpikeClipWidth_ms']['defaultValue'] = 1000
    else:
        logger.error(f'Did not understand detection type "{detectionPreset}"')
        logger.error(f'    bDetection.detectionPresets.fastneuron: {bDetection.detectionPresets.fastneuron}')
        logger.error(f'    type(bDetection.detectionPresets.fastneuron): {type(bDetection.detectionPresets.fastneuron)}')
        logger.error(f'    detectionPreset == bDetection.detectionPresets.fastneuron: {detectionPreset == bDetection.detectionPresets.fastneuron}')
    """

    # assign each detection param current value to it default value
    """
    for k,v in theDict.items():
        defaultValue = theDict[k]['defaultValue']
        theDict[k]['currentValue'] = defaultValue
    """

    return theDict.copy()

printDocs() ¤

Print or save human-readable detection-parameter documentation.

This developer utility currently writes a CSV file used when maintaining SanPy documentation.

Source code in sanpy/bDetection.py
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
def printDocs() -> None:
    """Print or save human-readable detection-parameter documentation.

    This developer utility currently writes a CSV file used when maintaining
    SanPy documentation.
    """
    logger.info("")

    import pandas as pd

    # detectionPreset = bDetection.detectionPresets.default  # detectionPresets_ is an enum class
    d = getDefaultDetection()
    dictList = []
    for k, v in d.items():
        parameter = k
        oneDict = {
            "Parameter": parameter,
            "Category": v["category"],
            "Default Value": v["defaultValue"],
            "Units": v["units"],
            "Human Readable": v["humanName"],
            "Description": v["description"],
        }
        dictList.append(oneDict)
    #
    df = pd.DataFrame(dictList)

    # spit out markdown to copy/paste into mkdocs md file
    # REMEMBER: This requires `pip install tabulate`
    # outStr = df.to_markdown()
    # print(outStr)

    # save to csv for making a table for manuscript
    path = "/Users/cudmore/Desktop/sanpy-detection-params-20230316.csv"
    print("saving to:", path)
    df.to_csv(path, index=False)

test_0() ¤

Testing get/set of detection params

Source code in sanpy/bDetection.py
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
def test_0():
    """
    Testing get/set of detection params
    """

    bd = bDetection()

    # xxx = bd.getValue('xxx')

    # ok = bd.setValue('xxx', 2)
    # print('ok:', ok)

    ok1 = bd.setValue("dvdtThreshold", None)
    if not ok1:
        print("ok1:", ok1)

    ok1_5 = bd.setValue("mvThreshold", None)
    if not ok1_5:
        print("failure ok ok1_5:", ok1_5)

    ok2 = bd.setValue("cellType", "111")
    if not ok2:
        print("ok2:", ok2)

    # for setting list, check that (i) not empty and (ii) list[i] == expected type
    ok3 = bd.setValue("halfHeights", [])
    if not ok3:
        print("ok3:", ok3)

    # start/stop seconds defaults to None but we want 'number'
    ok4 = bd.setValue("startSeconds", 1e6)
    if not ok4:
        print("ok4:", ok4)

    tmpDict = {
        "Idx": 2.0,
        "Include": 1.0,
        "File": "19114001.abf",
        "Dur(s)": 60.0,
        "kHz": 20.0,
        "Mode": "fix",
        "Cell Type": "",
        "Sex": "",
        "Condition": "",
        "Start(s)": math.nan,
        "Stop(s)": math.nan,
        "dvdtThreshold": 50.0,
        "mvThreshold": -20.0,
        "refractory_ms": math.nan,
        "peakWindow_ms": math.nan,
        "halfWidthWindow_ms": math.nan,
        "Notes": "",
    }
    for k, v in tmpDict.items():
        print("  ", k, ":", v)
    okSetFromDict = bd.setFromDict(tmpDict)
All material is Copyright 2019-2026 Robert H. Cudmore