Skip to content
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

Add concatenate function #452

Merged
merged 11 commits into from Jul 14, 2023
Merged

Add concatenate function #452

merged 11 commits into from Jul 14, 2023

Conversation

twennemann
Copy link
Contributor

@twennemann twennemann commented Mar 29, 2023

Which issue(s) are closed by this pull request?

Closes #

Changes proposed in this pull request:

  • Add a concatenation function, concatenating Signals along specified channels.

  • Add broadcasting parameter, which broadcasts the signals into largest cdim and a common shape, ignoring caxis.

  • Please rename to concatenate_channles

Otherwise approved

@twennemann
Copy link
Contributor Author

I tried to adjust the comment of the merged signal so that the comments can be assigned to the data in the merged signal. First, to the comment of the merged signal is passed on which caxis the signals were merged. If there is a comment in one of the input Signals, that comment is appended, as well as the corresponding channels on caxis.
Questions:

  • Is it right to call it channels?
  • Do we start counting at 0 or at 1? For example first_signal.cshape[caxis] is 3, would we say the comment corresponds to channel 1-3 or 0-2?

pyfar/utils.py Outdated
Comment on lines 134 to 140
Merge multiple Signal objects along a given axis. The signals are copied,
if needed broadcasted in largest dimenson and to a common cshape and a new
object is returned.

The :py:mod:`cshape <pyfar._concepts.audio_classes>` of the signals are
broadcasted following the `numpy broadcasting rules
<https://numpy.org/doc/stable/user/basics.broadcasting.html>`_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to make this shorter and move information about the broadcasting to the Parameters section. caxis could be linked to the audio concepts, where it is explained in more detail

Suggested change
Merge multiple Signal objects along a given axis. The signals are copied,
if needed broadcasted in largest dimenson and to a common cshape and a new
object is returned.
The :py:mod:`cshape <pyfar._concepts.audio_classes>` of the signals are
broadcasted following the `numpy broadcasting rules
<https://numpy.org/doc/stable/user/basics.broadcasting.html>`_
Merge multiple Signal objects along a given caxis.

pyfar/utils.py Outdated
to axis (the first, by default). If this is the case, set
``broadcasting=True``.
caxis : int
The axis along which the signals are concatenated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The axis along which the signals are concatenated.
The caxis along which the signals are concatenated.

We should also add a link to the concepts (see above)

pyfar/utils.py Outdated
Comment on lines 153 to 155
If this is ``True``, the signals will be broadcasted into largest
dimension and into a common cshape, except of the caxis along which the
signals are concatenated. The default is ``False``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If this is ``True``, the signals will be broadcasted into largest
dimension and into a common cshape, except of the caxis along which the
signals are concatenated. The default is ``False``.
If this is ``True``, the signals will be broadcasted to common
cshape, except for the caxis along which the signals are
concatenated. The default is ``False``.

pyfar/utils.py Outdated
Comment on lines 167 to 169
for signal in signals:
if not isinstance(signal, pf.Signal):
raise TypeError("All input data must be of type pf.Signal")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to make the function work also for Frequency and TimeData. Maybe _assert_matching_meta_data can already be used to check if all objects are of the same type.

pyfar/utils.py Outdated
Comment on lines 156 to 160
comment: string
A comment related to the merged `data`. The default is ``""``, which
initializes an empty string. Comments of the input signals will also
be returned in the new Signal object. These comments are marked with
their corresponding signal position in the input tuple.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to simply the comments handling. Use an empty string if the provided comment. Discard comments from input data. What do the others think?

pyfar/utils.py Outdated
cshape = np.broadcast_shapes(*np.delete(cshapes, caxis,
axis=-1))
broad_signals = []
for i, s in enumerate(signals):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using

Suggested change
for i, s in enumerate(signals):
for signal, cshape in zip(signals, cshapes):

might be better to read and avoid indexing

pyfar/utils.py Outdated
Comment on lines 203 to 212
if comment != "":
comment = comment + '\n'
comment = comment + f'Signals concatenated in caxis={caxis}.\n'
sig_channel = 0
for s in signals:
if s.comment != "":
comment += f"Channel {sig_channel+1}-"\
f"{sig_channel + s.cshape[caxis]}: "\
+ s.comment + '\n'
sig_channel += s.cshape[caxis]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above. Should we simplify this?

f-brinkmann
f-brinkmann previously approved these changes May 3, 2023
Copy link
Member

@f-brinkmann f-brinkmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing. Approved and awaiting a discussion on the comment handling...

pyfar/utils.py Outdated
signals : tuple of Signal, TimeData or FrequencyData
The signals to concatenate. All signals must be of the same object type
and either have the same cshape or be broadcastable to the same cshape,
except in the dimension corresponding to axis (the first, by default).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use caxis instead of axis.

@f-brinkmann f-brinkmann self-requested a review May 11, 2023 15:49
Copy link
Member

@f-brinkmann f-brinkmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just recognized that this would need small changes to work for complex signals. But I would suggest to make this a separate pull.

  • the complex variable must be obtained from _assert_matching_meta_data which requires check and test audio arithmetic for complex-valued data #464
  • before concatenating s._data the domain must be set to time if the input data contains a complex Signal object.
  • the complex flag must be set when returning Signal or TimeData objects

noted in #384

@f-brinkmann f-brinkmann mentioned this pull request Jun 2, 2023
34 tasks
@f-brinkmann f-brinkmann self-requested a review June 2, 2023 11:14
f-brinkmann
f-brinkmann previously approved these changes Jun 2, 2023
Copy link
Member

@ahms5 ahms5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for implementing, i just have a few comments on the usage und docstring

pyfar/utils.py Outdated
The default is ``False``.
Returns
-------
merged : Signal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
merged : Signal
merged : Signal, TimeData or FrequencyData


Parameters
----------
signals : tuple of Signal, TimeData or FrequencyData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be possible to also concatenate along the frequency axe? or is it another fucntion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to have, but should be another function

Parameters
----------
signals : tuple of Signal, TimeData or FrequencyData
The signals to concatenate. All signals must be of the same object type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should use a more general name for signals, like data or audio?

@mberz mberz merged commit 362ddd9 into develop Jul 14, 2023
7 checks passed
@mberz mberz deleted the feature/concatenate_function branch July 14, 2023 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants