-
-
Notifications
You must be signed in to change notification settings - Fork 534
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
rename field
to signal
#561
Conversation
as an additional bonus, this PR also adds "inverse choices dicts", which should significantly increase encoding performance for messages which contain many signals that exhibit a lot of choices. @zariiii9003: would be great if you gave this a spin with your DBC file... |
Pull Request Test Coverage Report for Build 4957995570Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Sure. I'm not sure how many choices that message actually has, but decoding with With
|
cantools/database/can/signal.py
Outdated
with contextlib.suppress(KeyError, TypeError): | ||
return self.choices[raw] # type: ignore[index] | ||
if decode_choices and self.choices and raw in self.choices: | ||
assert isinstance(raw, int) |
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.
isinstance
is slow and we shouldn't use assert
outside of test code.
I'd suggest return self.choices[cast(int, raw)]
, because the raw in self.choices
check guarantees, that raw
is an integer.
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.
I've pushed a new version of the patch. I'm a bit out of my depths of where the remaining performance regressions stem from. (IMO they are acceptable, though.) Can you help out?
# we simply assume that the choices are invertible | ||
self._inverse_choices = { str(x[1]): x[0] for x in choices.items() } | ||
|
||
def choice_to_number(self, choice: Union[str, NamedSignalValue]) -> int: |
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.
I hope you'll remember all these API changes when you create the next release 😄
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.
yeah. If not that should not be a too big deal as I'm pretty sure that not too many people use this function as it is pretty low level... (the old function name was a bit of a misnomer IMO, because it also accepted NamedSignalValue
objects.)
if scaling: | ||
raw_value = signal.scaled_to_raw(value) | ||
else: | ||
if isinstance(value, (str, NamedSignalValue)): |
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.
I think there is something wrong here. If scaling
is False, then value
must be the raw value. The choice_to_number()
was already performed in scaled_to_raw()
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 is pretty twisted:
If scaling is False, then value must be the raw value.
it can also be a choice (i.e., 'str' or 'NamedSignalValue'). it can be argued that considering choices should only be done if scaling
is True
, thouhgh. (the existing code does it unconditionally, so I thought I keep it this way...)
cantools/database/utils.py
Outdated
except KeyError: | ||
if not allow_truncated: | ||
raise | ||
continue | ||
|
||
if decode_choices and signal.choices is not None and value in signal.choices: |
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.
Maybe we could simplify this too like in the encoding function, if the performance allows it:
if scaling:
decoded[signal.name] = signal.raw_to_scaled(value, decode_choices)
else:
decoded[signal.name] = value
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.
yes, if we decide that choices are only considered if scaling
is True
and get rid of the decode_choices
parameter. (I'm a bit on the line about whether this is a good idea.)
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.
okay. passing decode_choices
to raw_to_scaled
simplifies matters a bit: 003efb5 . Thanks for the proposal!
…agnostics stuff Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
this is a preparation for piecewise linear function support. Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
instead of doing this directly, we use `signal.raw_to_scaled()` and `signal.scaled_to_raw()`. This requires introducing these methods to the `diagnostics.Data` class. TODO: introduce a common base class for `can.Signal` and `diagnostics.Data`. Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
this allows to accelerate message encoding. Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
it should not matter whether binary data is specified using `bytes`, `bytearray` or `memoryview` objects... Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
b416bc4
to
96cd0fe
Compare
thanks to [at]zariiii9003 for the suggestions. (this is a slightly modified version to allow translation of choices if `scaling` is set to `False`.) Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
cf. python/mypy#4871 thanks to [at]zariiii9003 for pointing this out! Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
do you consider this to be ready @zariiii9003? |
Data/Signal is also used in |
I hope the new variable/helper function names are more expressive. thanks to [at]zariiii9003 for pointing to this. Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io> Signed-off-by: Gerrit Ecke <gerrit.ecke@mbition.io>
good point. I hope to have captured the non-diagnostic code paths here |
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.
I think this is fine. Let's merge this and then i'll have some rebasing or merging to do 😩
I sometimes see this as an opportunity for going over the patch one more time. ;) Let's get this merged. Thanks, @zariiii9003 ! |
this renames the
field
variables of the encoding and decoding utility functions back tosignal
and prepares them for piecewise linear functions.Andreas Lauser <andreas.lauser@mercedes-benz.com>, on behalf of MBition GmbH.
Provider Information