-
Notifications
You must be signed in to change notification settings - Fork 8
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
Conversation
created docstring and tests for construct_component_matrix
Filled and marked test cases.
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 already got merged. Did you update your main branch?
I believe the last merge added the function that creates the stretching factor matrix, not the component matrix |
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.
couple of comments.
diffpy/snmf/subroutines.py
Outdated
@@ -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): |
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.
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....
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.
Ok. I will fix that here and in the other function
diffpy/snmf/subroutines.py
Outdated
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)) |
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 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
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.
component_matrix is a 2d array so I think that you need two indices. I'm trying to replace each column with columbia.iq
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 doesn't change that because you you are putting a vector at each position, i. It is just syntactically cleaner.
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 see. I will change it
replaced with len(components). corrected tests to account
....of course you have to switch the order of the axes for this to work..... |
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.
still a bit more cleaning to do
diffpy/snmf/subroutines.py
Outdated
@@ -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 |
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 it would be clearer if you did component_matrix = np.zeros((len(components), signal_length))
and then returned component_matrix
, no?
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. component_matrix
would have the incorrect shape, so you would have to transpose it.
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.
even after you change the order in the np.zeros as I did?
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.
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.
diffpy/snmf/subroutines.py
Outdated
""" | ||
if signal_length <= 0: | ||
raise ValueError(f"Signal length = {signal_length}. Signal length must be >= 1") | ||
if len(components) <= 0: |
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.
can a length be less than zero?
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.
no
diffpy/snmf/subroutines.py
Outdated
The matrix containing the component signal values. Has dimensions `signal_length` x `number_of_components`. | ||
|
||
""" | ||
if signal_length <= 0: |
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.
isn't signal_length = len(components[0].iq)
?
So you don't need to pass it in either.
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.
That's true. I will change it
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.
pls see comment
diffpy/snmf/subroutines.py
Outdated
@@ -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 |
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.
even after you change the order in the np.zeros as I did?
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.
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 |
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.
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
I don't believe there is a reason that it needs to be transposed. The component matrix is directly used in matrix multiplication. |
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! |
No description provided.