-
Notifications
You must be signed in to change notification settings - Fork 57
Features: TakeProperties #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
381630d
c6132f4
4b68ce1
21044e8
0f55b17
017f21e
3e4a546
83bb491
8851004
c806cfd
a1f71d7
b1ca6ca
0a78b0f
2ea065b
d1b8d34
84fecbd
6eafa18
addebc9
15cdd3c
ef06761
c8b3df6
17e3921
83a3566
158f247
00533aa
910dae0
755e09b
046b6e2
0c83f7c
b2da196
5730015
0c16b48
c8319d3
01fe26d
c9c418a
d414d40
5179396
62a4507
1b8a24c
66108d8
5fbb7cd
6c744fb
5224632
4b127ef
0edf8fd
c827935
6f65d74
63093f9
3f8a9cf
3af492b
ae805ee
7a5b83f
1cf1973
27a76b1
927f6ab
7040b1c
bfd6852
f90f2c1
f25fc1c
65773d0
155dcf3
12a88e1
4eb169c
e6734f0
3dee646
a5a9736
dcae336
c7985bf
0128f22
2234f8d
a23fa7c
5c67108
fd5fc9f
1714d5c
3ef47fd
9f1f019
ca3e443
b5ea873
9c7bb29
ee903ab
4d94ed0
a58bdc8
485fd1d
ad358a3
5610d40
eb17bab
6ccdfac
4b4d547
0cc0132
362ad85
07383ce
0be5371
fffdf6d
df8330f
57e42ec
e4e9750
4355a71
55d6788
a55e95f
b247a65
2c61e9f
434d661
c356091
f790513
c05ca7a
9b42746
bb709db
d5920c8
d28a829
50491da
a969692
1320523
adaa426
7d2379f
2c0edc6
38319bf
edb5c5b
91633c6
9c7e4c4
c6934da
673b98c
263bd57
dac7c79
fb865d5
e6dc8b8
26ea2b2
cf2fce7
3572764
cafb1e0
1cf97ff
6d67783
5cf559b
7945439
1ba87cd
4d51258
352bcb2
396b954
6ce4109
43b7f37
f6383ff
582d60e
3c848b5
2434e7e
cdafe07
e8d797f
9067d2d
19cb53e
1cfa3ae
3bda822
445abd8
df7fb81
69ae21a
c35e5be
1bb3541
d9d93ea
f54dd7b
3dcfbee
08c3cb9
3c5a439
c34f3da
a0c1692
b715bda
2b36344
ab42235
88c4458
dcd3314
ca587ea
0f7ab6b
5b8db3e
e36d948
1ddce89
6057d6e
3638042
7a757e7
fcc3802
bc6a9ca
cbaa5fc
2afc37d
cb892da
28488f6
6c9dccc
1c60d4e
612e125
0e37b0f
e935305
472d769
ea8be6b
7d8529d
a73a799
9b5bc20
8013e17
033d3a0
a9c8f5d
f726960
6db8a93
4527ff2
6412727
f6de929
75bf02d
d02d6ab
5cbefb9
e8d781c
3e614c0
a208162
fb34c58
cf185f1
467ea52
19b0e9c
1281142
21033f6
5418ed5
bd596df
0a61658
838f766
7f643b7
32e4e90
d97196c
fff6885
22452fe
9ec752b
20ed6d3
b137273
9f97899
bd93967
080043e
9476cb6
a3e961b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,7 +230,7 @@ def propagate_data_to_dependencies( | |
"Transpose", | ||
"Permute", | ||
"OneHot", | ||
"TakeProperties", #TODO ***JH*** | ||
"TakeProperties", | ||
] | ||
|
||
|
||
|
@@ -9440,11 +9440,11 @@ def get( | |
class TakeProperties(Feature): | ||
"""Extract all instances of a set of properties from a pipeline. | ||
|
||
Only extracts the properties if the feature contains all given | ||
property-names. The order of the properties is not guaranteed to be the | ||
Only extracts the properties if the feature contains all given | ||
property-names. The order of the properties is not guaranteed to be the | ||
same as the evaluation order. | ||
|
||
If there is only a single property name, this will return a list of the | ||
If there is only a single property name, this will return a list of the | ||
property values. | ||
|
||
Parameters | ||
|
@@ -9453,7 +9453,7 @@ class TakeProperties(Feature): | |
The feature from which to extract properties. | ||
names: list[str] | ||
The names of the properties to extract | ||
**kwargs:: dict of str to Any | ||
**kwargs: dict of str to Any | ||
Additional keyword arguments passed to the parent `Feature` class. | ||
|
||
Attributes | ||
|
@@ -9462,18 +9462,18 @@ class TakeProperties(Feature): | |
Indicates whether this feature distributes computation across inputs. | ||
Always `False` for `TakeProperties`, as it processes sequentially. | ||
__list_merge_strategy__: int | ||
Specifies how lists of properties are merged. Set to | ||
Specifies how lists of properties are merged. Set to | ||
`MERGE_STRATEGY_APPEND` to append values to the result list. | ||
|
||
Methods | ||
------- | ||
`get(image: Any, names: tuple[str, ...], **kwargs: dict[str, Any]) -> np.ndarray | tuple[np.ndarray, ...]` | ||
Extract the specified properties from the feature pipeline. | ||
|
||
Examples | ||
-------- | ||
>>> import deeptrack as dt | ||
|
||
>>> class ExampleFeature(Feature): | ||
... def __init__(self, my_property, **kwargs): | ||
... super().__init__(my_property=my_property, **kwargs) | ||
|
@@ -9489,7 +9489,7 @@ class TakeProperties(Feature): | |
|
||
Create a `Gaussian` feature: | ||
>>> noise_feature = dt.Gaussian(mu=7, sigma=12) | ||
|
||
Use `TakeProperties` to extract the property: | ||
>>> take_properties = dt.TakeProperties(noise_feature) | ||
>>> output = take_properties.get(image=None, names=["mu"]) | ||
|
@@ -9502,9 +9502,9 @@ class TakeProperties(Feature): | |
__list_merge_strategy__: int = MERGE_STRATEGY_APPEND | ||
|
||
def __init__( | ||
self: TakeProperties, | ||
self: Feature, | ||
feature: Feature, | ||
*names: str, | ||
*names: PropertyLike[str], | ||
**kwargs: Any, | ||
): | ||
"""Initialize the TakeProperties feature. | ||
|
@@ -9513,23 +9513,23 @@ def __init__( | |
---------- | ||
feature: Feature | ||
The feature from which to extract properties. | ||
*names: str | ||
*names: PropertyLike[str] | ||
One or more names of the properties to extract. | ||
= **kwargs: Any, optional | ||
**kwargs: Any, optional | ||
Additional keyword arguments passed to the parent `Feature` class. | ||
|
||
""" | ||
|
||
super().__init__(names=names, **kwargs) | ||
self.feature = self.add_feature(feature) | ||
|
||
def get( | ||
self: TakeProperties, | ||
self: Feature, | ||
image: Any, | ||
names: tuple[str, ...], | ||
_ID: tuple[int, ...] = (), | ||
**kwargs: Any, | ||
) -> np.ndarray | tuple[np.ndarray, ...]: | ||
) -> NDArray | tuple[NDArray, torch.Tensor, ...]: | ||
"""Extract the specified properties from the feature pipeline. | ||
|
||
This method retrieves the values of the specified properties from the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is one extra blank space at the end of the line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also the case for line 9545 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And some more lines. So please double check this :) |
||
|
@@ -9549,7 +9549,7 @@ def get( | |
|
||
Returns | ||
------- | ||
np.ndarray or tuple[np.ndarray, ...] | ||
NDArray or tuple[NDArray, torch.Tensor, ...] | ||
If a single property name is provided, a NumPy array containing the | ||
property values is returned. If multiple property names are | ||
provided, a tuple of NumPy arrays is returned, where each array | ||
|
@@ -9578,8 +9578,8 @@ def get( | |
if key[:len(_ID)] == _ID: | ||
res[name].append(value.current_value()) | ||
|
||
# Convert the results to NumPy arrays. | ||
res = tuple([np.array(res[name]) for name in names]) | ||
# Convert the results to tuple. | ||
res = tuple([res[name] for name in names]) | ||
|
||
# Return a single array if only one property name is specified. | ||
if len(res) == 1: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2678,23 +2678,59 @@ def __init__(self, my_property, **kwargs): | |
output = take_properties.get(image=None, names=["my_property"]) | ||
self.assertEqual(output, [42]) | ||
|
||
# with `Gaussian` feature | ||
# with `Gaussian` feature | ||
noise_feature = Gaussian(mu=7, sigma=12) | ||
|
||
take_properties = features.TakeProperties(noise_feature) | ||
output = take_properties.get(image=None, names=["mu"]) | ||
self.assertEqual(output, [7]) | ||
output = take_properties.get(image=None, names=["sigma"]) | ||
self.assertEqual(output, [12]) | ||
|
||
# with `Gaussian` feature | ||
noise_feature = Gaussian(mu=7, sigma=12) | ||
# with `Gaussian` feature with float properties | ||
noise_feature = Gaussian(mu=7.123, sigma=12.123) | ||
|
||
take_properties = features.TakeProperties(noise_feature) | ||
output = take_properties.get(image=None, names=["mu"]) | ||
self.assertEqual(output, [7]) | ||
output = take_properties.get(image=None, names=["sigma"]) | ||
self.assertEqual(output, [12]) | ||
output = take_properties.get(image=None, names=["mu", "sigma"]) | ||
self.assertEqual(output, ([7.123], [12.123])) | ||
self.assertEqual(output[0][0], 7.123) | ||
self.assertEqual(output[1][0], 12.123) | ||
|
||
### Test with PyTorch tensor (if available) | ||
if TORCH_AVAILABLE: | ||
class ExampleFeature(features.Feature): | ||
def __init__(self, my_property, **kwargs): | ||
super().__init__(my_property=my_property, **kwargs) | ||
|
||
feature = ExampleFeature(my_property= | ||
properties.Property(torch.tensor(42.123))) | ||
|
||
take_properties = features.TakeProperties(feature) | ||
take_properties = features.TakeProperties(feature) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for writing this twice after each other? It is the same for the numpy test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, this is something historic, I just copied and pasted from the previous numpy test. I will check and clean them, |
||
output = take_properties.get(image=None, names=["my_property"]) | ||
torch.testing.assert_close(output[0], torch.tensor(42.123)) | ||
|
||
# with `Gaussian` feature | ||
noise_feature = Gaussian( | ||
mu=torch.tensor(7), sigma=torch.tensor(12) | ||
) | ||
|
||
take_properties = features.TakeProperties(noise_feature) | ||
output = take_properties.get(image=None, names=["mu"]) | ||
torch.testing.assert_close(output[0], torch.tensor(7)) | ||
output = take_properties.get(image=None, names=["sigma"]) | ||
torch.testing.assert_close(output[0], torch.tensor(12)) | ||
|
||
# with `Gaussian` feature with float properties | ||
random_mu = torch.rand(1) | ||
random_sigma = torch.rand(1) | ||
noise_feature = Gaussian(mu=random_mu, sigma=random_sigma) | ||
|
||
take_properties = features.TakeProperties(noise_feature) | ||
output = take_properties.get(image=None, names=["mu", "sigma"]) | ||
torch.testing.assert_close(output, ([random_mu], [random_sigma])) | ||
torch.testing.assert_close(output[0][0], random_mu) | ||
torch.testing.assert_close(output[1][0], random_sigma) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is too long