-
Notifications
You must be signed in to change notification settings - Fork 0
/
findyourcell_rolling.jl
executable file
·88 lines (85 loc) · 3.17 KB
/
findyourcell_rolling.jl
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
using FileIO
using TiffImages
using MAT
using Dates
#using ProgressMeter
include("findyourcell.jl")
"""
Rolling cell segmentation during capturing
v0.1.0 hf 04202021
"""
DATA_DIR = "../0417-22-processed-stack-noRball/2021041720-preprocess"
RET_DIR = "../0417-22-processed-stack-noRball/result"
stack_list= filter(x->occursin(r"G-stack.tiff$", x), readdir(DATA_DIR))
unfinished = true
while unfinished
for idx in 1:length(stack_list)
raw_name = stack_list[idx]
timeout = false
println(raw_name)
while( !timeout )
if time() - stat("$DATA_DIR/$raw_name").mtime > 120 # be careful
img = try
load("$DATA_DIR/$raw_name")#, mmap=true)
#TiffImages.load("$DATA_DIR/$raw_name")#, mmap=true)
catch e
@warn "Failed to load $raw_name, skip to next stack"
break #TODO: or wait util tiff is free to read
end
mask_name = "$RET_DIR/$(raw_name[1:end-4])_mask.tiff"
info_name = "$RET_DIR/$(raw_name[1:end-4])_info.mat"
raw_frame_num = size(img, 3)
if isfile(info_name) #TODO: && time() - stat(info_name).mtime > 400
prev_info = matread(info_name)["info"]
prev_frame_num = length(prev_info)
append_num = raw_frame_num - prev_frame_num
# only process the new coming frames
if append_num > 0
println("Appending $raw_name:$(prev_frame_num+1) $raw_frame_num, $(Dates.now())")
prev_mask = try
reinterpret(UInt16, TiffImages.load(mask_name))
catch
@warn "Failed to load mask $mask_name, skip to next stack"
break
end
mask_patch = Array{UInt16}(undef, size(img, 1), size(img,2), append_num)
new_mask = cat(prev_mask, mask_patch, dims=3)
info_patch = Array{Any}(undef, append_num)
new_info = cat(prev_info, info_patch, dims=1)
@time Threads.@threads for t in prev_frame_num+1:raw_frame_num
print("*")
new_mask[:, :, t], new_info[t] = find_your_cell(img[:, :, t])
end
else
break
end
else # start from 0
println("Processing $raw_name: 1:$raw_frame_num, $(Dates.now())")
new_mask = Array{UInt16}(undef, size(img))
new_info = Array{Any}(undef, raw_frame_num)
slices_num = Threads.nthreads() - 2
@time for t_start in 1:slices_num:raw_frame_num
t_end = t_start + slices_num - 1
if t_end > raw_frame_num
t_end = raw_frame_num
end
Threads.@threads for t in t_start:t_end
print("*")
new_mask[:, :, t], new_info[t] = find_your_cell(img[:, :, t])
end
end
end
println("Writing")
save(mask_name, new_mask)
#TiffImages.save(mask_name, reinterpret(Gray{N0f16}, new_mask))
matwrite(info_name, Dict("info"=>new_info))
println("Done! $(Dates.now())")
GC.gc()
timeout = true
end
print("~")
sleep(2)
end
end
sleep(1)
end