"""Utility methods for clustering locdata."""from__future__importannotationsimportitertoolsimportsysfromcollections.abcimportCallablefromtypingimportAnyfromlocan.data.locdataimportLocData__all__:list[str]=["serial_clustering"]
[docs]defserial_clustering(locdata:LocData,algorithm:Callable[...,Any],parameter_lists:dict[str,Any],**kwargs:Any,)->tuple[LocData,...]:""" Run and analyse a series of clustering processes to identify optimal parameters. Parameters ---------- locdata Localization data. algorithm The locan clustering algorithm to use on locdata. parameter_lists A dictionary with all parameter lists that are to be iterated. The keys should be identical to parameter names of the used algorithm. kwargs Optional keyword arguments that are passed to the algorithm. Returns ------- tuple[LocData, ...] The first element is a LocData object with a selection of all localizations that are defined as noise. If noise is false this element will be None. The second element is a new LocData instance assembling all generated selections (i.e. localization cluster). """parameter=locals()keys=parameter_lists.keys()products=list(itertools.product(*parameter_lists.values()))products_transposed=zip(*products)dictionary={key:valuesforkey,valuesinzip(keys,products_transposed)}results=[]forproductinproducts:iterated_arguments={k:vfork,vinzip(keys,product)}results.append(algorithm(locdata,**iterated_arguments,**kwargs))noise_locdata=[result[0]forresultinresults]noise_collection=LocData.from_collection(noise_locdata)noise_collection.dataframe=noise_collection.dataframe.assign(**dictionary)# type: ignore[arg-type]collection_locdata=[result[1]forresultinresults]collection=LocData.from_collection(collection_locdata)collection.dataframe=collection.dataframe.assign(**dictionary)# type: ignore[arg-type]# metadata for noise_collectiondelnoise_collection.meta.history[:]noise_collection.meta.history.add(name=sys._getframe().f_code.co_name,parameter=str(parameter))# metadata for collectiondelcollection.meta.history[:]collection.meta.history.add(name=sys._getframe().f_code.co_name,parameter=str(parameter))returnnoise_collection,collection