| Conditions | 26 |
| Total Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like test_log_metrics() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #!/usr/bin/env python |
||
| 230 | def test_log_metrics(mongo_obs, sample_run, logged_metrics): |
||
| 231 | """ |
||
| 232 | Test storing scalar measurements |
||
| 233 | |||
| 234 | Test whether measurements logged using _run.metrics.log_scalar_metric |
||
| 235 | are being stored in the 'metrics' collection |
||
| 236 | and that the experiment 'info' dictionary contains a valid reference |
||
| 237 | to the metrics collection for each of the metric. |
||
| 238 | |||
| 239 | Metrics are identified by name (e.g.: 'training.loss') and by the |
||
| 240 | experiment run that produced them. Each metric contains a list of x values |
||
| 241 | (e.g. iteration step), y values (measured values) and timestamps of when |
||
| 242 | each of the measurements was taken. |
||
| 243 | """ |
||
| 244 | |||
| 245 | # Start the experiment |
||
| 246 | mongo_obs.started_event(**sample_run) |
||
| 247 | |||
| 248 | # Initialize the info dictionary and standard output with arbitrary values |
||
| 249 | info = {'my_info': [1, 2, 3], 'nr': 7} |
||
| 250 | outp = 'some output' |
||
| 251 | |||
| 252 | # Take first 6 measured events, group them by metric name |
||
| 253 | # and store the measured series to the 'metrics' collection |
||
| 254 | # and reference the newly created records in the 'info' dictionary. |
||
| 255 | mongo_obs.log_metrics(linearize_metrics(logged_metrics[:6]), info) |
||
| 256 | # Call standard heartbeat event (store the info dictionary to the database) |
||
| 257 | mongo_obs.heartbeat_event(info=info, captured_out=outp, beat_time=T1) |
||
| 258 | |||
| 259 | # There should be only one run stored |
||
| 260 | assert mongo_obs.runs.count() == 1 |
||
| 261 | db_run = mongo_obs.runs.find_one() |
||
| 262 | # ... and the info dictionary should contain a list of created metrics |
||
| 263 | assert "metrics" in db_run['info'] |
||
| 264 | assert type(db_run['info']["metrics"]) == list |
||
| 265 | |||
| 266 | # The metrics, stored in the metrics collection, |
||
| 267 | # should be two (training.loss and training.accuracy) |
||
| 268 | assert mongo_obs.metrics.count() == 2 |
||
| 269 | # Read the training.loss metric and make sure it references the correct run |
||
| 270 | # and that the run (in the info dictionary) references the correct metric record. |
||
| 271 | loss = mongo_obs.metrics.find_one({"name": "training.loss", "run_id": db_run['_id']}) |
||
| 272 | assert {"name": "training.loss", "id": str(loss["_id"])} in db_run['info']["metrics"] |
||
| 273 | assert loss["steps"] == [10, 20, 30] |
||
| 274 | assert loss["values"] == [1, 2, 3] |
||
| 275 | for i in range(len(loss["timestamps"]) - 1): |
||
| 276 | assert loss["timestamps"][i] <= loss["timestamps"][i + 1] |
||
| 277 | |||
| 278 | # Read the training.accuracy metric and check the references as with the training.loss above |
||
| 279 | accuracy = mongo_obs.metrics.find_one({"name": "training.accuracy", "run_id": db_run['_id']}) |
||
| 280 | assert {"name": "training.accuracy", "id": str(accuracy["_id"])} in db_run['info']["metrics"] |
||
| 281 | assert accuracy["steps"] == [10, 20, 30] |
||
| 282 | assert accuracy["values"] == [100, 200, 300] |
||
| 283 | |||
| 284 | # Now, process the remaining events |
||
| 285 | # The metrics shouldn't be overwritten, but appended instead. |
||
| 286 | mongo_obs.log_metrics(linearize_metrics(logged_metrics[6:]), info) |
||
| 287 | mongo_obs.heartbeat_event(info=info, captured_out=outp, beat_time=T2) |
||
| 288 | |||
| 289 | assert mongo_obs.runs.count() == 1 |
||
| 290 | db_run = mongo_obs.runs.find_one() |
||
| 291 | assert "metrics" in db_run['info'] |
||
| 292 | |||
| 293 | # The newly added metrics belong to the same run and have the same names, so the total number |
||
| 294 | # of metrics should not change. |
||
| 295 | assert mongo_obs.metrics.count() == 2 |
||
| 296 | loss = mongo_obs.metrics.find_one({"name": "training.loss", "run_id": db_run['_id']}) |
||
| 297 | assert {"name": "training.loss", "id": str(loss["_id"])} in db_run['info']["metrics"] |
||
| 298 | # ... but the values should be appended to the original list |
||
| 299 | assert loss["steps"] == [10, 20, 30, 40, 50, 60] |
||
| 300 | assert loss["values"] == [1, 2, 3, 10, 20, 30] |
||
| 301 | for i in range(len(loss["timestamps"]) - 1): |
||
| 302 | assert loss["timestamps"][i] <= loss["timestamps"][i + 1] |
||
| 303 | |||
| 304 | accuracy = mongo_obs.metrics.find_one({"name": "training.accuracy", "run_id": db_run['_id']}) |
||
| 305 | assert {"name": "training.accuracy", "id": str(accuracy["_id"])} in db_run['info']["metrics"] |
||
| 306 | assert accuracy["steps"] == [10, 20, 30] |
||
| 307 | assert accuracy["values"] == [100, 200, 300] |
||
| 308 | |||
| 309 | # Make sure that when starting a new experiment, new records in metrics are created |
||
| 310 | # instead of appending to the old ones. |
||
| 311 | sample_run["_id"] = "NEWID" |
||
| 312 | # Start the experiment |
||
| 313 | mongo_obs.started_event(**sample_run) |
||
| 314 | mongo_obs.log_metrics(linearize_metrics(logged_metrics[:4]), info) |
||
| 315 | mongo_obs.heartbeat_event(info=info, captured_out=outp, beat_time=T1) |
||
| 316 | # A new run has been created |
||
| 317 | assert mongo_obs.runs.count() == 2 |
||
| 318 | # Another 2 metrics have been created |
||
| 319 | assert mongo_obs.metrics.count() == 4 |