-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_analysis_workflow.nf
More file actions
128 lines (107 loc) · 4.68 KB
/
image_analysis_workflow.nf
File metadata and controls
128 lines (107 loc) · 4.68 KB
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
// Parameters
params.data_dir = "${projectDir}/../../data/"
params.intensity_threshold = 3.0 // Threshold for live/dead classification - examine the provided extract_features_and_labels.py script to understand how it works!
params.train_split = 0.7 // Proportion of the data for the training set
// Actual workflow
workflow {
// First, let's load the images...
// YOUR TURN! Replace the "..." below with your code!
// Hint: the files are in "${params.data_dir}/images/" and all have a name that ends with "_multichannel_image.tif"
images = Channel.fromPath("${params.data_dir}/images/*_multichannel_image.tif")
grouped_images = images.map { file ->
tuple(file.baseName.tokenize('_')[0], file)}
// ...and the masks
// YOUR TURN! Replace the "..." below with your code!
// Hint: the files are in "${params.data_dir}/masks/" and all have a name that ends with "_instances_mask.tif"
masks = Channel.fromPath("${params.data_dir}/masks/*_instances_mask.tif")
grouped_masks = masks.map { file ->
tuple(file.baseName.tokenize('_')[0], file)}
// Pair the images with their corresponding masks
// YOUR TURN! Replace the "..." below with your code!
// Hint: Use .join() to pair images and masks by their common well_id
paired_data = grouped_images.join(grouped_masks, by: [0])
// Extract features for each object in each image
(feature_files, label_files) = EXTRACT_FEATURES_AND_LABELS(paired_data)
// Combine features and labels from all images together into a data collection
// YOUR TURN! Replace the "..." below with your code!
// Hint: use .collect() to gather all feature/label files into a list, then call COMBINE_FEATURES_AND_LABELS and recover its outputs in a tuple
(combined_features, combined_labels) = COMBINE_FEATURES_AND_LABELS(feature_files, label_files)
// Train a decision tree to classify live from dead worms based on their features
// YOUR TURN! Replace the "..." below with your code!
// Hint: call TRAIN_CLASSIFIER and recover its outputs in a tuple
(confusion_matrix, decision_tree, classification_report, model_info) = TRAIN_CLASSIFIER(combined_features, combined_labels)
// Generate some performance readouts
// YOUR TURN! Replace the "..." below with your code!
// Hint: call CREATE_SUMMARY using the outputs from the previous steps
classification_summary = CREATE_SUMMARY(combined_features, combined_labels, model_info)
}
// Define the process that extracts the features for each object and its label
process EXTRACT_FEATURES_AND_LABELS {
publishDir "${params.data_dir}/features", mode: 'copy', pattern: '*.csv'
input:
tuple val(well_id), path(image), path(mask)
output:
// Define two separate CSV file outputs - one for features and one for labels
// YOUR TURN! Replace the "..." below with your code!
path("${well_id}_features.csv")
path("${well_id}_labels.csv")
script:
"""
# YOUR TURN! Replace the "..." below with your code!
# Use the provided extract_features_and_labels.py script
python ${projectDir}/extract_features_and_labels.py --well_id ${well_id} --image ${image} --mask ${mask} --threshold ${params.intensity_threshold}
"""
}
// Define the process that combines features from all objects into a single data structure
process COMBINE_FEATURES_AND_LABELS {
publishDir "${params.data_dir}", mode: 'copy'
input:
path(feature_files)
path(label_files)
output:
path("all_features.csv")
path("all_labels.csv")
script:
"""
python ${projectDir}/combine_features_and_labels.py \\
--output_features all_features.csv \\
--output_labels all_labels.csv
"""
}
// Define the process that trains a decision tree
process TRAIN_CLASSIFIER {
publishDir "${params.data_dir}/model", mode: 'copy'
input:
path(features)
path(labels)
output:
path("confusion_matrix.png")
path("decision_tree.png")
path("classification_report.txt")
path("model_info.json")
script:
"""
python ${projectDir}/train_classifier.py \\
--features ${features} \\
--labels ${labels} \\
--train_split ${params.train_split}
"""
}
// Process to create summary visualization
process CREATE_SUMMARY {
publishDir "${params.data_dir}", mode: 'copy'
input:
path(features)
path(labels)
path(model_info)
output:
path("classification_summary.png")
script:
"""
python ${projectDir}/create_classification_summary.py \\
--features ${features} \\
--labels ${labels} \\
--model_info ${model_info} \\
--threshold ${params.intensity_threshold}
"""
}