From e35fd8f9f805988a30f8bb80fd74d957a95d2f15 Mon Sep 17 00:00:00 2001 From: visualdust Date: Sat, 18 Nov 2023 06:29:25 +0000 Subject: [PATCH 1/5] added @logger.mention doc --- doc/docs/guide/logging/index.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/docs/guide/logging/index.md b/doc/docs/guide/logging/index.md index 9343eea..e62cb55 100644 --- a/doc/docs/guide/logging/index.md +++ b/doc/docs/guide/logging/index.md @@ -23,6 +23,37 @@ output: 2023-03-18-13:56:03 > test.py > hello world ``` +## Using a decorator + +`@logger.mention` will mention the decorated function on each call. + +```python +@logger.mention +def function_a(): + print('message from a') + +def function_b(): + function_a() + print('message from b') + +def function_c(): + function_a() + function_b() + print('message from c') + +function_c() +``` +output: +```html +2023-11-18-06:26:04 > main/function_c > Currently running: function_a +message from a +2023-11-18-06:26:04 > main/function_b > Currently running: function_a +message from a +message from b +message from c +``` + + ## Auto tracing The default `logger` automatically trace back the caller of logger instance. From ce59ad64c3daf45c083474c7af9627bf041bdd51 Mon Sep 17 00:00:00 2001 From: visualdust Date: Sun, 19 Nov 2023 06:35:53 +0000 Subject: [PATCH 2/5] turned LogStyle into dataclass --- neetbox/logging/formatting.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/neetbox/logging/formatting.py b/neetbox/logging/formatting.py index cb0a54f..d01e25e 100644 --- a/neetbox/logging/formatting.py +++ b/neetbox/logging/formatting.py @@ -4,13 +4,25 @@ # URL: https://gong.host # Date: 20230318 +from dataclasses import dataclass import os import warnings from random import random from typing import Optional - +@dataclass class LogStyle: + color: Optional[str] = None + prefix: str = "" + text_style: Optional[str] = None + datetime_format: str = "%Y-%m-%d-%H:%M:%S" + with_identifier: bool = True + trace_level = 3 + with_datetime: bool = True + split_char_cmd = " > " + split_char_identity = "/" + split_char_txt = " | " + @classmethod def get_supported_colors(cls): return ["red", "green", "blue", "cyan", "yellow", "magenta"] @@ -19,18 +31,6 @@ def get_supported_colors(cls): def get_supported_text_style(cls): return ["bold", "italic", "blink"] - def __init__(self) -> None: - self.color: Optional[str] = None - self.prefix: str = "" - self.text_style: Optional[str] = None - self.datetime_format: str = "%Y-%m-%d-%H:%M:%S" - self.with_identifier: bool = True - self.trace_level = 3 - self.with_datetime: bool = True - self.split_char_cmd = " > " - self.split_char_identity = "/" - self.split_char_txt = " | " - def parse(self, pattern: str): # todo pass From a71ce67af93dbeec0e7e65c900d170f3d3872c01 Mon Sep 17 00:00:00 2001 From: visualdust Date: Mon, 20 Nov 2023 12:10:16 +0000 Subject: [PATCH 3/5] fixed silent error on daemon launch --- neetbox/daemon/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/neetbox/daemon/__init__.py b/neetbox/daemon/__init__.py index f9dee65..02400bd 100644 --- a/neetbox/daemon/__init__.py +++ b/neetbox/daemon/__init__.py @@ -29,6 +29,7 @@ def __attach_daemon(daemon_config): "ipython, try to set 'allowIpython' to True." ) return False # ignore if debugging in ipython + pkg.is_installed("flask", try_install_if_not=True) _online_status = connect_daemon(daemon_config) # try to connect daemon logger.log("daemon connection status: " + str(_online_status)) if not _online_status: # if no daemon online From d190f3455bfc8b4117a88b2a4afcd0abf3acbda3 Mon Sep 17 00:00:00 2001 From: visualdust Date: Mon, 20 Nov 2023 12:10:40 +0000 Subject: [PATCH 4/5] fixed unsupposed httpx logging --- neetbox/daemon/_local_http_client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/neetbox/daemon/_local_http_client.py b/neetbox/daemon/_local_http_client.py index 85621f7..4ce734b 100644 --- a/neetbox/daemon/_local_http_client.py +++ b/neetbox/daemon/_local_http_client.py @@ -1,5 +1,9 @@ import httpx +import logging +httpx_logger = logging.getLogger("httpx") +httpx_logger.setLevel(logging.ERROR) + __no_proxy = { "http://": None, "https://": None, From b3665d449f13c7541a760d90f93ac00e79562468 Mon Sep 17 00:00:00 2001 From: visualdust Date: Mon, 20 Nov 2023 12:10:58 +0000 Subject: [PATCH 5/5] added one_hot with ignore label support --- neetbox/torch/nn/functional.py | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 neetbox/torch/nn/functional.py diff --git a/neetbox/torch/nn/functional.py b/neetbox/torch/nn/functional.py new file mode 100644 index 0000000..36f14d5 --- /dev/null +++ b/neetbox/torch/nn/functional.py @@ -0,0 +1,38 @@ +import torch +from typing import Union + +def one_hot( + tensor: torch.Tensor, + num_classes: int, + ignored_label: Union[str, int] = "negative", +): + """An advanced version of F.one_hot with ignore label support. Convert the mask to a one-hot encoded representation by @visualDust + + Args: + tensor (torch.Tensor): indexed label image. Should types int + num_classes (int): number of classes + ignored_label (Union[str|int], optional): specify labels to ignore, or ignore by pattern. Defaults to "negative". + + Returns: + torch.Tensor: one hot encoded tensor + """ + original_shape = tensor.shape + for _ in range(4 - len(tensor.shape)): + tensor = tensor.unsqueeze(0) # H W -> C H W -> B C H W, if applicable + # start to handle ignored label + # convert ignored label into positive index bigger than num_classes + if type(ignored_label) is int: + tensor[tensor == ignored_label] = num_classes + elif ignored_label == "negative": + tensor[tensor < 0] = num_classes + + # check if mask image is valid + if torch.max(tensor) > num_classes: + raise RuntimeError("class values must be smaller than num_classes.") + B, _, H, W = tensor.shape + one_hot = torch.zeros(B, num_classes + 1, H, W) + one_hot.scatter_(1, tensor, 1) # mark 1 on channel(dim=1) with index of mask + one_hot = one_hot[:, :num_classes] # remove ignored label(s) + for _ in range(len(one_hot.shape) - len(original_shape)): + one_hot.squeeze_(0) # B C H W -> H W -> C H W, if applicable + return one_hot \ No newline at end of file