Skip to content
Snippets Groups Projects
Commit 1d45bfff authored by lukas leufen's avatar lukas leufen
Browse files

new: prepare host, set experiment name, argparser for exp date

parent acff6865
Branches
No related tags found
2 merge requests!12Experiment Setup finished,!11finish run script creation, /close #11
Pipeline #25874 passed
...@@ -9,3 +9,4 @@ pytest-lazy-fixture==0.6.1 ...@@ -9,3 +9,4 @@ pytest-lazy-fixture==0.6.1
pytest-cov pytest-cov
pytest-html pytest-html
pydot pydot
mock
...@@ -3,7 +3,25 @@ __date__ = '2019-11-14' ...@@ -3,7 +3,25 @@ __date__ = '2019-11-14'
import logging import logging
from src.helpers import TimeTracking
from src import helpers
import argparse
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('experiment date', metavar='exp_date', type=str, nargs=1, help='set experiment date as string',
default=None)
logging.info("start run script") logging.info("start run script")
total_time = TimeTracking()
# set data path of this experiment
data_path = helpers.prepare_host()
# set experiment name
experiment_date = parser.parse_args(["experiment_date"])
experiment_name, experiment_path = helpers.set_experiment_name(experiment_date=experiment_date)
# set if model shall be trained or not
\ No newline at end of file
...@@ -10,6 +10,8 @@ from typing import Union ...@@ -10,6 +10,8 @@ from typing import Union
import numpy as np import numpy as np
import os import os
import time import time
import socket
import sys
def to_list(arg): def to_list(arg):
...@@ -123,3 +125,40 @@ class TimeTracking(object): ...@@ -123,3 +125,40 @@ class TimeTracking(object):
def duration(self): def duration(self):
return self._duration() return self._duration()
def prepare_host():
hostname = socket.gethostname()
user = os.getlogin()
if hostname == 'ZAM144': # pragma: no branch
path = f'/home/{user}/Data/toar_daily/'
elif hostname == 'zam347':
path = f'/home/{user}/Data/toar_daily/'
elif hostname == 'linux-gzsx':
path = f'/home/{user}/machinelearningtools'
elif (len(hostname) > 2) and (hostname[:2] == 'jr'):
path = f'/p/project/cjjsc42/{user}/DATA/toar_daily/'
elif (len(hostname) > 2) and (hostname[:2] == 'jw'):
path = f'/p/home/jusers/{user}/juwels/intelliaq/DATA/toar_daily/'
else:
logging.error(f"unknown host '{hostname}'")
raise OSError(f"unknown host '{hostname}'")
if not os.path.exists(path):
logging.error(f"path '{path}' does not exist for host '{hostname}'.")
raise NotADirectoryError(f"path '{path}' does not exist for host '{hostname}'.")
else:
logging.info(f"set path to: {path}")
return path
def set_experiment_name(experiment_date=None, experiment_path=None):
if experiment_date is None:
experiment_name = ""
else:
experiment_name = f"{experiment_date}_network/"
if experiment_path is None:
experiment_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", experiment_name))
else:
experiment_path = os.path.abspath(experiment_path)
return experiment_name, experiment_path
...@@ -4,6 +4,7 @@ import logging ...@@ -4,6 +4,7 @@ import logging
import os import os
import keras import keras
import numpy as np import numpy as np
import mock
class TestToList: class TestToList:
...@@ -135,3 +136,39 @@ class TestTimeTracking: ...@@ -135,3 +136,39 @@ class TestTimeTracking:
assert duration is not None assert duration is not None
duration = t.stop() duration = t.stop()
assert duration == t.duration() assert duration == t.duration()
class TestPrepareHost:
@mock.patch("socket.gethostname", return_value="linux-gzsx")
@mock.patch("os.getlogin", return_value="testUser")
@mock.patch("os.path.exists", return_value=True)
def test_prepare_host(self, mock_host, mock_user, mock_path):
path = prepare_host()
assert path == "/home/testUser/machinelearningtools"
@mock.patch("socket.gethostname", return_value="NotExistingHostName")
@mock.patch("os.getlogin", return_value="zombie21")
def test_error_handling(self, mock_user, mock_host):
with pytest.raises(OSError) as e:
prepare_host()
assert "unknown host 'NotExistingHostName'" in e.value.args[0]
mock_host.return_value = "linux-gzsx"
with pytest.raises(NotADirectoryError) as e:
prepare_host()
assert "path '/home/zombie21/machinelearningtools' does not exist for host 'linux-gzsx'" in e.value.args[0]
class TestSetExperimentName:
def test_set_experiment(self):
exp_name, exp_path = set_experiment_name()
assert exp_name == ""
assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ""))
exp_name, exp_path = set_experiment_name(experiment_date="2019-11-14", experiment_path="./test2")
assert exp_name == "2019-11-14_network/"
assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "test2"))
def test_set_experiment_from_sys(self):
exp_name, _ = set_experiment_name(experiment_date="2019-11-14")
assert exp_name == "2019-11-14_network/"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment