File size: 5,471 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
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
import json
import os
import numpy as np
from utils import fileIO,imgproc
import reports
import makereports
from pathlib import Path
import time
import re

coord_file = "config.json"
a_file = open(coord_file, "r")
config = json.load(a_file)


fov = '001'
z='02'

def download_azure():
    
    if config["isRemote"]=="N" or os.path.exists(os.path.join(config['results_path'],'data')):
        return None
    
    os.system(config["azure_command"].format(config["raw_data_path"],os.path.join(config['results_path'],'data')))
    
    return None

def check_params():

    # Check to see if there are any params that need to be found
    b_findz = len(config['z'])==0
    b_findfov = len(config['fov'])==0
    b_findir = len(config['ir'])==0
    b_findwv = len(config['channel'])==0


    # get all the images
    if config["isRemote"] == "Y":
        files = Path(os.path.join(config['results_path'],'data')).rglob('*.TIFF')
    else:
        files = Path(config['raw_data_path']).rglob('*.TIFF')
    files = list(map(str,files))
    # Find all channel, ir, fov, and z from the file names
    re_filter = r"(.*)(\d{3})(?=nm).*(\B\d{2})\D(\B\d{3})\D(\B\d{2}\b)"
    param_filter = re.compile(re_filter)
    results = list(map(param_filter.search,files))
    
    results =list(filter(lambda x: isinstance(x,re.Match), results))
    # Get the list of parameters if they are needed
    #   Group 0 is the full match, so each capture group is 1 indexed
    if b_findz:
        zs = list(map(lambda x: x.group(5),results))
    else:
        zs = config['z']

    if b_findir:
        irs = list(map(lambda x: x.group(3),results))
    else:
        irs = config['ir'] 

    if b_findfov:
        fovs = list(map(lambda x: x.group(4),results))
    else:
        fovs = config['fov']

    if b_findwv:
        wvs = list(map(lambda x: x.group(2),results))
    else:
        wvs = config['channel']

    if isinstance(results,list) and len(results)>0:
        full_raw_path = results[0].group(1)
    else:
        full_raw_path=''


    return sorted(list(set(zs)),key=int),sorted(list(set(fovs)),key=int),sorted(list(set(irs)),key=int),sorted(list(set(wvs)),key=int),full_raw_path


def check_dirs(files):
    """Checks to see if the directories for the files in the list exist. If they dont, then make those directories

    Args:
        files (list[str]): the list of files whose directory paths to create. Needs to be the full, not relative paths
        
    """
    if not type(files) == list:
        d = os.path.dirname(files)
        if not os.path.isdir(d):
            # print(files+'files' + d +'Does not exist')
            os.makedirs(d)
    else:
        for f in files:

            d = os.path.dirname(f)
            if d != "" and not os.path.isdir(d):
                # print(f+'files' + d +'Does not exist')
                os.makedirs(d)

download_azure()
zs,fovs,irs,wvs,full_raw_path = check_params()

def create_image_stack():

    out_file = os.path.join(config['results_path'],f'imgstack_{fov}_{z}.npy')
    coord_file = os.path.join(config['results_path'],f'coord_{fov}_{z}.json')
    check_dirs(out_file)

    fileIO.create_image_stack(os.path.join(full_raw_path,config['raw_image_format']),fov,z,irs,wvs,out_file,coord_file)

def create_deconvolved_images():
    in_file = os.path.join(config['results_path'],f'imgstack_{fov}_{z}.npy')

    out_file = os.path.join(config['results_path'],'deconvolved',f'deconvolved_{fov}_{z}.npy')
    check_dirs(out_file)
    imgproc._deconvolute(in_file,out_file)

def create_brightness_report():
    
    img_stack = os.path.join(config['results_path'],f'imgstack_{fov}_{z}.npy')
    coord_file = os.path.join(config['results_path'],f'coord_{fov}_{z}.json')
    
    out=os.path.join(config['results_path'],f'brightness_report_{fov}_{z}.pdf')
    check_dirs(out)
    
    makereports.brightness_worker(img_stack,coord_file,out,fov,z)

def create_focus_report():
    
    img_stack = [os.path.join(config['results_path'],f'imgstack_{fov}_{z}.npy')]
    coord_file = [os.path.join(config['results_path'],f'coord_{fov}_{z}.json')]
    
    out=os.path.join(config['results_path'],f'focus_report_{fov}.pdf')
    out_csv = os.path.join(config['results_path'],f'focus_report_{fov}.csv')
    check_dirs(out)
    makereports.focus_worker(img_stack,coord_file,out,out_csv,fov)


def compile_focus_reports():

    in_files = [os.path.join(config['results_path'],f'focus_report_{fov}.csv')]
    output = 'full_report_debug.csv'
    
    makereports.compile_focus_report(in_files,output=output,irs=irs,wvs=wvs)


if __name__=='__main__':
    start_time = time.time()

    sub_start_time = time.time()
    create_image_stack()
    sub_end_time = time.time()
    print(f'Image Stack: {sub_end_time-sub_start_time}')
    
    sub_start_time = time.time()
    create_deconvolved_images()
    sub_end_time = time.time()
    print(f'Deconvolved Stack: {sub_end_time-sub_start_time}')

    sub_start_time = time.time()
    create_brightness_report()
    sub_end_time = time.time()
    print(f'Brightness Report: {sub_end_time-sub_start_time}')

    sub_start_time = time.time()
    create_focus_report()
    sub_end_time = time.time()
    print(f'Focus Report: {sub_end_time-sub_start_time}')

    sub_start_time = time.time()
    compile_focus_reports()
    sub_end_time = time.time()
    print(f'Compiled Focus Report: {sub_end_time-sub_start_time}')    

    end_time = time.time()
    print(f'Total Time: {end_time-start_time}')