Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Tony

Pages: 1 ... 26 27 [28] 29 30 ... 39
406
Help and Support / Re: Limit on array length
« on: October 30, 2013, 03:41:48 PM »
Hi,

usually creating numpy arrays is quite fast as long as you're not resizing the array as you add values (which will be slow). I just did a quick test in ipython and this is what I found:

In [1]: import numpy as np
In [2]: %timeit np.array(100000000)
1000000 loops, best of 3: 633 ns per loop

Without seeing your code it's difficult to guess as to why it could be slow. You could try using cProfile to identify where the time's going (writing a standalone script to profile is probably easiest).

Best regards,
Tony

407
Hi,

you can have an array function and just make the range of the array function larger than the number of results. You will get N/A in the cells below the last result. This is the simplest thing to do, and as you are intending to replace this anyway I wouldn't suggest trying anything more complicated.

You can write back to Excel if you want to write to a range larger than the range of the formula itself, and there's an example that shows how to do that - see the 'automation_example' function here:
http://www.pyxll.com/docs/examples/automation.html

For caching objects rather than returning the numbers to Excel directly you could start with the object cache example here:
http://www.pyxll.com/docs/examples/objectcache.html

I hope this helps.

Best regards,
Tony

408
Help and Support / Re: Limit on array length
« on: October 03, 2013, 09:37:29 AM »
Hi,

when you call Application.Run you are making a call to the Excel Automation API which is why this article is applicable.

PyXLL only creates one function for your Python function, and it's the same one that's called regardless of how you call it and the data structures used support more than 65536 rows (from Excel 2007 onwards). This is why I think it's a problem with how Excel is calling the function, rather than the function itself.

The PyXLL wrapper functions are exposed as regular dll exports so perhaps it would be possible to get VBA to call them directly rather than using Application.Run... that's not something I've ever tried though.

Alternatively you could have a VBA function than chunked your array into blocks of 65536 and passed them into python one at a time to be reconstructed before calling the actual python function you want - not particularly elegant but it might be a workable solution for you, something like (pseudo code):

_arrays = {}
def store_partial_array(handle, array):
    _arrays.setdefault(handle, []).extend(array)

def my_function(array, .args):
    if isinstance(array, string):   # this could also be done using a custom type (http://www.pyxll.com/docs/udfs.html#custom-types)
        array = _arrays.pop(array_handle)
    ... do stuff ....

Sub CallArrayFunc(...)
    For Each chunk in array
        Application.Run(store_partial_array, handle, chunk)
    End
    Application.Run(my_function, handle, ...)
End Sub

Best regards,
Tony

409
Help and Support / Re: Installation error
« on: October 02, 2013, 03:49:54 PM »
You need to run that installer from the link I sent, not just rename the dlls.

I suspect the python dll is failing to load, not because it can't be found but because it can't load the other dlls it needs (ie the c runtime dlls). These need to be installed in the correct place, not just next to the dll as windows is most likely looking for them in the side-by-side (WinSxS) folder as the initial executable (Excel 2007) has no embedded manifest to tell it to get them from somewhere else.

Please can you try actually installing that runtime package and see if that works?

Best regards,
Tony

410
Help and Support / Re: Installation error
« on: October 02, 2013, 03:00:03 PM »
Hi,

it's possible that the correct c runtime you need for Python 2.7 hasn't been installed. Normally it is installed as part of Python, but I think that's not the case with portable python.

You can try installing it from here:

http://support.microsoft.com/kb/2019667 ("Visual Studio 2008 VC++ (SP1) - VC++ Redistributable Package MFC Security Update"  is the one you want)

Best regards,
Tony

411
Help and Support / Re: Limit on array length
« on: September 30, 2013, 04:23:25 PM »
Hi,

this is a limitation of Excel's COM API unfortunately, see kb 177991:
http://support.microsoft.com/kb/177991

If you are passing around sparse matrices then maybe an array isn't the best representation. Depending on what it is you're trying to do in VBA perhaps you could get away with keeping the matrices entirely on the python side and keeping a map of handles to arrays that you could use to refer to them in VBA?

Best regards,
Tony

412
Help and Support / Re: Custom Task Pane in Excel
« on: September 17, 2013, 09:28:44 PM »
Hi,

you've got quite a few options as there are plenty of windowing toolkits for Python that you can use. I know that both wx and qt have both been used to write controls in Excel using PyXLL.

From your UI you can call back into Excel using win32com as shown in the automation examples.

If you want to use a .NET CustomTaskPane you could even use PythonNet (http://pythonnet.sourceforge.net). You can import the clr module from that project and use it to create instances of controls and manipulate them from python within Excel.

Currently my personal preferred method for tasks like this is to use the Qt Python bindings PySide (https://pypi.python.org/pypi/PySide). The following code shows how to create a Qt application and make sure the events are processed periodically as part of the main windows message loop:

Code: [Select]
from PySide import QtGui, QtCore
import timer

def get_qt_app():
    app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QtGui.QApplication([])
        processing_events = {}
        def qt_timer_callback(timer_id, time):
            if timer_id in processing_events:
                return
            processing_events[timer_id] = True
            try:
                app = QtCore.QCoreApplication.instance()
                if app is not None:
                    app.processEvents(QtCore.QEventLoop.AllEvents, 300)
            finally:
                del processing_events[timer_id]

        timer.set_timer(100, qt_timer_callback)

    return app

Once the Qt app is created and running as part of the normal windows message loop you can create qt windows and dialogs as in a normal Qt application. You are probably best off sticking with modal dialogs for simplicity (see QDialog.setModal in the PySide docs).

Best regards,
Tony


413
Help and Support / Re: Limitations on doc strings and help file format
« on: September 17, 2013, 10:03:43 AM »
Hi,

yes, those string length limitations are set by Excel so there is no way to use longer strings. It would be nicer if PyXLL would check for that and log a warning and truncate the string rather than fail to register the function though, so I will look into that.

The help file should be a .chm or .hlp file. It can either be a filepath or a url. You must also include a context id as an integer after the file separated with by a "!". The final string must be in the form "filePath!HelpContextId". Apologies that this isn't explained in the docs. This argument is passed directly to the Excel C Api function "xlfRegister" which you can read about here:
http://msdn.microsoft.com/en-us/library/office/bb687900.aspx

Best regards,
Tony

414
Help and Support / Re: Transferring an Excel range to an array of c_doubles
« on: September 17, 2013, 09:54:17 AM »
Hi,

you can use a numpy_array instead of float[] and examine it's shape (shape is a property of numpy.ndarray) and then use the slice operator to take either the first row or the first column.

I think you can use the tag [ code][/ code] (without the spaces) to post code, but I have to confess I've not tried it.

Best regards,
Tony

415
Help and Support / Re: Error running pyxll with Parallel Python
« on: September 13, 2013, 08:13:21 AM »
Hi,

I'm not familiar with PaPy to be honest, but I imagine what's happening is that it's trying to launch a child python process to run the job server and is using sys.executable to determine what command to use to run python.

Because pyxll is running in excel and not as its own python process, sys.executable will be excel.exe and not python.exe. PyXLL doesn't use python.exe at all, and so doesn't know where it is (it only uses the python dll).

Try setting sys.executable to the python.exe you want the child process to run as (full path, eg c:/python33/python.exe) before calling PaPy.

Best regards,
Tony

416
Help and Support / Re: Returning a float array or error string
« on: September 13, 2013, 08:08:08 AM »
Hi,

great :) Yes, converting between numpy arrays or basic types (float, int, string) will be faster than using var, but unless you're returning very large arrays I don't think it's something to worry about to be honest.

best regards,
Tony

417
Help and Support / Re: Returning a float array or error string
« on: September 12, 2013, 01:54:32 PM »
Hi,

the numpy_row type expects the elements in the array to be floats, which is why I expect you're having this problem. If you check the log file you should see the exception along with the stack trace.

You could change the type to var and return a 2d array or a string depending on whether there was an error or not, i.e: something like this:

   @xl_func("int uid, int node: var") ## return type changed to var
   def py_GetNodeXYZ(uid, node):
       """Get node coordinates """
# Create empty array of c_doubles required by the API function
            XYZType = ctypes.c_double * 3
            XYZ = XYZType()
# Call API function
       ierr = St7GetNodeXYZ(uid, node, XYZ)
# If ierr <> 0 return an error string
       if ierr <> 0:
      err = py_ErrString(ierr)  # this returns an error message to err
      return err ## return a string
       else:
          return array([XYZ[0:3]])  ## return a 2d array

best regards,
Tony

418
Help and Support / Re: Using coo_matrix
« on: September 05, 2013, 09:59:33 AM »
Hi,

that's probably better asked on a scipy forum as I'm not an expert, but from a general python perspective two functions with the same name imported from different modules are different functions, and so you would have to call whichever one you want. It's possible that scipy determines the most appropriate method internally, but I don't know - you'd have to refer to the scipy docs or ask elsewhere to find that out.

Best regards,
Tony

419
Help and Support / Re: Stepping through code
« on: September 04, 2013, 08:51:17 AM »
Hi,

yes. You need a python debugger that supports connecting to a remote python process. You simply connect to your python code in Excel with the debugger as you would to debug remotely any normal python process.

One debugger than can do this is the eclipse plugin pydev. There's an example included with pyxll that shows how to attach to the remote debugger in pydev from pyxll (see 'attach_to_pydev' in http://www.pyxll.com/docs/examples/developertools.html).

If you don't want to use eclipse another alternative it winpdb (http://winpdb.org/download/ and see http://winpdb.org/docs/embedded-debugging/ for how to attach to the debugger from your python code - which you could put in a menu function).

Best regards,
Tony

420
Help and Support / Re: Using coo_matrix
« on: September 04, 2013, 08:47:00 AM »
Hi,

I'm not familiar with coo_matrix I'm afraid, but looking at the docs it seems like i and j should be vectors rather than 2d arrays? The numpy_array type is always a 2d array, even if only a single column or row is passed in. You can use the types numpy_row and numpy_column to pass 1d vectors (see http://www.pyxll.com/docs/udfs.html#using-numpy-arrays). If you want to automatically detect is a matrix only has one row or column and convert it to a vector you can use a custom type (see http://www.pyxll.com/docs/examples/customtypes.html).

You should also look at the log file as any exceptions throws will be output there, along with the stack trace. That should help you figure out what's not working.

Best regards,
Tony

Pages: 1 ... 26 27 [28] 29 30 ... 39