Package: analyzefrc
AnalyzeFRCDeveloped at the Department of Imaging Physics (ImPhys), Faculty of Applied Sciences, TU Delft.Plots, analysis and resolution measurement of microscopy images using Fourier Ring Correlation (FRC).AnalyzeFRC has native support for .lif files and can also easily read single images in formats supported by Pillow (PIL). Other formats require converting that image into a NumPy array and using that to instantiate AnalyzeFRC's native objects.AnalyzeFRC provides a lot of default options and convenience functions for a specific use case. However, its core functionality, themeasure_frcfunction inanalyzefrc.processcan be adapted in other workflows. You can also directly use thefrc library, on which this library is built.Defaults (please read)By default, when usingprocess_frc,preprocessis set to True. It ensures that each input image is cropped into square form and that a Tukey window is applied. Supplyproprocess=Falseto disable this behavior.By default, when usingprocess_frc,concurrencyis set to False. If set to true by passingconcurrency=True, it leverages thedecopackage to leverage more cores for a 1.5x+ speedup (not higher because the most resource-intensive computations are already parallelized). !! However, please run the program inside aif __name__ == '__main__':block when concurrency is enabled! Otherwise it will fail! Note: on some platforms, this type of concurrency can cause issues, notably Linux and macOS. This is a problem caused by a dependency.By default, if anFRCMeasurementis processed without any presetCurveTaskand has two images, it sets the method to2FRC. Otherwise,1FRCis used.By default, plots are grouped bymeasures, i.e. every measurement will be plotted separately. Use thegroup_<grouping>. Other available groupings includeall(all curves in one plot, use this only to retrieve them to use custom groupings),sets(all curves in the same set name in one plot) andcurves(one plot per curve).By default, 1FRC curves are computed 5 times and averaged, this can be overriden by passingoverride_nto process_frc.InstallationWith (Ana)condaIf you already have Anaconda installed (or miniconda), it is easiest to create a new Python 3.9 environment. Open the Anaconda/miniconda3 prompt and write (here 'envanalyze' can be any environment name you like):condacreate-n'envanalyze'python=3.9This package depends on a number of PyPI-only packages, some also with compiled extensions, which are difficult to port to conda. For this reason, it is recommended to have a seperate environment with only this package and then install using pip:condaactivateenvanalyze
pipinstallanalyzefrcYou now have an environment called 'envanalyze' with analyzefrc installed. Configure your favorite IDE to use the newly created environment and you're good to go!  See the usage examples for more details on how to use this package.Without condaCurrently, this library only works on Python 3.9. Ensure you have a working installation. You can use tools likepyenvfor managing Python versions.It is recommended to install this library into avirtual environment. Many tools exist for this today (most IDEs can do it for you), but I recommendPoetry.Install using:pipinstallanalyzefrcIf using Poetry:poetryaddanalyzefrcThis library indirectly (through thefrclibrary) depends onrustfrc(Rust extension) anddiplib(C++ extension). These compiled extensions can sometimes cause issues, so refer to their pages as well.UsageDefault .lif processingTo simply compute the 1FRC of all channels of a .lif dataset and plot the results, you can do the following:importanalyzefrcasafrc# This if-statement is required because concurrency is enabledif__name__=='__main__':# ./ means relative to the current folderfrc_sets=afrc.lif_read('./data/sted/2021_10_05_XSTED_NileRed_variation_excitation_power_MLampe.lif')plot_curves=afrc.process_frc("XSTED_NileRed",frc_sets,preprocess=True)afrc.plot_all(plot_curves)Plot series in one plotIf instead you want to plot each image inside a .lif file in a single plot, do the following:...# imports and processingplot_curves=afrc.process_frc("XSTED_NileRed",frc_sets,grouping='sets',preprocess=True,concurrency=False)afrc.plot_all(plot_curves)Change grouping after computationOr if you already computed the curves with the default grouping ('all'):...# imports and processingfrc_per_set_sets=afrc.group_sets(plot_curves)plot_all(frc_per_set_sets)Save instead of plotIf you don't want to plot the results (in the case of many images the IDE plot buffer can easily be exceeded), but instead save them:...# imports and processing# Will save to './results/<timestamp>-XSTED_NileRed'save_folder=afrc.create_save('./results','XSTED_NileRed',add_timestamp=True)afrc.plot_all(plot_curves,show=False,save=True,save_directory=save_folder,dpi=180)Only extract data, don't plotPlotting using your own tools can also be desired. To extract only the resulting data, do not callplot_all. Instead, use the result ofprocess_frc, which yields a dictionary of lists ofCurve-objects. ACurve-object is simply a data container for NumPy arrays and metadata. An example:...# imports and data readingfromanalyzefrcimportCurveimportmatplotlib.pyplotaspltplot_curves:dict[str,list[Curve]]=afrc.process_frc("XSTED_NileRed",frc_sets,grouping='sets',preprocess=True)# plot all on your ownforcurvesinplot_curves.values():first_curve:Curve=curves[0]plt.plot(first_curve.curve_x,first_curve.curve_y)plt.plot(first_curve.curve_x,first_curve.thres)plt.show()Example: 1FRC vs 2FRC from .tiffA slightly more complex example: If you have a sample .tiff file and you want to compare the performance of 1FRC vs 2FRC, you could do the following:importnumpyasnpimportdiplibasdipimportfrc.utilityasfrcuimportanalyzefrcasafrcfromanalyzefrcimportFRCMeasurement,FRCSetdata_array:np.ndarray=afrc.get_image('./data/siemens.tiff')# Blur the image (to create a frequency band)data_array=frcu.gaussf(data_array,30)data_dip=dip.Image(data_array)half_set_1=np.array(dip.PoissonNoise(data_dip/2))half_set_2=np.array(dip.PoissonNoise(data_dip/2))full_set=np.array(dip.PoissonNoise(data_dip))# Create seperate measurement objectsfrc_2:FRCMeasurement=afrc.frc_measure(half_set_1,half_set_2,set_name='2FRC')frc_1:FRCMeasurement=afrc.frc_measure(full_set,set_name='1FRC')# Combine in one set so they can be plot togetherfrc_set:FRCSet=afrc.frc_set(frc_1,frc_2,name='2FRC vs 1FRC')plot_curve=afrc.process_frc("2FRC vs 1FRC",frc_set,concurrency=False)afrc.plot_all(plot_curve)DetailsThe three operations of setting up the measurements, computing the curves and plotting them are all decoupled and each have their Python module (analyzefrc.read,analyzefrc.process,analyzefrc.plot, respectively). Furthermore, actual file reading convenience functions can be found inanalyzefrc.file_read.FRCSet, FRCMeasurement and FRCMeasureSettingsFor setting up the measurements in preparation of processing, these three classes are essential.FRCSet-objects can be completely unrelated, they share no information. As such, if doing batch processing of different datasets, they can be divided overFRCSet-objects.
Within anFRCSet, there can be an arbitrary number ofFRCMeasurement-objects, which should have similar image dimensions and should, in theory, be able to be sensibly plotted in a single figure.FRCMeasurementis the main data container class. It can be instantiated using anFRCMeasureSettings-object, which contains important parameters that are the same across all images within the measurement (such as the objective's NA value). If these differ across the images, multiple measurements should be used.Changing default curvesBy default, when processing, a singleCurveTaskwill be generated for eachFRCMeasurement, meaning a single curve will be generated for each measurement. However, if a different threshold (other than the 1/7) is desired, or multiple curves per figure are wanted, aCurveTaskcan be created beforehand and given to theFRCMeasurement.Example:...# see .tiff examplefromanalyzefrcimportCurveTask# Create seperate measurement objects# For example we want a smoothed curve for the 1FRC, as well as a non-smoothed curvefrc1_task_smooth=CurveTask(key='smooth_curve',smooth=True,avg_n=3,threshold='half_bit')frc1_task=CurveTask(key='standard_curve',avg_n=3,threshold='half_bit')frc_2:FRCMeasurement=afrc.frc_measure(half_set_1,half_set_2,set_name='2FRC')frc_1:FRCMeasurement=afrc.frc_measure(full_set,set_name='1FRC',curve_tasks=[frc1_task,frc1_task_smooth])...# process and plotChanging default processingIf other measurement-based processings are desired, they can be added in two ways. Arbitrary functions (of the typeMeasureProcessing = Callable[[FRCMeasurement], FRCMeasurement]) can be run for each measurement by passing them as a list to theextra_processings-argument forprocess_frc, or by populating theFRCMeasurement-objects'extra_processingsattribute.Note: each processing is performed in list order after the optionalpreprocessingstep, with global extras performed before the measurement-defined extra processing tasks.This can be useful when using a convenience file loading function. For example, to flip every image and apply a different window functon:...# .lif examplefromanalyzefrcimportFRCMeasurementimportnumpyasnpfromscipy.signalimportwindowsaswinsdefflip_window_data(measure:FRCMeasurement)->FRCMeasurement:measure.image=np.flip(measure.image)size=measure.image.shape[0]assertsize==measure.image.shape[1]cosine=wins.tukey(size)cosine_square=np.ones((size,size))*cosine.reshape((size,1))*cosine.reshape((1,size))measure.image=cosine_square*measure.imagereturnmeasureplot_curves=afrc.process_frc("XSTED_NileRed",frc_sets,preprocess=False,extra_processings=[flip_window_data],concurrency=False)...# plotOther internal detailsThe general processing flow is as follows:(read/read_file) CreateFRCMeasureSettingsbased on data acquisition parameters(read/read_file) CreateFRCMeasurementusing the previous step.(Optionally) create customCurveTask-objects for theFRCMeasurement. Created by default in theprocessstep if not provided.(read/read_file) CreateFRCSetusing multipleFRCMeasurement-objects.(process) ComputeCurve-objects usingmeasure_frc.(process) Sort/group theCurve-objects into a dictionary with lists ofCurve-objects as entries.(plot) Plot thelist[Curve]-dictionary, where each entry becomes a single figure.All steps besides themeasure_frc-step can be implemented in a custom way quite trivially. In a way, all steps except step 5 are for your convenience. Step 5, which is the only step that involves actually processing all the data using thefrclibrary, forms the core of this package.PerformanceProcessing 32 measurements of 1024x1024 pixels takes about thirty seconds to read from a .lif file, process (computing each curve 5 times) and plot on my i7-8750H laptop CPU (which is decently performant even by today's standards).Over 80% of the time is spent processing, i.e. performing the binomial splitting and computing the FRCs (with the latter taking significantly longer). All these functions are implemented through Rust (rustfrc), C++ (diplib) or C (numpy) extensions, meaning they are as fast as can be and mostly parallelized.10-15% of the time is spent plotting using matplotlib, meaning the overhead of this library is only 5-10%.