MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / test_loss_dict

Method test_loss_dict

tests/handlers/test_handler_stats.py:123–153  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

121 self.assertEqual(content_count, 2) # 2 = len([1, 3]) from event_filter
122
123 def test_loss_dict(self):
124 log_stream = StringIO()
125 log_handler = logging.StreamHandler(log_stream)
126 log_handler.setLevel(logging.INFO)
127 key_to_handler = "test_logging"
128 key_to_print = "myLoss1"
129
130 # set up engine
131 def _train_func(engine, batch):
132 return [torch.tensor(0.0)]
133
134 engine = Engine(_train_func)
135
136 # set up testing handler
137 logger = logging.getLogger(key_to_handler)
138 logger.setLevel(logging.INFO)
139 logger.addHandler(log_handler)
140 stats_handler = StatsHandler(name=key_to_handler, output_transform=lambda x: {key_to_print: x[0]})
141 stats_handler.attach(engine)
142
143 engine.run(range(3), max_epochs=2)
144
145 # check logging output
146 output_str = log_stream.getvalue()
147 log_handler.close()
148 has_key_word = re.compile(f".*{key_to_print}.*")
149 content_count = 0
150 for line in output_str.split("\n"):
151 if has_key_word.match(line):
152 content_count += 1
153 self.assertGreater(content_count, 0)
154
155 def test_loss_file(self):
156 key_to_handler = "test_logging"

Callers

nothing calls this directly

Calls 5

attachMethod · 0.95
StatsHandlerClass · 0.90
splitMethod · 0.80
runMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected