Skip to content

Commit

Permalink
Merge pull request #63 from AndPuQing/master
Browse files Browse the repository at this point in the history
fix code style
  • Loading branch information
visualDust authored Nov 23, 2023
2 parents 3c09c16 + 82fc817 commit 83013d4
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 41 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[flake8]
max-line-length = 100
extend-ignore = E203
2 changes: 1 addition & 1 deletion neetbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from neetbox.daemon import _try_attach_daemon
from neetbox.utils.framing import get_frame_module_traceback

module = get_frame_module_traceback(1).__name__
module = get_frame_module_traceback(1).__name__ # type: ignore
config_file_name = f"{module}.toml"


Expand Down
70 changes: 70 additions & 0 deletions neetbox/daemon/_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import functools
import inspect
from ast import literal_eval
from typing import Callable, Optional

from neetbox.core import Registry
from neetbox.logging import logger
from neetbox.utils.mvc import Singleton


class PackedAction(Callable):
def __init__(self, function: Callable, name=None, **kwargs):
super().__init__(**kwargs)
self.function = function
self.name = name if name else function.__name__
self.argspec = inspect.getfullargspec(self.function)

def __call__(self, **argv):
self.function(argv)

def eval_call(self, params: dict):
eval_params = dict((k, literal_eval(v)) for k, v in params.items())
return self.function(**eval_params)


class _NeetAction(metaclass=Singleton):
__ACTION_POOL: Registry = Registry("__NEET_ACTIONS")

def register(
self,
*,
name: Optional[str] = None,
):
return functools.partial(self._register, name=name)

def _register(self, function: Callable, name: str = None):
packed = PackedAction(function=function, name=name)
_NeetAction.__ACTION_POOL._register(what=packed, name=packed.name, force=True)
return function

def get_actions(self):
action_names = _NeetAction.__ACTION_POOL.keys()
actions = {}
for n in action_names:
actions[n] = _NeetAction.__ACTION_POOL[n].argspec
return actions

def eval_call(self, name: str, params: dict):
if name not in _NeetAction.__ACTION_POOL:
logger.err(f"Could not find action with name {name}, action stopped.")
return False
return _NeetAction.__ACTION_POOL[name].eval_call(params)


# singleton
neet_action = _NeetAction()


# example
if __name__ == "__main__":

@neet_action.register(name="some")
def some(a, b):
print(a, b)

print("registered actions:")
print(neet_action.get_actions())

print("calling 'some")
neet_action.eval_call("some", {"a": "3", "b": "4"})
2 changes: 1 addition & 1 deletion neetbox/integrations/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ def get_installed_engines():
importlib.import_module(engine.value)
installed_engines.append(engine)
logger.info(f"'{engine.vaule}' was found installed.")
except:
except ImportError:
pass
return installed_engines.copy()
28 changes: 12 additions & 16 deletions neetbox/integrations/environment/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
# Date: 20230413


from neetbox.utils import pkg
from neetbox.utils.framing import get_frame_module_traceback

module_name = get_frame_module_traceback().__name__
assert pkg.is_installed(
"psutil", try_install_if_not=True
), f"{module_name} requires psutil which is not installed"
assert pkg.is_installed(
"GPUtil", try_install_if_not=True
), f"{module_name} requires GPUtil which is not installed"
import time
from threading import Thread

Expand All @@ -23,8 +13,18 @@
from GPUtil import GPU

from neetbox.pipeline import watch
from neetbox.utils import pkg
from neetbox.utils.framing import get_frame_module_traceback
from neetbox.utils.mvc import Singleton

module_name = get_frame_module_traceback().__name__ # type: ignore
assert pkg.is_installed(
"psutil", try_install_if_not=True
), f"{module_name} requires psutil which is not installed"
assert pkg.is_installed(
"GPUtil", try_install_if_not=True
), f"{module_name} requires GPUtil which is not installed"


class _CPU_STAT(dict):
def __init__(self, id=-1, percent=0.0, freq=0.0) -> None:
Expand Down Expand Up @@ -97,15 +97,11 @@ def watcher_fun(env_instance: _Hardware, do_update_gpus: bool):
freq=cpu_freq[index],
)
if do_update_gpus:
env_instance["gpus"] = [
_GPU_STAT.parse(_gpu) for _gpu in GPUtil.getGPUs()
]
env_instance["gpus"] = [_GPU_STAT.parse(_gpu) for _gpu in GPUtil.getGPUs()]
env_instance[""] = psutil.cpu_stats()
time.sleep(env_instance._update_interval)

self._watcher = Thread(
target=watcher_fun, args=(self, self._with_gpu), daemon=True
)
self._watcher = Thread(target=watcher_fun, args=(self, self._with_gpu), daemon=True)
self._watcher.start()


Expand Down
8 changes: 2 additions & 6 deletions neetbox/integrations/environment/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def __init__(self):
# system
self["username"] = getpass.getuser()
self["machine"] = platform.machine()
self["processor"] = (
"unknown" if len(platform.processor()) == 0 else platform.processor()
)
self["processor"] = "unknown" if len(platform.processor()) == 0 else platform.processor()
self["os_name"] = platform.system()
self["os_release"] = platform.version()
self["architecture"] = platform.architecture()
Expand All @@ -39,9 +37,7 @@ def exec(self, command):
str: The command running results.
err: The command error information.
"""
p = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
raw_output, raw_err = p.communicate()
rc = p.returncode
if self.platform_info["architecture"] == "32bit":
Expand Down
16 changes: 5 additions & 11 deletions neetbox/integrations/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
from neetbox.logging import logger
from neetbox.utils import pkg

_loader_pool: Dict[
str, "ResourceLoader"
] = dict() # all ResourceLoaders are stored here
_loader_pool: Dict[str, "ResourceLoader"] = dict() # all ResourceLoaders are stored here


class ResourceLoader:
Expand Down Expand Up @@ -104,9 +102,7 @@ def perform_scan():
glob_str = "**/*" if self._scan_sub_dirs else "*"
if not verbose: # do not output
self.file_path_list = [
str(path)
for path in pathlib.Path(self.path).glob(glob_str)
if can_match(path)
str(path) for path in pathlib.Path(self.path).glob(glob_str) if can_match(path)
]
else:
self.file_path_list = []
Expand Down Expand Up @@ -175,7 +171,7 @@ def get_random_image_as_numpy(self):
return np.array(image)

def get_random_image_as_tensor(self, engine=engine.Torch):
assert engine in [engine.Torch] # todo support other engines
assert engine in [engine.Torch] # TODO support other engines
if engine == engine.Torch:
assert pkg.is_installed("torchvision")
import torchvision.transforms as T
Expand All @@ -186,12 +182,10 @@ def get_random_image_as_tensor(self, engine=engine.Torch):
T.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)),
]
)
image = tensor_transform(self.get_random_image()).unsqueeze(
0
) # To tensor of NCHW
image = tensor_transform(self.get_random_image()).unsqueeze(0) # To tensor of NCHW
return image

# todo to_dataset
# TODO(VisualDust): to_dataset


def download(
Expand Down
12 changes: 6 additions & 6 deletions neetbox/pipeline/_signal_and_slot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import datetime
from functools import partial
from threading import Thread
from typing import Any, Callable
from typing import Any, Callable, Optional, Union

from neetbox.config import get_module_level_config
from neetbox.core import Registry
Expand Down Expand Up @@ -79,7 +79,7 @@ def _so_update_and_ping_listen(_name, _value, _watch_config):
return _the_value


def _watch(func: Callable, name: str, freq: float, initiative=False, force=False):
def _watch(func: Callable, name: Optional[str], freq: float, initiative=False, force=False):
"""Function decorator to let the daemon watch a value of the function
Args:
Expand Down Expand Up @@ -114,9 +114,9 @@ def watch(name=None, freq=None, initiative=False, force=False):
return partial(_watch, name=name, freq=freq, initiative=initiative, force=force)


def _listen(func: Callable, target: str, name: str = None, force=False):
def _listen(func: Callable, target: Union[str, Callable], name: Optional[str] = None, force=False):
name = name or func.__name__
if type(target) is not str:
if not isinstance(target, str):
if type(target) is partial:
if target.func in [__update_and_get, __get]:
target = target.args[0]
Expand All @@ -138,7 +138,7 @@ def _listen(func: Callable, target: str, name: str = None, force=False):
return func


def listen(target, name: str = None, force=False):
def listen(target, name: Optional[str] = None, force=False):
return partial(_listen, target=target, name=name, force=force)


Expand All @@ -151,7 +151,7 @@ def _update_thread():
for _vname, _watched_fun in _watch_queue_dict.items():
_watch_config = _watched_fun.others
if not _watch_config["initiative"] and _ctr % _watch_config["freq"] == 0: # do update
_the_value = __update_and_get(_vname)
_ = __update_and_get(_vname)


update_thread = Thread(target=_update_thread, daemon=True)
Expand Down

0 comments on commit 83013d4

Please sign in to comment.