Repository: mlflow/mlflow

Issue:
Code refactoring
### Summary

Make the following changes to simplify the code:

```diff
diff --git a/mlflow/system_metrics/metrics/cpu_monitor.py b/mlflow/system_metrics/metrics/cpu_monitor.py
index 67d6fb6d6..d1720e6ed 100644
--- a/mlflow/system_metrics/metrics/cpu_monitor.py
+++ b/mlflow/system_metrics/metrics/cpu_monitor.py
@@ -20,8 +20,4 @@ class CPUMonitor(BaseMetricsMonitor):
         )
 
     def aggregate_metrics(self):
-        metrics = {}
-        for name, values in self._metrics.items():
-            if len(values) > 0:
-                metrics[name] = round(sum(values) / len(values), 1)
-        return metrics
+        return {k: round(sum(v) / len(v), 1) for k, v in self._metrics.items() if v}
diff --git a/mlflow/system_metrics/metrics/disk_monitor.py b/mlflow/system_metrics/metrics/disk_monitor.py
index 2e2a885f1..f4f24b2e9 100644
--- a/mlflow/system_metrics/metrics/disk_monitor.py
+++ b/mlflow/system_metrics/metrics/disk_monitor.py
@@ -18,8 +18,4 @@ class DiskMonitor(BaseMetricsMonitor):
:...skipping...
diff --git a/mlflow/system_metrics/metrics/cpu_monitor.py b/mlflow/system_metrics/metrics/cpu_monitor.py
index 67d6fb6d6..d1720e6ed 100644
--- a/mlflow/system_metrics/metrics/cpu_monitor.py
+++ b/mlflow/system_metrics/metrics/cpu_monitor.py
@@ -20,8 +20,4 @@ class CPUMonitor(BaseMetricsMonitor):
         )
 
     def aggregate_metrics(self):
-        metrics = {}
-        for name, values in self._metrics.items():
-            if len(values) > 0:
-                metrics[name] = round(sum(values) / len(values), 1)
-        return metrics
+        return {k: round(sum(v) / len(v), 1) for k, v in self._metrics.items() if v}
diff --git a/mlflow/system_metrics/metrics/disk_monitor.py b/mlflow/system_metrics/metrics/disk_monitor.py
index 2e2a885f1..f4f24b2e9 100644
--- a/mlflow/system_metrics/metrics/disk_monitor.py
+++ b/mlflow/system_metrics/metrics/disk_monitor.py
@@ -18,8 +18,4 @@ class DiskMonitor(BaseMetricsMonitor):
         self._metrics["disk_available_megabytes"].append(disk_usage.free / 1e6)
 
     def aggregate_metrics(self):
-        metrics = {}
-        for name, values in self._metrics.items():
-            if len(values) > 0:
-                metrics[name] = round(sum(values) / len(values), 1)
-        return metrics
+        return {k: round(sum(v) / len(v), 1) for k, v in self._metrics.items() if v}
diff --git a/mlflow/system_metrics/metrics/gpu_monitor.py b/mlflow/system_metrics/metrics/gpu_monitor.py
index c35f1367c..3cf366aab 100644
--- a/mlflow/system_metrics/metrics/gpu_monitor.py
+++ b/mlflow/system_metrics/metrics/gpu_monitor.py
@@ -48,8 +48,4 @@ class GPUMonitor(BaseMetricsMonitor):
             self._metrics[f"gpu_{i}_utilization_percentage"].append(device_utilization.gpu)
 
     def aggregate_metrics(self):
-        metrics = {}
-        for name, values in self._metrics.items():
-            if len(values) > 0:
-                metrics[name] = round(sum(values) / len(values), 1)
-        return metrics
+        return {k: round(sum(v) / len(v), 1) for k, v in self._metrics.items() if v}
```

### Notes

- Make sure to open a PR from a **non-master** branch.
- Sign off the commit using the `-s` flag when making a commit:

  ```sh
  git commit -s -m "..."
           # ^^ make sure to use this
  ```

- Include `Resolve #{issue_number}` (e.g. `Resolve #123`) in the PR description when opening a PR.



Discussion:

Resolving patch:
diff --git a/mlflow/system_metrics/metrics/cpu_monitor.py b/mlflow/system_metrics/metrics/cpu_monitor.py
--- a/mlflow/system_metrics/metrics/cpu_monitor.py
+++ b/mlflow/system_metrics/metrics/cpu_monitor.py
@@ -20,8 +20,4 @@ def collect_metrics(self):
         )
 
     def aggregate_metrics(self):
-        metrics = {}
-        for name, values in self._metrics.items():
-            if len(values) > 0:
-                metrics[name] = round(sum(values) / len(values), 1)
-        return metrics
+        return {k: round(sum(v) / len(v), 1) for k, v in self._metrics.items()}
diff --git a/mlflow/system_metrics/metrics/disk_monitor.py b/mlflow/system_metrics/metrics/disk_monitor.py
--- a/mlflow/system_metrics/metrics/disk_monitor.py
+++ b/mlflow/system_metrics/metrics/disk_monitor.py
@@ -18,8 +18,4 @@ def collect_metrics(self):
         self._metrics["disk_available_megabytes"].append(disk_usage.free / 1e6)
 
     def aggregate_metrics(self):
-        metrics = {}
-        for name, values in self._metrics.items():
-            if len(values) > 0:
-                metrics[name] = round(sum(values) / len(values), 1)
-        return metrics
+        return {k: round(sum(v) / len(v), 1) for k, v in self._metrics.items()}
diff --git a/mlflow/system_metrics/metrics/gpu_monitor.py b/mlflow/system_metrics/metrics/gpu_monitor.py
--- a/mlflow/system_metrics/metrics/gpu_monitor.py
+++ b/mlflow/system_metrics/metrics/gpu_monitor.py
@@ -48,8 +48,4 @@ def collect_metrics(self):
             self._metrics[f"gpu_{i}_utilization_percentage"].append(device_utilization.gpu)
 
     def aggregate_metrics(self):
-        metrics = {}
-        for name, values in self._metrics.items():
-            if len(values) > 0:
-                metrics[name] = round(sum(values) / len(values), 1)
-        return metrics
+        return {k: round(sum(v) / len(v), 1) for k, v in self._metrics.items()}


Test patch: