From 057b13d7c5796272839b341c91bb818b8fa3d5b4 Mon Sep 17 00:00:00 2001
From: huongdm1896 <domaihuong1451997@gmail.com>
Date: Fri, 21 Feb 2025 02:55:54 +0100
Subject: [PATCH] removed hardcode dir to ./

---
 Flower_v1/server_1.py     | 13 +++++++++---
 README.md                 |  4 ++--
 Run/config_instances.json | 20 +++++++++---------
 Run/measure_instance.py   |  8 ++++++--
 Run/run_flwr.py           | 43 +++++++++++++++++++++++++++++++--------
 requirements.txt          |  7 ++++++-
 6 files changed, 68 insertions(+), 27 deletions(-)

diff --git a/Flower_v1/server_1.py b/Flower_v1/server_1.py
index 8bf37a0..a466496 100644
--- a/Flower_v1/server_1.py
+++ b/Flower_v1/server_1.py
@@ -18,9 +18,14 @@ def create_csv_filepath(strategy_name: str, num_rounds: int) -> str:
     # Determine the parent directory of the current script
     parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
-    # Create the "Log_autres" directory if it doesn't exist
-    log_dir = os.path.join(parent_dir, "Log_autres")
-    os.makedirs(log_dir, exist_ok=True)
+    # # Create the "Log_autres" directory if it doesn't exist
+    # log_dir = os.path.join(parent_dir, "Log_autres")
+    # os.makedirs(log_dir, exist_ok=True)
+    
+    log_dir = args.log_dir
+    if log_dir is None:
+        log_dir = os.path.join(parent_dir, "Log_autres")
+        os.makedirs(log_dir, exist_ok=True)
 
     # Generate a timestamp for the file
     timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
@@ -109,6 +114,8 @@ parser.add_argument(
     help="Select strategy: fedAvg, fedMedian, fedOpt or fedAdam or fedAvg2Clients",
 )
 
+parser.add_argument("-l", "--log_dir", type=str, default=None, help="Directory to save logs")
+
 # Parse the arguments from the command line
 args = parser.parse_args()
 
diff --git a/README.md b/README.md
index fa82163..76e7ab2 100644
--- a/README.md
+++ b/README.md
@@ -174,10 +174,10 @@ cd Run
 ```
 
 ### Step 2. Configure
-Create the JSON configuration file (e.g. `config_instances.json` for CPU and `config_instances_1.json` for GPU) to specify experiment details includes one or more instances.
+Two JSON configuration files (e.g. `config_instances.json` for CPU and `config_instances_1.json` for GPU) to specify experiment details includes one or more instances. 
 
 ```bash
-vim config_instances.json
+cat config_instances.json
 ```
 
 <u>For example</u>: `config_instances.json` provides two examples of instance configuration. All fields are configured except "`output_dir`" and "`args`" must be updated with your directories setting.
diff --git a/Run/config_instances.json b/Run/config_instances.json
index b48fb27..dc0e2bb 100644
--- a/Run/config_instances.json
+++ b/Run/config_instances.json
@@ -2,7 +2,7 @@
     "instances": {
         "1": {
             "instance": "fedAvg_cifar10",
-            "output_dir": "/home/mdo/Framework/eflwr/Log",
+            "output_dir": "./eflwr/Log",
             "dvfs_cpu": {
                 "dummy": true,
                 "baseline": false,
@@ -11,7 +11,7 @@
             "server": {
                 "command": "python3",
                 "args": [
-                    "/home/mdo/Framework/eflwr/Flower_v1/server_1.py",
+                    "./eflwr/Flower_v1/server_1.py",
                     "-r 1",
                     "-s fedAvg"
                 ],
@@ -23,7 +23,7 @@
                 "name": "client1",
                 "command": "python3",
                 "args": [
-                    "/home/mdo/Framework/eflwr/Flower_v1/client_1.py",
+                    "./eflwr/Flower_v1/client_1.py",
                     "cifar10",
                     "1",
                     "3"
@@ -34,7 +34,7 @@
                 "name": "client2",
                 "command": "python3",
                 "args": [
-                    "/home/mdo/Framework/eflwr/Flower_v1/client_1.py",
+                    "./eflwr/Flower_v1/client_1.py",
                     "cifar10",
                     "2",
                     "3"
@@ -45,7 +45,7 @@
                 "name": "client3",
                 "command": "python3",
                 "args": [
-                    "/home/mdo/Framework/eflwr/Flower_v1/client_1.py",
+                    "./eflwr/Flower_v1/client_1.py",
                     "cifar10",
                     "3",
                     "3"
@@ -56,7 +56,7 @@
         },
         "2": {
             "instance": "fedAvg2Clients_cifar10",
-            "output_dir": "/home/mdo/Framework/eflwr/Log",
+            "output_dir": "./eflwr/Log",
             "dvfs_cpu": {
                 "dummy": true,
                 "baseline": false,
@@ -65,7 +65,7 @@
             "server": {
                 "command": "python3",
                 "args": [
-                    "/home/mdo/Framework/eflwr/Flower_v1/server_1.py",
+                    "./eflwr/Flower_v1/server_1.py",
                     "-r 1",
                     "-s fedAvg2Clients"
                 ],
@@ -77,7 +77,7 @@
                 "name": "client1",
                 "command": "python3",
                 "args": [
-                    "/home/mdo/Framework/eflwr/Flower_v1/client_1.py",
+                    "./eflwr/Flower_v1/client_1.py",
                     "cifar10",
                     "1",
                     "3"
@@ -88,7 +88,7 @@
                 "name": "client2",
                 "command": "python3",
                 "args": [
-                    "/home/mdo/Framework/eflwr/Flower_v1/client_1.py",
+                    "./eflwr/Flower_v1/client_1.py",
                     "cifar10",
                     "2",
                     "3"
@@ -99,7 +99,7 @@
                 "name": "client3",
                 "command": "python3",
                 "args": [
-                    "/home/mdo/Framework/eflwr/Flower_v1/client_1.py",
+                    "./eflwr/Flower_v1/client_1.py",
                     "cifar10",
                     "3",
                     "3"
diff --git a/Run/measure_instance.py b/Run/measure_instance.py
index 9be54c1..a9df987 100644
--- a/Run/measure_instance.py
+++ b/Run/measure_instance.py
@@ -1,4 +1,4 @@
-# python3 measure.py -c config_instances.json -i 1 -x 1 -r 1
+# python3 measure_instance.py -c config_instances.json -i 1 -x 1 -r 1
 
 import os
 import argparse
@@ -19,6 +19,9 @@ parser.add_argument("-r", "--repeat", type=int, default=1, required=True, help="
 # Parse arguments
 args = parser.parse_args()
 
+config_path = os.path.abspath(args.config)
+config_dir = os.path.dirname(config_path)
+
 try:
     with open(args.config, "r") as file:
         config_data = json.load(file)
@@ -60,7 +63,8 @@ else:
     LEVERAGES = []
 
 # Log directory
-log_dir = config["output_dir"]
+# log_dir = config["output_dir"]
+log_dir = os.path.abspath(config["output_dir"])
 instance_name = config.get("instance", "default_instance")
 flower_log_dir = os.path.join(log_dir, f"Flower_{args.suffix}", f"Flower_instance_{instance_name}", "Expetator")
 os.makedirs(flower_log_dir, exist_ok=True)
diff --git a/Run/run_flwr.py b/Run/run_flwr.py
index cf9da47..f63f316 100644
--- a/Run/run_flwr.py
+++ b/Run/run_flwr.py
@@ -12,6 +12,9 @@ import argparse
 def main(config_path, instance, suffix):
     print(f"Running Flower instance with instance: {instance} and suffix: {suffix}")
 
+    config_path = os.path.abspath(config_path)
+    config_dir = os.path.dirname(config_path)
+    
     # read json
     try:
         with open(config_path, "r") as file:
@@ -30,20 +33,33 @@ def main(config_path, instance, suffix):
         sys.exit(1)
 
     # get server/client info
-    output_dir = instance_config["output_dir"]
+    output_dir = os.path.abspath(os.path.join(config_dir, instance_config["output_dir"])) if instance_config["output_dir"].startswith("./") else instance_config["output_dir"]
     instance_name = instance_config.get("instance", f"default_instance_{instance}")
     server_ip = instance_config["server"]["ip"]
     server_port = instance_config["server"]["port"]
-    server_command = [instance_config["server"]["command"], *instance_config["server"]["args"]]
 
-    clients = instance_config["clients"]
-    client_commands = [
-        {
-            "ip": client["ip"],
-            "command": [client["command"], *client["args"], f'"{server_ip}:{server_port}"']
-        } for client in clients
+    # Convert server args to absolute paths if needed
+    server_args = [
+        os.path.abspath(os.path.join(config_dir, arg)) if arg.startswith("./") else arg
+        for arg in instance_config["server"]["args"]
     ]
+    server_command = [instance_config["server"]["command"], *server_args]
+    
+    # Convert client args to absolute paths if needed
+    clients = instance_config["clients"]
+    client_commands = []
+    for client in clients:
+        client_ip = client["ip"]
+        client_args = [
+            os.path.abspath(os.path.join(config_dir, arg)) if arg.startswith("./") else arg
+            for arg in client["args"]
+        ]
+        client_command = [client["command"], *client_args, f'"{server_ip}:{server_port}"']
+        client_commands.append({"ip": client_ip, "command": client_command})
 
+    # print(server_command, client_commands)
+    # print(output_dir)
+    # return
     # Create log for experiment Flower
     current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
     flower_dir = os.path.join(output_dir, f"Flower_{suffix}")
@@ -55,11 +71,20 @@ def main(config_path, instance, suffix):
         print(f"========== Run Server on {server_ip} ==========")
         server_log_path = os.path.join(log_exp_dir, f"Server_{server_ip}")
         with open(server_log_path, "w") as log_file:
+            server_command_with_log = server_command.copy()  
+            if "logger" in instance_config["server"]["modules"]:  
+                server_command_with_log.extend(["--log_dir", log_exp_dir])  # Nếu có, thêm log_dir
+
             server_process = subprocess.Popen(
-                ["oarsh", server_ip, *server_command],
+                ["oarsh", server_ip, *server_command_with_log],  # Chạy lệnh server (có hoặc không có log_dir)
                 stdout=log_file, stderr=subprocess.STDOUT
             )
 
+            # server_process = subprocess.Popen(
+            #     ["oarsh", server_ip, *server_command],
+            #     stdout=log_file, stderr=subprocess.STDOUT
+            # )
+
         client_processes = []
         for client in client_commands:
             client_ip = client["ip"]
diff --git a/requirements.txt b/requirements.txt
index cbfa9c7..1ecfd4d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,8 @@
 flwr==1.13.0
 flwr-datasets==0.4.0
-expetator
\ No newline at end of file
+expetator
+tensorflow>=2.16.1,<2.17.0
+tensorflow-datasets==4.4.0
+tensorboard>=2.16.2,<2.17.0
+scikit-learn>=1.6.1
+numpy>=1.26.0,<1.27.0
\ No newline at end of file
-- 
GitLab