Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ado embedded python code not working

    I have an ado called groupfunction which made use of python for calculating Gini coefficients since it is much faster. However, I realized recently that it is not loading the python function. I thought it may be that I made a mistake in my code, but it seems to be a different problem. I'm testing with the example code given in the manual. The python function is not recognized in the program, but it is in memory and can be called on its own, see below:

    clear all

    version 16.1
    python:
    from sfi import Data, Scalar
    def calcsum(varname, touse):
    x = Data.get(varname, None, touse)
    Scalar.setValue("r(sum)", sum(x))
    end

    program varsum
    version 16.1
    syntax varname [if] [in]
    marksample touse
    python: calcsum("`varlist'", "`touse'")
    display as txt " sum of ‘varlist’: " as res r(sum)
    end




    sysuse auto, clear

    noisily capture varsum price
    dis _rc

    //But function is in memory!!
    gen uno =1
    python: calcsum("price", "uno")
    dis r(sum)


    Any help or thoughts you may have on fixing this would be much appreciated. Thanks!

  • #2
    Also, I realize the indentation is not correct in the code pasted, even with the correct indentation it won't work.

    Comment


    • #3
      You can help those who might be able to help you by posting your code using the code delimiters [CODE] and [/CODE], as explained in section 12 of the Statalist FAQ linked to at the top of the page.

      Comment


      • #4

        Are you using elements from the example "Embedding Python code in an adofile"?

        With your code the Python function calcsum() is not defined within the Stata program varsum, but outside in __main__

        Importing calcsum() from __main__ should work:
        Code:
        program varsum
        
        version 16.1
        syntax varname [if] [in]
        marksample touse
        python: from __main__ import calcsum ; calcsum("`varlist'", "`touse'")
        display as txt " sum of ‘varlist’: " as res r(sum)
        end
        
        python:
        from sfi import Data, Scalar
        def calcsum(varname, touse):
            x = Data.get(varname, None, touse)
            Scalar.setValue("r(sum)", sum(x))
        end
        
        sysuse auto, clear
        varsum price
        ret li

        Comment


        • #5
          Many thanks Bjarte! This works.

          I'm curious as to why this changed. About a year ago I tried out the example from the Stata manual and it worked fine, but now it requires this fix that you shared.

          Comment


          • #6

            The example of "Embedding Python code in an adofile" in [P] page 437 does not need any fix.

            (in your example #1, code was copied out of the ado/program context, and failed ref #4)

            Comment


            • #7
              You're correct, I may have made another mistake. Thanks!

              Comment

              Working...
              X