This repository has been archived by the owner on Jan 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil_nodes.py
294 lines (265 loc) · 11.3 KB
/
util_nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import sys
import logging
import math
import torch
class CyclistMathFloat:
"""Node for simple floating point math operations between two vars. Does not check if values are safe to math."""
@classmethod
def INPUT_TYPES(s):
return { "required": { "operation": (["addition", "subtraction", "multiplication", "division", "exponentiation", "max", "min"],),
"float_1": ("FLOAT", {"default": 1.0, "min": -sys.float_info.max, "max": sys.float_info.max, "step": 0.01, "round": False, "display": "number"}),
"float_2": ("FLOAT", {"default": 1.0, "min": -sys.float_info.max, "max": sys.float_info.max, "step": 0.01, "round": False, "display": "number"})}}
RETURN_TYPES = ("FLOAT", )
FUNCTION = "calc"
CATEGORY = "cyclist/Utilities"
#NODE_NAME = "Float Math"
def calc(self, operation, float_1, float_2):
if operation == "addition":
return (float_1 + float_2,)
elif operation == "subtraction":
return (float_1 - float_2,)
elif operation == "multiplication":
return (float_1 * float_2,)
elif operation == "division":
return (float_1 / float_2,)
elif operation == "exponentiation":
return (float_1 ** float_2,)
elif operation == "max":
if float_1 > float_2:
return (float_1,)
else:
return (float_2,)
elif operation == "min":
if float_1 < float_2:
return (float_1,)
else:
return (float_2,)
else:
raise Exception("Float math operation is not in the list")
return (1.0,)
class CyclistMathInt:
"""Node for simple integer math operations between two vars. Does not check if values are safe to math. Works as python operations."""
@classmethod
def INPUT_TYPES(s):
return { "required": { "operation": (["addition", "subtraction", "multiplication", "floor division", "modulo", "exponentiation", "max", "min"],),
"int_1": ("INT", {"default": 1, "min": -sys.maxsize, "max": sys.maxsize}),
"int_2": ("INT", {"default": 1, "min": -sys.maxsize, "max": sys.maxsize})}}
RETURN_TYPES = ("INT", )
FUNCTION = "calc"
CATEGORY = "cyclist/Utilities"
#NODE_NAME = "Int Math"
def calc(self, operation, int_1, int_2):
if operation == "addition":
return (int_1 + int_2,)
elif operation == "subtraction":
return (int_1 - int_2,)
elif operation == "multiplication":
return (int_1 * int_2,)
elif operation == "floor division":
return (int_1 // int_2,)
elif operation == "modulo":
return (int_1 % int_2,)
elif operation == "exponentiation":
return (int_1 ** int_2,)
elif operation == "max":
if int_1 > int_2:
return (int_1,)
else:
return (int_2,)
elif operation == "min":
if int_1 < int_2:
return (int_1,)
else:
return (int_2,)
else:
raise Exception("Int math operation is not in the list")
return (0,)
# 'required' input can't be '*', unless it can. Thanks, @pythongossss
class AnyType(str):
def __ne__(self, __value: object) -> bool:
return False
class CyclistTypeCast:
"""Tries to cast any input into str, int, float and bool. Int returns mathematically rounded. Float and int return None on fails."""
@classmethod
def INPUT_TYPES(s):
return {"required": {"anything": (AnyType("*"), )}}
RETURN_TYPES = ("STRING", "INT", "FLOAT", "BOOLEAN")
FUNCTION = "cast"
CATEGORY = "cyclist/Utilities"
#NODE_NAME = "Convert to"
def cast(self, anything):
res_str, res_int, res_float, res_bool = my_cast(anything)
if res_int is None:
logging.warn(f"Can't cast {type(anything)} to int")
if res_float is None:
logging.warn(f"Can't cast {type(anything)} to float")
return res_str, res_int, res_float, res_bool
def my_cast(anything):
try:
result_float = float(anything)
result_int = int(round(result_float))
except:
result_float = None
result_int = None
try:
result_bool = bool(anything)
except:
if anything is None:
result_bool = False
else:
result_bool = True
return (str(anything), result_int, result_float, result_bool)
class CyclistCompare:
"""Tries to compare any two inputs. If different types provided, they would be cast to equal types. Order of prefered casts: bool, float, str."""
@classmethod
def INPUT_TYPES(s):
return { "required": {"condition": (["equals", "not equals", "greater than", "less than", "greater or equals", "less or equals"],),
"thing1": (AnyType("*"), ),
"thing2": (AnyType("*"), )}}
RETURN_TYPES = ("BOOLEAN", )
FUNCTION = "compare"
CATEGORY = "cyclist/Utilities"
#NODE_NAME = "Compare Anything"
def compare(self, condition, thing1, thing2):
str1, int1, float1, bool1 = my_cast(thing1)
str2, int2, float2, bool2 = my_cast(thing2)
if torch.is_tensor(thing1):
tensor1 = thing1
elif isinstance(thing1, dict) and "samples" in thing1 and torch.is_tensor(thing1["samples"]):
tensor1 = thing1["samples"]
else:
tensor1 = None
if torch.is_tensor(thing2):
tensor2 = thing2
elif isinstance(thing2, dict) and "samples" in thing2 and torch.is_tensor(thing2["samples"]):
tensor2 = thing2["samples"]
else:
tensor2 = None
if isinstance(thing1, bool) or isinstance(thing2, bool):
return (self.compare_bool(bool1, bool2, condition), )
elif isinstance(thing1, str) or isinstance(thing2, str):
if not float1 is None and not float2 is None:
return (self.compare_float(float1, float2, condition), )
else:
return (self.compare_str(str1, str2, condition), )
elif isinstance(thing1, float) or isinstance(thing2, float):
return (self.compare_float(float1, float2, condition), )
elif isinstance(thing1, int) and isinstance(thing2, int):
return (self.compare_int(int1, int2, condition), )
elif not tensor1 is None and not tensor2 is None:
if (tensor1.dim() == 4 and tensor1.size()[1] != 4) or (tensor2.dim() == 4 and tensor2.size()[1] != 4):
return (self.compare_tensors(tensor1, tensor2, condition, as_images=True), ) # Assume at least one is image
else:
return (self.compare_tensors(tensor1, tensor2, condition), )
else:
try:
if condition == "equals":
result = thing1 == thing2
elif condition == "not equals":
result = thing1 != thing2
elif condition == "greater than":
result = thing1 > thing2
elif condition == "less than":
result = thing1 < thing2
elif condition == "greater or equals":
result = thing1 >= thing2
elif condition == "less or equals":
result = thing1 <= thing2
else:
raise Exception("Compare operation is not in the list")
if not isinstance(result, bool):
raise Exception("Comparison ends in a non-boolean return")
return (result, )
except:
logging.warn("'Compare Anything' node doesn't really know how to compare these inputs, so it falls back to compare string representations.")
return (self.compare_str(str1, str2, condition), )
def compare_bool(self, a, b, condition):
if condition == "equals":
return a == b
elif condition == "not equals":
return a != b
elif condition == "greater than":
return a == True and b == False
elif condition == "less than":
return a == False and b == True
elif condition == "greater or equals":
return True
elif condition == "less or equals":
return True
else:
raise Exception("Compare operation is not in the list")
def compare_str(self, a, b, condition):
if condition == "equals":
return a == b
elif condition == "not equals":
return a != b
elif condition == "greater than":
return a > b
elif condition == "less than":
return a < b
elif condition == "greater or equals":
return a >= b
elif condition == "less or equals":
return a <= b
else:
raise Exception("Compare operation is not in the list")
def compare_float(self, a, b, condition):
if condition == "equals":
return math.isclose(a, b)
elif condition == "not equals":
return not math.isclose(a, b)
elif condition == "greater than":
return a > b
elif condition == "less than":
return a < b
elif condition == "greater or equals":
return a >= b or math.isclose(a, b)
elif condition == "less or equals":
return a <= b or math.isclose(a, b)
else:
raise Exception("Compare operation is not in the list")
def compare_int(self, a, b, condition):
if condition == "equals":
return a == b
elif condition == "not equals":
return a != b
elif condition == "greater than":
return a > b
elif condition == "less than":
return a < b
elif condition == "greater or equals":
return a >= b
elif condition == "less or equals":
return a <= b
else:
raise Exception("Compare operation is not in the list")
def compare_tensors(self, a, b, condition, as_images=False):
if condition == "equals":
return torch.equal(a, b)
elif condition == "not equals":
return not torch.equal(a, b)
else:
size1 = 1
if as_images:
all_sizes1 = a.size()[:3] # Only comparing pixels count, not channels count
else:
all_sizes1 = a.size()
for i in all_sizes1:
size1 *= i
size2 = 1
if as_images:
all_sizes2 = b.size()[:3] # Only comparing pixels count, not channels count
else:
all_sizes2 = b.size()
for i in all_sizes2:
size2 *= i
if condition == "greater than":
return size1 > size2
elif condition == "less than":
return size1 < size2
elif condition == "greater or equals":
return size1 > size2 or torch.equal(a, b)
elif condition == "less or equals":
return size1 < size2 or torch.equal(a, b)
else:
raise Exception("Compare operation is not in the list")