Skip to content

Commit fe70603

Browse files
committed
Added the stopWithError() method to Streamer class
1 parent 16e1b1a commit fe70603

File tree

6 files changed

+54
-12
lines changed

6 files changed

+54
-12
lines changed

source/FAST/Algorithms/ImagePatch/PatchGenerator.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,7 @@ void PatchGenerator::generateStream() {
310310
if(!previousPatch) {
311311
// No patches have been created
312312
reportInfo() << "No patches were generated." << reportEnd();
313-
frameAdded(); // Unlock blocking execute()
314-
for(const auto& channel : mOutputConnections[0]) {
315-
channel.lock()->stop("No patches were created by the PatchGenerator");
316-
}
313+
stopWithError("No patches were created by the PatchGenerator");
317314
} else {
318315
previousPatch->setLastFrame(getNameOfClass());
319316
previousPatch->setFrameData("streaming", "yes"); // Since we are not propagating frame data, we have to set this
@@ -326,12 +323,7 @@ void PatchGenerator::generateStream() {
326323
}
327324
} catch(std::exception &e) {
328325
// Exception happened in thread. Stop pipeline, and propagate error message.
329-
for(auto item : mOutputConnections) {
330-
for(auto output : item.second) {
331-
output.lock()->stop(e.what());
332-
}
333-
}
334-
frameAdded(); // To unlock if happens before first frame
326+
stopWithError(e.what());
335327
}
336328
}
337329

source/FAST/Algorithms/ImagePatch/Tests.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ TEST_CASE("Patch generator with no patches", "[fast][PatchGenerator]") {
8282
REQUIRE(counter == 0);
8383
}
8484

85+
86+
TEST_CASE("Patch generator throws exception in thread", "[fast][PatchGenerator]") {
87+
auto importer = WholeSlideImageImporter::create(Config::getTestDataPath() + "/WSI/CMU-1.svs");
88+
auto wsi = importer->runAndGetOutputData<ImagePyramid>();
89+
90+
auto segmentation = Image::create(512, 512, TYPE_UINT8, 1);
91+
segmentation->fill(0); // Empty segmentation will mean no patches
92+
const int width = 256;
93+
const int height = 256;
94+
// Level 4 doesn't exist in this image pyramid, thus it will trigger an exception
95+
auto generator = PatchGenerator::create(width, height, 1, 4)
96+
->connect(importer)
97+
->connect(1, segmentation);
98+
auto stream = DataStream(generator);
99+
int counter = 0;
100+
CHECK_THROWS(
101+
while(!stream.isDone()) {
102+
auto image = stream.getNextFrame<Image>();
103+
++counter;
104+
}
105+
);
106+
REQUIRE(counter == 0);
107+
}
108+
85109
TEST_CASE("Patch generator on 3D image", "[fast][PatchGenerator]") {
86110
auto importer = ImageFileImporter::create(Config::getTestDataPath() + "/CT/CT-Thorax.mhd");
87111
auto image = importer->runAndGetOutputData<Image>();

source/FAST/Python/Tests/test_data_stream_object.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55

6-
def test_data_stream_empty():
6+
def test_data_stream_empty_generator():
77
importer = fast.WholeSlideImageImporter.create(fast.Config.getTestDataPath() + '/WSI/CMU-1.svs')
88

99
segmentation = fast.TissueSegmentation.create(False).connect(importer).runAndGetOutputData()
@@ -14,7 +14,19 @@ def test_data_stream_empty():
1414
with pytest.raises(RuntimeError):
1515
for image in fast.DataStream(generator):
1616
counter += 1
17-
assert(counter == 0)
17+
assert counter == 0
18+
19+
20+
def test_data_stream_empty_file_streamer():
21+
streamer = fast.ImageFileStreamer \
22+
.create(fast.Config.getTestDataPath() + "/US/Heart/ApicalFourChamber/US-2D_#.mhd",)
23+
streamer.setStartNumber(200) # Incorrect start number, will result in no frames
24+
25+
counter = 0
26+
with pytest.raises(RuntimeError):
27+
for image in fast.DataStream(streamer):
28+
counter += 1
29+
assert counter == 0
1830

1931

2032
def test_data_stream_single():

source/FAST/Streamers/FileStreamer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ void FileStreamer::setFilenameFormats(std::vector<std::string> strs) {
100100
}
101101

102102
void FileStreamer::generateStream() {
103+
if(getNrOfFrames() == 0) {
104+
stopWithError("No frames were created by the FileStreamer");
105+
}
103106

104107
// Read timestamp file if available
105108
std::ifstream timestampFile;

source/FAST/Streamers/Streamer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,13 @@ void Streamer::stopPipeline() {
101101
ProcessObject::stopPipeline();
102102
}
103103

104+
void Streamer::stopWithError(std::string message, int outputPort) {
105+
frameAdded(); // Unlock blocking execute()
106+
if(outputPort < 0)
107+
outputPort = 0;
108+
for(const auto& channel : mOutputConnections[outputPort]) {
109+
channel.lock()->stop();
110+
}
111+
}
112+
104113
}

source/FAST/Streamers/Streamer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class FAST_EXPORT Streamer : public ProcessObject {
4848
*/
4949
virtual void stop();
5050

51+
virtual void stopWithError(std::string message, int outputPort = -1);
52+
5153
virtual bool isStopped();
5254

5355
virtual void setMaximumNrOfFrames(int maximumNrOfFrames);

0 commit comments

Comments
 (0)