How do I keep the dtype intact of a kwarg?











up vote
0
down vote

favorite












For a script that I am working on I want to make it optional to pass on an array to a function. The way in which I have attempted to do this is by making the variable in question (residue) a kwarg.



The problem is that when I do it in this way, python changes de dtype of the kwarg from a numpy.ndarray to dict. The simplest solution is to convert the variable back to a np.array using:



    residue = np.array(residue.values())


But I do not find this a very elegant solution. So I was wondering if someone could show me a "prettier" way to accomplish this and possibly explain to my why python does this?



The function in question is:



    #Returns a function for a 2D Gaussian model 
def Gaussian_model2D(data,x_box,y_box,amplitude,x_stddev,y_stddev,theta,**residue):
if not residue:
x_mean, y_mean = max_pixel(data) # Returns location of maximum pixel value
else:
x_mean, y_mean = max_pixel(residue) # Returns location of maximum pixel value
g_init = models.Gaussian2D(amplitude,x_mean,y_mean,x_stddev,y_stddev,theta)
return g_init
# end of Gaussian_model2D


The function is called with the following command:



    g2_init = Gaussian_model2D(cut_out,x_box,y_box,amp,x_stddev,y_stddev,theta,residue=residue1)


The version of Python that I am working in is 2.7.15










share|improve this question


















  • 2




    That is not how optional arguments work.
    – user2357112
    Nov 19 at 17:54






  • 2




    def Gaussian_model2D(data, x_box, y_box, amplitude, x_stddev, y_stddev, theta, residue=None):
    – Idlehands
    Nov 19 at 17:55








  • 1




    dtype cannot be dict...
    – juanpa.arrivillaga
    Nov 19 at 17:58










  • Make residue an optional argument, something like def foo(residue=None) then check if residue is not None: ... do stuff with residue
    – juanpa.arrivillaga
    Nov 19 at 17:59










  • Using residue = None gives me the following error when compiling: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    – Crispyvb
    Nov 19 at 18:09















up vote
0
down vote

favorite












For a script that I am working on I want to make it optional to pass on an array to a function. The way in which I have attempted to do this is by making the variable in question (residue) a kwarg.



The problem is that when I do it in this way, python changes de dtype of the kwarg from a numpy.ndarray to dict. The simplest solution is to convert the variable back to a np.array using:



    residue = np.array(residue.values())


But I do not find this a very elegant solution. So I was wondering if someone could show me a "prettier" way to accomplish this and possibly explain to my why python does this?



The function in question is:



    #Returns a function for a 2D Gaussian model 
def Gaussian_model2D(data,x_box,y_box,amplitude,x_stddev,y_stddev,theta,**residue):
if not residue:
x_mean, y_mean = max_pixel(data) # Returns location of maximum pixel value
else:
x_mean, y_mean = max_pixel(residue) # Returns location of maximum pixel value
g_init = models.Gaussian2D(amplitude,x_mean,y_mean,x_stddev,y_stddev,theta)
return g_init
# end of Gaussian_model2D


The function is called with the following command:



    g2_init = Gaussian_model2D(cut_out,x_box,y_box,amp,x_stddev,y_stddev,theta,residue=residue1)


The version of Python that I am working in is 2.7.15










share|improve this question


















  • 2




    That is not how optional arguments work.
    – user2357112
    Nov 19 at 17:54






  • 2




    def Gaussian_model2D(data, x_box, y_box, amplitude, x_stddev, y_stddev, theta, residue=None):
    – Idlehands
    Nov 19 at 17:55








  • 1




    dtype cannot be dict...
    – juanpa.arrivillaga
    Nov 19 at 17:58










  • Make residue an optional argument, something like def foo(residue=None) then check if residue is not None: ... do stuff with residue
    – juanpa.arrivillaga
    Nov 19 at 17:59










  • Using residue = None gives me the following error when compiling: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    – Crispyvb
    Nov 19 at 18:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











For a script that I am working on I want to make it optional to pass on an array to a function. The way in which I have attempted to do this is by making the variable in question (residue) a kwarg.



The problem is that when I do it in this way, python changes de dtype of the kwarg from a numpy.ndarray to dict. The simplest solution is to convert the variable back to a np.array using:



    residue = np.array(residue.values())


But I do not find this a very elegant solution. So I was wondering if someone could show me a "prettier" way to accomplish this and possibly explain to my why python does this?



The function in question is:



    #Returns a function for a 2D Gaussian model 
def Gaussian_model2D(data,x_box,y_box,amplitude,x_stddev,y_stddev,theta,**residue):
if not residue:
x_mean, y_mean = max_pixel(data) # Returns location of maximum pixel value
else:
x_mean, y_mean = max_pixel(residue) # Returns location of maximum pixel value
g_init = models.Gaussian2D(amplitude,x_mean,y_mean,x_stddev,y_stddev,theta)
return g_init
# end of Gaussian_model2D


The function is called with the following command:



    g2_init = Gaussian_model2D(cut_out,x_box,y_box,amp,x_stddev,y_stddev,theta,residue=residue1)


The version of Python that I am working in is 2.7.15










share|improve this question













For a script that I am working on I want to make it optional to pass on an array to a function. The way in which I have attempted to do this is by making the variable in question (residue) a kwarg.



The problem is that when I do it in this way, python changes de dtype of the kwarg from a numpy.ndarray to dict. The simplest solution is to convert the variable back to a np.array using:



    residue = np.array(residue.values())


But I do not find this a very elegant solution. So I was wondering if someone could show me a "prettier" way to accomplish this and possibly explain to my why python does this?



The function in question is:



    #Returns a function for a 2D Gaussian model 
def Gaussian_model2D(data,x_box,y_box,amplitude,x_stddev,y_stddev,theta,**residue):
if not residue:
x_mean, y_mean = max_pixel(data) # Returns location of maximum pixel value
else:
x_mean, y_mean = max_pixel(residue) # Returns location of maximum pixel value
g_init = models.Gaussian2D(amplitude,x_mean,y_mean,x_stddev,y_stddev,theta)
return g_init
# end of Gaussian_model2D


The function is called with the following command:



    g2_init = Gaussian_model2D(cut_out,x_box,y_box,amp,x_stddev,y_stddev,theta,residue=residue1)


The version of Python that I am working in is 2.7.15







python python-2.7 dictionary kwargs numpy-ndarray






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 17:50









Crispyvb

31




31








  • 2




    That is not how optional arguments work.
    – user2357112
    Nov 19 at 17:54






  • 2




    def Gaussian_model2D(data, x_box, y_box, amplitude, x_stddev, y_stddev, theta, residue=None):
    – Idlehands
    Nov 19 at 17:55








  • 1




    dtype cannot be dict...
    – juanpa.arrivillaga
    Nov 19 at 17:58










  • Make residue an optional argument, something like def foo(residue=None) then check if residue is not None: ... do stuff with residue
    – juanpa.arrivillaga
    Nov 19 at 17:59










  • Using residue = None gives me the following error when compiling: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    – Crispyvb
    Nov 19 at 18:09














  • 2




    That is not how optional arguments work.
    – user2357112
    Nov 19 at 17:54






  • 2




    def Gaussian_model2D(data, x_box, y_box, amplitude, x_stddev, y_stddev, theta, residue=None):
    – Idlehands
    Nov 19 at 17:55








  • 1




    dtype cannot be dict...
    – juanpa.arrivillaga
    Nov 19 at 17:58










  • Make residue an optional argument, something like def foo(residue=None) then check if residue is not None: ... do stuff with residue
    – juanpa.arrivillaga
    Nov 19 at 17:59










  • Using residue = None gives me the following error when compiling: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    – Crispyvb
    Nov 19 at 18:09








2




2




That is not how optional arguments work.
– user2357112
Nov 19 at 17:54




That is not how optional arguments work.
– user2357112
Nov 19 at 17:54




2




2




def Gaussian_model2D(data, x_box, y_box, amplitude, x_stddev, y_stddev, theta, residue=None):
– Idlehands
Nov 19 at 17:55






def Gaussian_model2D(data, x_box, y_box, amplitude, x_stddev, y_stddev, theta, residue=None):
– Idlehands
Nov 19 at 17:55






1




1




dtype cannot be dict...
– juanpa.arrivillaga
Nov 19 at 17:58




dtype cannot be dict...
– juanpa.arrivillaga
Nov 19 at 17:58












Make residue an optional argument, something like def foo(residue=None) then check if residue is not None: ... do stuff with residue
– juanpa.arrivillaga
Nov 19 at 17:59




Make residue an optional argument, something like def foo(residue=None) then check if residue is not None: ... do stuff with residue
– juanpa.arrivillaga
Nov 19 at 17:59












Using residue = None gives me the following error when compiling: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
– Crispyvb
Nov 19 at 18:09




Using residue = None gives me the following error when compiling: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
– Crispyvb
Nov 19 at 18:09












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










See the accepted answer here why you always get a mapping-object (aka a dict) if you pass arguments via **kwargs; the language spec says:




If the form “**identifier” is present, it is initialized to a new
ordered mapping receiving any excess keyword arguments, defaulting to
a new empty mapping of the same type.




In other words, the behavior you described is exactly what the language guarantees.



One of the reasons for this behavior is that all functions, wrappers, and implementations in the underlying language (e.g. C / J) will understand that **kwargs is part of the arguments and should be expanded to its key-value combinations.
If you want to preserve your extra-arguments as an object of a certain type, you can't use **kwargs to do so; pass it via an explicit argument, e.g. extra_args which has no special meaning.






share|improve this answer





















  • I understand, thanks for the explanation!
    – Crispyvb
    Nov 20 at 11:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380139%2fhow-do-i-keep-the-dtype-intact-of-a-kwarg%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










See the accepted answer here why you always get a mapping-object (aka a dict) if you pass arguments via **kwargs; the language spec says:




If the form “**identifier” is present, it is initialized to a new
ordered mapping receiving any excess keyword arguments, defaulting to
a new empty mapping of the same type.




In other words, the behavior you described is exactly what the language guarantees.



One of the reasons for this behavior is that all functions, wrappers, and implementations in the underlying language (e.g. C / J) will understand that **kwargs is part of the arguments and should be expanded to its key-value combinations.
If you want to preserve your extra-arguments as an object of a certain type, you can't use **kwargs to do so; pass it via an explicit argument, e.g. extra_args which has no special meaning.






share|improve this answer





















  • I understand, thanks for the explanation!
    – Crispyvb
    Nov 20 at 11:02















up vote
0
down vote



accepted










See the accepted answer here why you always get a mapping-object (aka a dict) if you pass arguments via **kwargs; the language spec says:




If the form “**identifier” is present, it is initialized to a new
ordered mapping receiving any excess keyword arguments, defaulting to
a new empty mapping of the same type.




In other words, the behavior you described is exactly what the language guarantees.



One of the reasons for this behavior is that all functions, wrappers, and implementations in the underlying language (e.g. C / J) will understand that **kwargs is part of the arguments and should be expanded to its key-value combinations.
If you want to preserve your extra-arguments as an object of a certain type, you can't use **kwargs to do so; pass it via an explicit argument, e.g. extra_args which has no special meaning.






share|improve this answer





















  • I understand, thanks for the explanation!
    – Crispyvb
    Nov 20 at 11:02













up vote
0
down vote



accepted







up vote
0
down vote



accepted






See the accepted answer here why you always get a mapping-object (aka a dict) if you pass arguments via **kwargs; the language spec says:




If the form “**identifier” is present, it is initialized to a new
ordered mapping receiving any excess keyword arguments, defaulting to
a new empty mapping of the same type.




In other words, the behavior you described is exactly what the language guarantees.



One of the reasons for this behavior is that all functions, wrappers, and implementations in the underlying language (e.g. C / J) will understand that **kwargs is part of the arguments and should be expanded to its key-value combinations.
If you want to preserve your extra-arguments as an object of a certain type, you can't use **kwargs to do so; pass it via an explicit argument, e.g. extra_args which has no special meaning.






share|improve this answer












See the accepted answer here why you always get a mapping-object (aka a dict) if you pass arguments via **kwargs; the language spec says:




If the form “**identifier” is present, it is initialized to a new
ordered mapping receiving any excess keyword arguments, defaulting to
a new empty mapping of the same type.




In other words, the behavior you described is exactly what the language guarantees.



One of the reasons for this behavior is that all functions, wrappers, and implementations in the underlying language (e.g. C / J) will understand that **kwargs is part of the arguments and should be expanded to its key-value combinations.
If you want to preserve your extra-arguments as an object of a certain type, you can't use **kwargs to do so; pass it via an explicit argument, e.g. extra_args which has no special meaning.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 17:56









user2722968

2,64211637




2,64211637












  • I understand, thanks for the explanation!
    – Crispyvb
    Nov 20 at 11:02


















  • I understand, thanks for the explanation!
    – Crispyvb
    Nov 20 at 11:02
















I understand, thanks for the explanation!
– Crispyvb
Nov 20 at 11:02




I understand, thanks for the explanation!
– Crispyvb
Nov 20 at 11:02


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380139%2fhow-do-i-keep-the-dtype-intact-of-a-kwarg%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy