nd2py.utils package#
Subpackages#
Submodules#
nd2py.utils.attr_dict module#
nd2py.utils.auto_gpu module#
- class nd2py.utils.auto_gpu.AutoGPU[source]#
Bases:
objectAutomatically choose a GPU with enough free memory
nd2py.utils.classproperty module#
nd2py.utils.factory module#
- class nd2py.utils.factory.FactoryMixin[source]#
Bases:
objectMixin class that provides factory pattern capabilities for any class.
Any class inheriting from this mixin gains the ability to: 1. Register subclasses via @<Class>.register_model(‘name’) 2. Create instances via <Class>.create(config, *args, **kwargs)
The base class is automatically registered as ‘default’ model.
═══════════════════════════════════════════════════════════════════════════ INHERITANCE ORDER (IMPORTANT) ═══════════════════════════════════════════════════════════════════════════
FactoryMixin should be placed BEFORE other base classes in the inheritance:
- class MyModel(FactoryMixin, nn.Module):
…
This ensures correct Method Resolution Order (MRO) so that: 1. create() method resolves correctly 2. Subclasses share the same MODEL_DICT through the mixin
═══════════════════════════════════════════════════════════════════════════ EXAMPLE ═══════════════════════════════════════════════════════════════════════════
```python from nd2py.utils import FactoryMixin
- class MyModel(FactoryMixin, nn.Module):
- def __init__(self, config, tokenizer):
super().__init__() self.config = config
@MyModel.register_model(‘gcn’) class GCNModel(MyModel):
- def __init__(self, config, tokenizer):
super().__init__(config, tokenizer) # … custom architecture
# Usage - create() automatically handles model selection config = MyConfig(model=’gcn’) model = MyModel.create(config, tokenizer)
# ‘default’ is automatically the base class config = MyConfig(model=’default’) model = MyModel.create(config, tokenizer) # Returns MyModel instance ```
- MODEL_DICT: Dict[str, Type] = {}#
Registry of model classes keyed by name. Shared across all subclasses.
- classmethod register_model(name: str)[source]#
Decorator to register a model subclass.
- Usage:
@MyModel.register_model(‘gcn’) class GCNModel(MyModel):
…
- classmethod create(config, *args, **kwargs) T[source]#
Factory method to create instance based on config.model.
- Parameters:
config – Configuration object with model type specified in config.model
*args – Positional arguments passed to constructor
**kwargs – Keyword arguments passed to constructor
- Returns:
Instantiated object of the type specified in config.model
- Raises:
ValueError – If config.model is not found in MODEL_DICT
nd2py.utils.fix_parser module#
nd2py.utils.log_exception module#
nd2py.utils.logger module#
- nd2py.utils.logger.init_logger(package_name: str, exp_name: str = None, log_file: str = None, info_level: Literal['debug', 'trace', 'info', 'note', 'warning', 'error', 'critical'] = 'info', file_max_size_MB: float = 50.0, file_backup_count: int = 100, show_lineno_for_all_levels: bool = False)[source]#
Initialize the logger for the package. Args: - package_name: The name of the package / project, used in loggin.getLogger({package_name}.{path}.{to}.{file}). - exp_name: The name of the experiment, used in log prefix. - log_file: The path to the log file. If None, no file logging is performed. - info_level: The level of info logging. Can be one of ‘debug’, ‘info’, ‘note’, ‘warning’, ‘error’, ‘critical’. - file_max_size_MB: The maximum size of the log file in MB. Default is 50MB. - file_backup_count: The number of backup files to keep. Default is 100.
nd2py.utils.metrics module#
nd2py.utils.plot module#
- nd2py.utils.plot.get_fig(RN, CN, FW=None, FH=None, AW=None, AH=None, A_ratio=1.0, LM=3, RM=3, TM=3, BM=3, HS=None, VS=None, dpi=300, fontsize=7, lw=0.5, gridspec=False, **kwargs)[source]#
- nd2py.utils.plot.plot_resilience(f: callable, extent=(0, 1, 0, 1), gridnum=(1000, 1000), cmap=None, norm=None, ax=None, reset_xylim=True, lw=1)[source]#
绘制二维函数的韧性 - f: 二维函数,如 lambda x, y: y - 3*y**2 - y**3 + x*y**3 - extent: 函数定义域 (xmin, xmax, ymin, ymax) - gridnum: 网格数量 (xnum, ynum) — Example: >>> f = lambda x, y: y - 3*y**2 - y**3 + x*y**3 >>> plot_resilience(f, ax=axes[0], extent=(0, 5, 0, 5)) >>> f = lambda x, y: np.sin(np.sqrt(x**2 + y ** 2)) >>> plot_resilience(f, ax=axes[1], extent=(-4*np.pi, 4*np.pi, -4*np.pi, 4*np.pi)) >>> f = lambda x, y: np.sin(np.sqrt(x ** 2 + y ** 2)) * x - y * np.cos(np.sqrt(x ** 2 + y ** 2)) >>> plot_resilience(f, ax=axes[2], extent=(-4*np.pi, 4*np.pi, -4*np.pi, 4*np.pi))
- class nd2py.utils.plot.EqualizeNormalize(*args: Any, **kwargs: Any)[source]#
Bases:
Normalize按分布而非值进行归一化
- nd2py.utils.plot.plotOD(ax, source: List[str], destination: List[str], flow: List[float], location: Dict[str, Tuple[float, float]], linetype: Literal['straight', 'parabola', 'rotated_parabola', 'projected_parabola'] = 'straight', N=100, zorder=10, **kwargs)[source]#
绘制OD流量
- nd2py.utils.plot.clear_svg(path, debug=False)[source]#
matplotlib 生成的 svg 中会使用 <text style=”font: 9.8px ‘Arial’; text-anchor: middle” x=”80.307802” y=”193.900483”>2020-02-02</text> 的语法,而 Powerpoint 无法识别 font: 9.8px ‘Arial’; 的简写记法,只能识别 font-family: ‘Arial’; font-size: 9.8px; 的记法。因此需要进行转换。考虑的属性包括: - font-size - font-family - font-weight - font-style
- nd2py.utils.plot.load_font()[source]#
plt.title(“示例图表:数字平方”, fontproperties=font, size=15)
plt.xlabel(“数字”, fontproperties=font, size=12)
plt.ylabel(“平方”, fontproperties=font, size=12)
nd2py.utils.render_markdown module#
nd2py.utils.render_python module#
nd2py.utils.tag2ansi module#
nd2py.utils.timing module#
Lightweight timing utilities for optional performance diagnostics.
- class nd2py.utils.timing.Timer(unit='iter')[source]#
Bases:
object- property time#
- property count#
- property pace#
- property speed#
- class nd2py.utils.timing.NamedTimer(unit='iter')[source]#
Bases:
Timer- to_str(mode: Literal['time', 'count', 'pace', 'speed'] = 'pace', mode_of_detail: Literal['time', 'count', 'pace', 'speed'] | None = 'pace', mode_of_percent: Literal['by_time', 'by_count'] | None = 'by_time')[source]#
- property names#
- property time#
- property count#
- property named_time#
- property named_count#
- property named_pace#
- property named_speed#
- class nd2py.utils.timing.ParallelTimer(unit='iter')[source]#
Bases:
Timer- to_str(mode: Literal['time', 'count', 'pace', 'speed'] = 'pace', mode_of_detail: Literal['time', 'count', 'pace', 'speed'] | None = 'pace', mode_of_percent: Literal['by_time', 'by_count'] | None = 'by_time')[source]#
- property names#
- property count#
- property named_time#
- property named_count#
- property named_pace#
- property named_speed#