File size: 5,089 Bytes
5ad11ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
from reports.BrightnessReport import BrightnessReport
from reports.DecodabilityReport import DecodabilityReport
from reports.MaskedBrightnessReport import MaskedBrightnessReport
from reports.FocusReport import FocusReport
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

import pandas as pd


# Brightness report worker-------
def generate_brightness_reports(image_stack_file,coord_info,out_file,fov,z):
    #This was necessary to prototype on OSX...consider removing in the future after testing
    import multiprocessing
    job = multiprocessing.Process(target=brightness_worker,args=(image_stack_file,coord_info,out_file,fov,z))
    job.start()

    #brightness_worker(image_stack_file,coord_info,out_file,fov,fovs)

def brightness_worker(image_stack_file,coord_info,out_file,fov,z):
    br = BrightnessReport(image_stack_file,coord_info,fov,z)
    br.set_pdf(PdfPages(filename=out_file))
    
    br.preview_images()
    br.brightness_infov_z()
    br.brightness_on_images()
    br.contrast_heatmap()
    br.closePdf()

# Masked Brightness report worker-------
def generate_masked_brightness_reports(image_stack_file,coord_info,out_file,fov,z,mask_stack):
    #This was necessary to prototype on OSX...consider removing in the future after testing
    import multiprocessing
    job = multiprocessing.Process(target=masked_brightness_worker,args=(image_stack_file,mask_stack,coord_info,out_file,fov,z))
    job.start()

def masked_brightness_worker(image_stack_file,mask_stack,coord_info,out_file,fov,z):
    mbr = MaskedBrightnessReport(image_stack_file,mask_stack,coord_info,fov,z)
    mbr.set_pdf(PdfPages(filename=out_file))
    
    mbr.preview_images()
    mbr.brightness_infov_z()
    mbr.brightness_on_images()
    mbr.contrast_heatmap()
    mbr.closePdf()


# Decodability Report worker --------
def generate_decodability_reports(image_stack_file,coord_info,out_file,codebook_file,data_org_file,fov,z,out_detection_stats,):
    #This was necessary to prototype on OSX...consider removing in the future after testing
    import multiprocessing
    job = multiprocessing.Process(target=decodability_worker,args=(image_stack_file,coord_info,out_file,codebook_file,data_org_file,fov,z,out_detection_stats))
    job.start()

def decodability_worker(image_stack_file,coord_info,out_file,codebook_file,data_org_file,fov,z,out_detection_stats):
    dr = DecodabilityReport(image_stack_file,coord_info,codebook_file,data_org_file,fov,z,out_detection_stats)
    dr.set_pdf(PdfPages(filename=out_file))
    
    dr.make_bit_imgs()
    dr.make_decodability_hists()
    dr.closePdf()


# Focus report worker-------
def generate_focus_reports(image_stack_file,coord_info,out_file,out_csv,fov):
    #This was necessary to prototype on OSX...consider removing in the future after testing
    import multiprocessing
    job = multiprocessing.Process(target=focus_worker,args=(image_stack_file,coord_info,out_file,out_csv,fov))
    job.start()
    # focus_worker(image_stack_file,coord_info,out_file,out_csv,fov,fovs)


def focus_worker(image_stack_file,coord_info,out_file,out_csv,fov):
    fr = FocusReport(image_stack_file,coord_info,fov,out_csv)
    fr.set_pdf(PdfPages(filename=out_file))
    fr.f_measure_report()
    fr.closePdf()

# Compile reports----------------

def compile_focus_report(file_list:list,output,irs,wvs):
    
    full_df = pd.DataFrame()
    for fl in file_list:
        test = pd.read_csv(fl)
        full_df = full_df.append(test,ignore_index=True)
    
    full_df.to_csv(output)
    if not isinstance(irs,list):
        irs = list(irs)
    report_pdf = PdfPages(filename = output)
    

    compiled_matrix = np.zeros((
        len(file_list), # length of fovs
        len(wvs),
        len(irs)
    ))

    for iir, ir in enumerate(irs):#for each ir
        
        for iwv, wv in enumerate(wvs):#for each wv
            data = full_df[["FOV",str(wv)]][(full_df["IR"]==int(ir))] #extract the FOVs...assume stuff is ordered
            
            data = data.to_numpy() # FOV x 2

            compiled_matrix[:,iwv,iir] = data[:,1] #this is the z

            #ax[iir].plot("FOV",str(wv),data=data)
    #x:FOV y:z spy:IR
    f,ax = plt.subplots(nrows = len(wvs),ncols = 1,sharex=True,sharey=True,figsize=(8,len(irs)*4))    
    
    if not isinstance(ax,np.ndarray):
        ax = np.array(ax)
    
    for iwv, wv in enumerate(wvs):#for each wv
        ax[iwv].imshow(compiled_matrix[:,iwv,:])
        ax[iwv].set_xticks(np.arange(len(irs)), irs)
        ax[iwv].set_yticks(np.arange(len(file_list)), np.arange(len(file_list)))
        ax[iwv].set_ylabel("FOVS ")
        ax[iwv].set_xlabel("Imaging Round")
        plt.setp(ax[iwv].get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")

        for iir in range(len(irs)):
            for ifl in range(len(file_list)):
                text = ax[iwv].text(iir, ifl, int(compiled_matrix[ifl,iwv, iir]), ha="center", va="center", color="w")
        ax[iwv].set_title(f"Wavelength: {wv} nm")


    report_pdf.savefig()
    report_pdf.close()