def __init__(self, sort1dir, sort2dir, exclude_groups=[], SampleRate = 30000): """ Compares klusta to spyking-circus for now. Does NOT yet compare manually clustered data in Phy (2020MAY25). :param sort1dir: directory with klusta spike sorting files :param sort2dir: directory with spyking-circus sorting files :param exclude_groups: Phy unit classifications ['good','mua','noise','unsorted'] to exclude, (e.g. ['noise','unsorted'] will only include good units and multi-unit activity. default = 30000 :param SampleRate in Hz. default = 30000 """ # Load in data self.sorting1, self.sort1name = load_sorted_data(sort1dir, exclude_groups) self.sorting2, self.sort2name = load_sorted_data(sort2dir, exclude_groups) self.SampleRate = SampleRate # Start out doing this for just Klusta and Spyking-Circus and Phy manually # self.sort1name = sort1name # self.sort2name = sort2name # self.sorting1 = se.KlustaSortingExtractor(sort1dir) # try: # Look for spyking circus, if not there load phy folder # self.sorting2 = se.SpykingCircusSortingExtractor(sort2dir) # except AssertionError: # self.sorting2 = se.PhySortingExtractor(sort2dir) # self.sort2name = 'Phy manual clustering' self.cmp_sorters = sc.compare_two_sorters(sorting1=self.sorting1, sorting2=self.sorting2, sorting1_name=self.sort1name, sorting2_name=self.sort2name)
def test_compare_two_sorters(): # simple match sorting1, sorting2 = make_sorting([100, 200, 300, 400], [0, 0, 1, 0], [101, 201, 301, ], [0, 0, 5]) sc = compare_two_sorters(sorting1, sorting2) print(sc.agreement_scores)
def compare_sorters(sort1, sort2): comp_KL_MS4 = sc.compare_two_sorters(sorting1=sort1, sorting2=sort2) mapped_units = comp_KL_MS4.get_mapped_sorting1().get_mapped_unit_ids() print('Klusta units:', sort1.get_unit_ids()) print('Mapped Mountainsort4 units:', mapped_units) comp_multi = sc.compare_multiple_sorters(sorting_list=[sort1, sort2], name_list=['klusta', 'ms4']) sorting_agreement = comp_multi.get_agreement_sorting(minimum_matching=2) print('Units in agreement between Klusta and Mountainsort4:', sorting_agreement.get_unit_ids()) w_multi = sw.plot_multicomp_graph(comp_multi) plt.show()
############################################################################# # Then run two spike sorters and compare their output. sorting_HS = ss.run_herdingspikes(recording) sorting_TDC = ss.run_tridesclous(recording) ############################################################################# # The :code:`compare_two_sorters` function allows us to compare the spike # sorting output. It returns a :code:`SortingComparison` object, with methods # to inspect the comparison output easily. The comparison matches the # units by comparing the agreement between unit spike trains. # # Let’s see how to inspect and access this matching. cmp_HS_TDC = sc.compare_two_sorters(sorting1=sorting_HS, sorting2=sorting_TDC, sorting1_name='HS', sorting2_name='TDC') ############################################################################# # We can check the agreement matrix to inspect the matching. sw.plot_agreement_matrix(cmp_HS_TDC) ############################################################################# # Some useful internal dataframes help to check the match and count # like **match_event_count** or **agreement_scores** print(cmp_HS_TDC.match_event_count) print(cmp_HS_TDC.agreement_scores) #############################################################################
sorting_curated_snr = st.curation.threshold_snr(sorting_KL, recording, threshold=5) snrs_above = st.validation.compute_snrs(sorting_curated_snr, recording_cmr) print('Curated SNR', snrs_above) ############################################################################## # The final part of this tutorial deals with comparing spike sorting outputs. # We can either (1) compare the spike sorting results with the ground-truth sorting :code:`sorting_true`, (2) compare # the output of two (Klusta and Mountainsor4), or (3) compare the output of multiple sorters: comp_gt_KL = sc.compare_sorter_to_ground_truth(gt_sorting=sorting_true, tested_sorting=sorting_KL) comp_KL_MS4 = sc.compare_two_sorters(sorting1=sorting_KL, sorting2=sorting_MS4) comp_multi = sc.compare_multiple_sorters( sorting_list=[sorting_MS4, sorting_KL], name_list=['klusta', 'ms4']) ############################################################################## # When comparing with a ground-truth sorting extractor (1), you can get the sorting performance and plot a confusion # matrix comp_gt_KL.get_performance() w_conf = sw.plot_confusion_matrix(comp_gt_KL) ############################################################################## # When comparing two sorters (2), we can see the matching of units between sorters. For example, this is how to extract # the unit ids of Mountainsort4 (sorting2) mapped to the units of Klusta (sorting1). Units which are not mapped has -1 # as unit id.
print(keep_mask) keep_unit_ids = keep_mask[keep_mask].index.values print(keep_unit_ids) curated_sorting = sorting_TDC.select_units(keep_unit_ids) print(curated_sorting) ############################################################################## # The final part of this tutorial deals with comparing spike sorting outputs. # We can either (1) compare the spike sorting results with the ground-truth # sorting :code:`sorting_true`, (2) compare the output of two (HerdingSpikes # and Tridesclous), or (3) compare the output of multiple sorters: comp_gt_TDC = sc.compare_sorter_to_ground_truth(gt_sorting=sorting_true, tested_sorting=sorting_TDC) comp_TDC_HS = sc.compare_two_sorters(sorting1=sorting_TDC, sorting2=sorting_HS) comp_multi = sc.compare_multiple_sorters(sorting_list=[sorting_TDC, sorting_HS], name_list=['tdc', 'hs']) ############################################################################## # When comparing with a ground-truth sorting extractor (1), you can get the sorting performance and plot a confusion # matrix comp_gt_TDC.get_performance() w_conf = sw.plot_confusion_matrix(comp_gt_TDC) w_agr = sw.plot_agreement_matrix(comp_gt_TDC) ############################################################################## # When comparing two sorters (2), we can see the matching of units between sorters. # Units which are not matched has -1 as unit id:
sw.plot_agreement_matrix(comp_MATLAB, ordered=True, count_text=True) perf_MATLAB = comp_MATLAB.get_performance() plt.show() print(perf_MATLAB) #comparing the sorting algos #We will try to compare all the three sorters mcmp = sc.compare_multiple_sorters( sorting_list=[sortingPipeline, sorting_MS4, sorting_SP], name_list=['Our', 'MS4', 'SP'], verbose=True) sw.plot_multicomp_graph(mcmp) plt.show() #Pairwise cmp_MS4_Our = sc.compare_two_sorters(sorting1=sorting_MS4, sorting2=sortingPipeline, sorting1_name='MS4', sorting2_name='Our') sw.plot_agreement_matrix(cmp_MS4_Our, ordered=True, count_text=True) plt.show() cmp_SP_Our = sc.compare_two_sorters(sorting1=sorting_SP, sorting2=sortingPipeline, sorting1_name='SP', sorting2_name='Our') sw.plot_agreement_matrix(cmp_SP_Our, ordered=True, count_text=True) plt.show()
############################################################################# # Then run two spike sorters and compare their ouput. sorting_KL = sorters.run_klusta(recording) sorting_MS4 = sorters.run_mountainsort4(recording) ############################################################################# # The :code:`compare_two_sorters` function allows us to compare the spike # sorting output. It returns a :code:`SortingComparison` object, with methods # to inspect the comparison output easily. The comparison matches the # units by comparing the agreement between unit spike trains. # # Let’s see how to inspect and access this matching. cmp_KL_MS4 = sc.compare_two_sorters(sorting1=sorting_KL, sorting2=sorting_MS4, sorting1_name='klusta', sorting2_name='ms4') ############################################################################# # In order to check which units were matched, the :code:`get_mapped_sorting` # methods can be used. If units are not matched they are listed as -1. # units matched to klusta units mapped_sorting_klusta = cmp_KL_MS4.get_mapped_sorting1() print('Klusta units:', sorting_KL.get_unit_ids()) print('Klusta mapped units:', mapped_sorting_klusta.get_mapped_unit_ids()) # units matched to ms4 units mapped_sorting_ms4 = cmp_KL_MS4.get_mapped_sorting2() print('Mountainsort units:', sorting_MS4.get_unit_ids()) print('Mountainsort mapped units:', mapped_sorting_ms4.get_mapped_unit_ids())