Skip to content

function construct_component_matrix #43

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

Merged
merged 8 commits into from
Aug 7, 2023

Conversation

aajayi-21
Copy link
Contributor

No description provided.

created docstring and tests for construct_component_matrix
Filled and marked test cases.
@aajayi-21 aajayi-21 marked this pull request as ready for review August 3, 2023 17:30
Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

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

I think this already got merged. Did you update your main branch?

@aajayi-21
Copy link
Contributor Author

I believe the last merge added the function that creates the stretching factor matrix, not the component matrix

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

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

couple of comments.

@@ -84,6 +84,35 @@ def construct_stretching_matrix(components, number_of_components, number_of_sign
return stretching_factor_matrix


def construct_component_matrix(components, number_of_components, signal_length):
Copy link
Contributor

Choose a reason for hiding this comment

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

we don't need to pass in number_of_components because that is len(components). I fixed this in the other function before merging, but looking back I see that I did all the t hings except remove it from the signature. Please can you remove it from the arguments here and and also there? Unless I have missed something....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I will fix that here and in the other function

if (number_of_components <= 0) or (len(components) <= 0):
raise ValueError(f"Number of components = {number_of_components}. Number_of_components must be >= 1")

component_matrix = np.zeros((signal_length, number_of_components))
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a slightly ugly way of doing it. Better to maybe use an append. Also, is there a reason we are not doing like

component_matrix[i] = component.iq

Copy link
Contributor Author

Choose a reason for hiding this comment

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

component_matrix is a 2d array so I think that you need two indices. I'm trying to replace each column with columbia.iq

Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't change that because you you are putting a vector at each position, i. It is just syntactically cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. I will change it

replaced with len(components). corrected tests to account
@sbillinge
Copy link
Contributor

....of course you have to switch the order of the axes for this to work.....

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

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

still a bit more cleaning to do

@@ -105,10 +105,10 @@ def construct_component_matrix(components, signal_length):
if len(components) <= 0:
raise ValueError(f"Number of components = {len(components)}. Number_of_components must be >= 1")

component_matrix = np.zeros((signal_length, len(components)))
component_matrix = np.zeros((signal_length, len(components))).T
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be clearer if you did component_matrix = np.zeros((len(components), signal_length)) and then returned component_matrix, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. component_matrix would have the incorrect shape, so you would have to transpose it.

Copy link
Contributor

Choose a reason for hiding this comment

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

even after you change the order in the np.zeros as I did?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

component_matrix should have the shape signal_length by number_of_components once it is returned. If I had component_matrix = np.zeros((len(components), signal_length)) I think that I would have to transpose it after I have replaced the rows inside the for loop.

"""
if signal_length <= 0:
raise ValueError(f"Signal length = {signal_length}. Signal length must be >= 1")
if len(components) <= 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

can a length be less than zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no

The matrix containing the component signal values. Has dimensions `signal_length` x `number_of_components`.

"""
if signal_length <= 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't signal_length = len(components[0].iq)?

So you don't need to pass it in either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true. I will change it

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

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

pls see comment

@@ -105,10 +105,10 @@ def construct_component_matrix(components, signal_length):
if len(components) <= 0:
raise ValueError(f"Number of components = {len(components)}. Number_of_components must be >= 1")

component_matrix = np.zeros((signal_length, len(components)))
component_matrix = np.zeros((signal_length, len(components))).T
Copy link
Contributor

Choose a reason for hiding this comment

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

even after you change the order in the np.zeros as I did?

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

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

small cleaning.

Also, is there a reason that you need the array to be transposed? I know it has to be that way for your code to work, but we could also update the code. If there is a reasonable mathematical reason let's keep it this way, but just check later and let me know. Is it because a matrix multiplication later ?

@@ -188,7 +188,7 @@ def test_construct_stretching_matrix(tcso):
#assert actual[component.id, :] == component.stretching_factors

tccm = [ # (ComponentSignal([0,.25,.5,.75,1],20,0),1,5), # Expected to fail
([ComponentSignal([0,.25,.5,.75,1],20,0)],5),
([ComponentSignal([0,.25,.5,.75,1],20,0)]),
# ([ComponentSignal([0,.25,.5,.75,1],20,2)],1,5), # Expected to fail
Copy link
Contributor

Choose a reason for hiding this comment

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

please can you remove or fix these commented out ones. Probably many of them are not needed now we are not passing in constants. But there may be some new ones you want to fail, but just 2 I can think of

@aajayi-21
Copy link
Contributor Author

I don't believe there is a reason that it needs to be transposed. The component matrix is directly used in matrix multiplication.

@sbillinge sbillinge merged commit 8cf7163 into diffpy:main Aug 7, 2023
@sbillinge
Copy link
Contributor

That's much cleaner and easier to read.... thanks so much. Very satisfying! This code will end up being much nicer and easier to maintain!

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.

2 participants