tools 1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tools/torch/utils.py ADDED
@@ -0,0 +1,121 @@
1
+ import random
2
+ import multiprocessing
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ __all__ = [
8
+ 'get_device',
9
+ 'to',
10
+ 'multiprocessing_device',
11
+ 'nanparam',
12
+ 'nangrad',
13
+ 'param_same',
14
+ 'print_rng_state',
15
+ 'seed',
16
+ ]
17
+
18
+ def get_device(network):
19
+ return next(network.parameters()).device
20
+
21
+ # TODO: Merge into get_device
22
+ def device_of_optimizer(optimizer):
23
+ return optimizer.param_groups[0]['params'][0].device
24
+
25
+ def to(data, *args, **kwargs):
26
+ if isinstance(data, (list, tuple)):
27
+ return tuple(getattr(tensor, 'to')(*args, **kwargs) for tensor in data)
28
+ elif isinstance(data, dict):
29
+ return {k: getattr(tensor, 'to')(*args, **kwargs) for k, tensor in data.items()}
30
+ else:
31
+ return data.to(*args, **kwargs)
32
+
33
+ def spread_device(gpu_id = None):
34
+ '''
35
+ what's this for?
36
+ '''
37
+ if gpu_id != None:
38
+ gpu_id = gpu_id % torch.cuda.device_count()
39
+ else:
40
+ gpu_id = torch.cuda.default_stream().device.index
41
+ device = torch.device(f"cuda:{gpu_id}" if torch.cuda.is_available() else "cpu")
42
+ return device
43
+
44
+ def process_number():
45
+ p=multiprocessing.current_process()
46
+ try:
47
+ worker_num = int(p.name.split('-')[-1]) # Fails if it's not integer
48
+ return worker_num
49
+ except ValueError:
50
+ return -1
51
+
52
+ def multiprocessing_device(gpu_id = None):
53
+ '''
54
+ device setting for hydra multiprocessing
55
+ '''
56
+ # CPU
57
+ if gpu_id == -1 or not torch.cuda.is_available():
58
+ device = torch.device('cpu')
59
+ # GPU
60
+ else:
61
+ if gpu_id == None:
62
+ # use distributed device, or default device
63
+ p=multiprocessing.current_process()
64
+ #
65
+ try:
66
+ worker_num = int(p.name.split('-')[-1]) # Fails if it's not integer
67
+ gpu_id = worker_num % torch.cuda.device_count()
68
+ device = torch.device(f"cuda:{gpu_id}")
69
+ except ValueError: # Parent process
70
+ device = torch.cuda.default_stream().device
71
+ else:
72
+ device = torch.device(f'cuda:{gpu_id}')
73
+ torch.cuda.set_device(device)
74
+ torch.cuda.empty_cache()
75
+
76
+ # log.info(f'device: {device}') # integrate logging?
77
+
78
+ return device
79
+
80
+ def nanparam(model):
81
+ for p in model.parameters():
82
+ if torch.isnan(p).any():
83
+ return True
84
+ return False
85
+
86
+ def nangrad(model):
87
+ for p in model.parameters():
88
+ if torch.isnan(p.grad).any():
89
+ return True
90
+ return False
91
+
92
+ def param_same(model1, model2):
93
+ for p1, p2 in zip(model1.parameters(), model2.parameters()):
94
+ same = (p1==p2).all()
95
+ if not same:
96
+ return False
97
+ return True
98
+
99
+ def print_rng_state(n=10, nonzero=True):
100
+ rngs = torch.random.get_rng_state().numpy().tolist()
101
+ if nonzero:
102
+ rngs = [i for i in rngs if i != 0]
103
+ print(rngs[:n])
104
+
105
+
106
+ def seed(random_seed, strict=False):
107
+ '''
108
+
109
+ '''
110
+ np.random.seed(random_seed)
111
+ torch.manual_seed(random_seed)
112
+
113
+ if strict:
114
+ # Following is verbose, but just in case.
115
+ random.seed(random_seed)
116
+ torch.cuda.manual_seed(random_seed)
117
+ torch.cuda.manual_seed_all(random_seed)
118
+
119
+ # deterministic cnn
120
+ torch.backends.cudnn.deterministic = True
121
+ torch.backends.cudnn.benchmark = False
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 JaeSung Yoo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,37 @@
1
+ Metadata-Version: 2.1
2
+ Name: tools
3
+ Version: 1.0
4
+ Summary: python syntax tool
5
+ Home-page: https://github.com/jsyoo61/tools
6
+ Author: JaeSung Yoo
7
+ Author-email: jsyoo61@unc.edu
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE.txt
14
+
15
+ # tools-jsyoo61
16
+
17
+ (`tools` package was replaced on 2025.04.28. If you're looking for web scraping tools, visit http://pypi.python.org/pypi/weblib)
18
+
19
+ Python syntax tools for faster programming. [github](https://github.com/jsyoo61/tools) \
20
+ jsyoo61@unc.edu
21
+
22
+ pip install tools
23
+
24
+ import tools as T
25
+ T.save_pickle(data, 'data.p')
26
+ data = T.load_pickle('data.p')
27
+
28
+ Most useful functions are in `tools.tools`\
29
+ API dependent tools are in their corresponding folders: `tools.API_name` (i.e. `tools.torch.optim`)
30
+
31
+ Planning to document using chatGPT,
32
+ Not documented yet, so you'll have to look at documents written inside the codes. (Most codes contain explanation)
33
+
34
+ I'm open to using my tools for any purpose, and I would appreciate it if the users let me know.
35
+ Please email me before using it.
36
+
37
+ I'm happy to discuss questions or suggestions, please contact me via email. :) \
@@ -0,0 +1,37 @@
1
+ tools/__init__.py,sha256=iicNtDQSQ15NapFJnmv82QhA0qdQkfWD-WBbT1vHE3U,285
2
+ tools/array.py,sha256=5HtcPaThaoMOhUq0w701hds631LxeosPxCQ1HUK61aM,1100
3
+ tools/data.py,sha256=LfAGNUxE0wTwtF6-X7GYXrRrGPeJ7ibaeR5jUJj3rnE,3207
4
+ tools/exp.py,sha256=4iwAeLDcWTx7VbZ2RUlkdvbrUPr5ld0_C79JxnsiLLw,6927
5
+ tools/modules.py,sha256=I-gRL9fB3yXwf5-Nfssk_Otk5wQWANz9vt4-qO8lG_4,3263
6
+ tools/os.py,sha256=Pfq65RqH0LQmGKNBPs0lXVC6_aFZ-JCABTnAM5nKGKI,2279
7
+ tools/random.py,sha256=w6oIdqz8QMdrdzFR_9-CnjRY_DhHTDcHA_SqMDifL9Q,834
8
+ tools/tools.py,sha256=BQE1rY6fxRm47JF9cT-PTQkV0Asb3wIdDaEkkpUoBqw,15802
9
+ tools/hydra/__init__.py,sha256=v77zoVDbu9PmcQ282SrWY3wSNA7D7-JA7zhuMpbRzbk,143
10
+ tools/numpy/__init__.py,sha256=acavFlaFXxlS-rVlPF03BEHq8YZQ4moIfa0CokhkYbc,235
11
+ tools/numpy/_f.py,sha256=ZuW9irjopBtXhVzHbJHuPK6MAlgXY7T5GJqRrGV8Zkk,1389
12
+ tools/numpy/_utils.py,sha256=arDQ09jQcOodmSfQMZbDo9AtzwNgStqUGHUsRC02CDs,4364
13
+ tools/pandas/__init__.py,sha256=RpEHh6y3n1F8sm_H5mW-uexwCTxhkB1VgdxcxrYR_pA,1437
14
+ tools/plot/__init__.py,sha256=KCu-I5vauScayG0fo9ca8oyJA_5IbQVErb3f9vVLdwU,86
15
+ tools/plot/sklearn.py,sha256=ueEErn1osolR6J5TEua9qldiH6hHg3xg1KUA01qh8RA,809
16
+ tools/plot/utils.py,sha256=pzSYT0vrLZdSgyHTaHReS6xF6OrIiK-n1eCoIJCFBqw,2371
17
+ tools/sklearn/__init__.py,sha256=xRv9FnFAWJrna1VLD6rR-NQrVHuojSCiI_gQF_e6qj4,48
18
+ tools/sklearn/metrics.py,sha256=E7MW2fUZYkqXHfFF5I2u-sjz7BwbbeNSIpMtSj5iW8c,3253
19
+ tools/sklearn/model_selection.py,sha256=kjgCvX4E8xvA58BbEKruRlw0Q8GjSTwNEx7sEvq_oxE,8747
20
+ tools/sklearn/preprocessing.py,sha256=myJR2493s77eIPbRToh2CeAHS03kRkHXv9WhulF8e0k,3980
21
+ tools/stats/__init__.py,sha256=ukCyVuhes67YOlxMj0lSErcg-TklHWDmn1oTL1oxaS8,1241
22
+ tools/torch/__init__.py,sha256=CK3PbCBBrEhja3hnE9ozeuEVgIzT9FRYlVNDxNASyp4,306
23
+ tools/torch/_pandas.py,sha256=eL2_4RyPJNeYApZnZWSWw1aI6IKdEKfJxh85M5Aj3Ao,333
24
+ tools/torch/data.py,sha256=jB5wMQWOVHcExaa6keuoRayFLi093E47_ZaDWFQoig8,3013
25
+ tools/torch/estimator.py,sha256=B9HGbPN--k8RDlcO3dqDKOBsezoja8U5G3iksWIJ3nE,2046
26
+ tools/torch/federated_learning.py,sha256=Kmic1xo0lHmfgWiGAoXxt6QKFAj1qVQthfJY1zsRayI,10951
27
+ tools/torch/layers.py,sha256=WIPt5yEmVelYs6CEPJ7V5WrUkkQ22QsnZClHREFJ-S4,310
28
+ tools/torch/model.py,sha256=HpTfqQ5o_Xaj2gk2n1m8W-_xLkWFmiI3YLFHQHPwEzc,18008
29
+ tools/torch/plot.py,sha256=5naNIOOHJi7qipeciCmzzumoQEuKHcK9qX_1E6z5hgE,266
30
+ tools/torch/utils.py,sha256=nPindxaLmykDIAUeftu5dML0ApPpTRHGxPRZHvgwRHo,3351
31
+ tools/torch/optim/__init__.py,sha256=Hw7gdiP-6KGapNWeK3FJfX8olfDU2JopcFxqUcWHvxU,30
32
+ tools/torch/optim/lr_scheduler.py,sha256=0tEi5oh2cJIXSg-q5XQ4347HlmV1O2vQ2TLPVN0VpTU,8208
33
+ tools-1.0.dist-info/LICENSE.txt,sha256=YZ380rWqqDjsxdM2CR_8-OiydcMjilzn8jF5Z2RGMQk,1089
34
+ tools-1.0.dist-info/METADATA,sha256=jpXv0LIMprz3bl2OwDSmd4YHjb9WLyBxjppgwTfB6ek,1318
35
+ tools-1.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
36
+ tools-1.0.dist-info/top_level.txt,sha256=Ib4ZA2MPBpiT0tC-0qt5RcP9g42gLddRbx9PiYGa-Fc,6
37
+ tools-1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.1.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ tools