code: def test_bmuf_correct_metrics_averaging(tmpdir, device_id, mb_source):
      if platform.system() == "Linux":
          pytest.skip(
              "test only runs on Windows due to mpiexec -l option"
          )
  
      # check whether trainer can be initialized or not
      bmuf = SimpleBMUFTrainer()
      if not bmuf.trainer:
          pytest.skip("BMUF not available on this build")
  
      launch_args = []
      if device_id >= 0:
          launch_args += ["--gpu"]
  
      launch_args += ["--outputdir", str(tmpdir)]
      launch_args += ["--mb_source", mb_source]
  
      ret_str = mpiexec_execute(
          __file__, ["-n", str(NUM_WORKERS), "-l"], launch_args
      )
      # print(ret_str)
  
      # [0]Finished Epoch[1]: [Training] loss = 1.663636 * 10, metric = 52.40% * 10 0.890s ( 11.2 samples/s);
      regex_pattern = r"\[(?P<worker_rank>\d)\].*? Epoch\[(?P<epoch>\d+)\].*? loss = (?P<loss>\d+\.\d+) .*? metric = (?P<metric>\d+\.\d+)"
      loss_perepoch_perworker = {i: {} for i in range(NUM_WORKERS)}
      for match in re.finditer(regex_pattern, ret_str):
          rank = int(match.groupdict()["worker_rank"])
          epoch = int(match.groupdict()["epoch"])
          loss = match.groupdict()["loss"]
          metric = match.groupdict()["metric"]
          loss_perepoch_perworker[rank].update({epoch: (loss, metric)})
  
      num_epochs_per_worker = list(
          map(len, loss_perepoch_perworker.values())
      )
  
      # assert that data exists
      assert len(num_epochs_per_worker) != 0
  
      # assert that number of epochs isn't zero for 1st worker.
      assert num_epochs_per_worker[0] != 0
  
      # assert all workers have same number of epochs
      assert min(num_epochs_per_worker) == max(num_epochs_per_worker)
  
      # assert all workers have same loss and metric values
      loss_per_worker = loss_perepoch_perworker.values()
      loss_per_worker_epochsort = []
      for epoch_losses in loss_per_worker:
          loss_per_worker_epochsort.append(
              [epoch_losses[i] for i in sorted(epoch_losses)]
          )
  
      assert all(
          [
              loss_per_worker_epochsort[0] == i
              for i in loss_per_worker_epochsort
          ]
      )
  
file_path: CNTK/bindings/python/cntk/learners/tests/bmuf_metrics_aggregation_test.py
imports: import pytest
  import cntk
  import numpy
  import sys, os
  from distributed_learner_test import mpiexec_execute
  import argparse
  import re
  import platform
label_desc: Use image node_id 3 for calling a global function with example usage: test_bmuf_correct_metrics_averaging(tmpdir, device_id, mb_source) without return types