Search in sources :

Example 1 with PySequenceList

use of org.python.core.PySequenceList in project gda-core by openGDA.

the class GdaBuiltin method boxVarargs.

/**
 * If there are more arguments than expected, wrap all extra arguments into a single
 * array to represent the varargs argument of a Java method. The types of the extra
 * arguments are checked later so do not matter at this stage.
 * <br>
 * This is a modified version of the private function
 * {@code ReflectedArgs#ensureBNoxedVarargs}.
 * <br>
 * There is still some ambiguity if the type of the varargs is itself an iterable
 * and the final arg passed is a list. For now, this assumes that the final arg is
 * the wrapped vararg and returns it as reveived.
 *
 * @param args All the arguments passed to the function
 * @param length The number of arguments expected (including varargs array as 1)
 * @return An array of objects matching the number expected - may be the original array
 */
private PyObject[] boxVarargs(PyObject[] args, int length) {
    if (args.length == 0) {
        // if length is > 1, this will still fail later but not our problem
        return new PyObject[] { new PyList() };
    }
    PyObject lastArg = args[args.length - 1];
    if (args.length == length && lastArg instanceof PySequenceList || lastArg instanceof PyArray || lastArg instanceof PyXRange || lastArg instanceof PyIterator) {
        // will be boxed in an array once __tojava__ is called
        return args;
    }
    int positionals = length - 1;
    if (args.length < positionals) {
        return args;
    }
    var boxedArgs = new PyObject[length];
    System.arraycopy(args, 0, boxedArgs, 0, positionals);
    int others = args.length - positionals;
    var varargs = new PyObject[others];
    System.arraycopy(args, positionals, varargs, 0, others);
    boxedArgs[positionals] = new PyList(varargs);
    return boxedArgs;
}
Also used : PySequenceList(org.python.core.PySequenceList) PyXRange(org.python.core.PyXRange) PyIterator(org.python.core.PyIterator) PyArray(org.python.core.PyArray) PyList(org.python.core.PyList) PyObject(org.python.core.PyObject)

Example 2 with PySequenceList

use of org.python.core.PySequenceList in project scisoft-core by DawnScience.

the class PythonUtils method convertPySlicesToSlice.

/**
 * Convert an array of python slice objects to a slice array
 *
 * @param indexes
 * @param shape
 * @return slice array
 */
private static SliceData convertPySlicesToSlice(final PyObject indexes, final int[] shape) {
    PyObject[] indices = indexes instanceof PySequenceList ? ((PySequenceList) indexes).getArray() : new PyObject[] { indexes };
    int orank = shape.length;
    // count new axes
    int na = 0;
    // count collapsed dimensions
    int nc = 0;
    // count slices or ellipses
    int ns = 0;
    boolean hasEllipsis = false;
    for (int j = 0; j < indices.length; j++) {
        PyObject index = indices[j];
        if (index instanceof PyNone) {
            na++;
        } else if (index instanceof PyInteger) {
            nc++;
        } else if (index instanceof PySlice) {
            ns++;
        } else if (index instanceof PyEllipsis) {
            if (hasEllipsis) {
                throw new IllegalArgumentException("Only one ellipsis is allowed");
            }
            hasEllipsis = true;
            ns++;
        } else {
            throw new IllegalArgumentException("Unsupported object in index: " + index);
        }
    }
    // number of spare dimensions
    int spare = orank - nc - ns;
    SliceND slice = new SliceND(shape);
    hasEllipsis = false;
    // flag which dimensions are sliced
    boolean[] sdim = new boolean[orank];
    // new axes
    int[] axes = new int[na];
    int i = 0;
    // new axes
    int a = 0;
    // collapsed dimensions
    int c = 0;
    for (int j = 0; i < orank && j < indices.length; j++) {
        PyObject index = indices[j];
        if (index instanceof PyEllipsis) {
            sdim[i++] = true;
            if (!hasEllipsis) {
                // pad out with full slices on first ellipsis
                hasEllipsis = true;
                for (int k = 0; k < spare; k++) {
                    sdim[i++] = true;
                }
            }
        } else if (index instanceof PyInteger) {
            int n = ((PyInteger) index).getValue();
            if (n < -shape[i] || n >= shape[i]) {
                throw new PyException(Py.IndexError);
            }
            if (n < 0) {
                n += shape[i];
            }
            // nb specifying indexes whilst using slices will reduce rank
            sdim[i] = false;
            slice.setSlice(i++, n, n + 1, 1);
            c++;
        } else if (index instanceof PySlice) {
            PySlice pyslice = (PySlice) index;
            sdim[i] = true;
            Slice nslice = convertToSlice(pyslice);
            slice.setSlice(i++, nslice.getStart(), nslice.getStop(), nslice.getStep());
        } else if (index instanceof PyNone) {
            // newaxis
            axes[a++] = (hasEllipsis ? j + spare : j) - c;
        } else {
            throw new IllegalArgumentException("Unexpected item in indexing");
        }
    }
    assert nc == c;
    while (i < orank) {
        sdim[i++] = true;
    }
    while (a < na) {
        axes[a] = i - c + a;
        a++;
    }
    int[] sShape = slice.getShape();
    int[] newShape = new int[orank - nc];
    i = 0;
    for (int j = 0; i < orank; i++) {
        if (sdim[i]) {
            newShape[j++] = sShape[i];
        }
    }
    if (na > 0) {
        int[] oldShape = newShape;
        newShape = new int[newShape.length + na];
        i = 0;
        for (int k = 0, j = 0; i < newShape.length; i++) {
            if (k < na && i == axes[k]) {
                k++;
                newShape[i] = 1;
            } else {
                newShape[i] = oldShape[j++];
            }
        }
    }
    SliceData sd = new SliceData();
    sd.slice = slice;
    sd.shape = newShape;
    return sd;
}
Also used : PySequenceList(org.python.core.PySequenceList) PyInteger(org.python.core.PyInteger) SliceND(org.eclipse.january.dataset.SliceND) PyNone(org.python.core.PyNone) PyException(org.python.core.PyException) Slice(org.eclipse.january.dataset.Slice) PySlice(org.python.core.PySlice) PySlice(org.python.core.PySlice) PyObject(org.python.core.PyObject) PyEllipsis(org.python.core.PyEllipsis)

Example 3 with PySequenceList

use of org.python.core.PySequenceList in project scisoft-core by DawnScience.

the class PythonUtils method convertToJava.

/**
 * Convert tuples/lists of tuples/lists to Java lists of lists. Also convert complex numbers to Apache Commons
 * version and Jython strings to strings
 *
 * @param obj
 * @return converted object
 */
public static Object convertToJava(Object obj) {
    if (obj == null || obj instanceof PyNone)
        return null;
    if (obj instanceof PySequenceList) {
        obj = ((PySequenceList) obj).toArray();
    }
    if (obj instanceof PyArray) {
        obj = ((PyArray) obj).getArray();
    }
    if (obj instanceof List<?>) {
        @SuppressWarnings("unchecked") List<Object> jl = (List<Object>) obj;
        int l = jl.size();
        for (int i = 0; i < l; i++) {
            Object lo = jl.get(i);
            if (lo instanceof PyObject) {
                jl.set(i, convertToJava(lo));
            }
        }
        return obj;
    }
    if (obj.getClass().isArray()) {
        int l = Array.getLength(obj);
        for (int i = 0; i < l; i++) {
            Object lo = Array.get(obj, i);
            if (lo instanceof PyObject) {
                Array.set(obj, i, convertToJava(lo));
            }
        }
        return obj;
    }
    // NB PyLong gets converted to BigInteger automatically so pass it through
    if (obj instanceof BigInteger || !(obj instanceof PyObject)) {
        return obj;
    }
    if (obj instanceof PyComplex) {
        PyComplex z = (PyComplex) obj;
        return new Complex(z.real, z.imag);
    } else if (obj instanceof PyString) {
        return obj.toString();
    }
    return ((PyObject) obj).__tojava__(Object.class);
}
Also used : PySequenceList(org.python.core.PySequenceList) PyArray(org.python.core.PyArray) Complex(org.apache.commons.math3.complex.Complex) PyComplex(org.python.core.PyComplex) PyNone(org.python.core.PyNone) PyString(org.python.core.PyString) PyComplex(org.python.core.PyComplex) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List) PySequenceList(org.python.core.PySequenceList) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject)

Aggregations

PyObject (org.python.core.PyObject)3 PySequenceList (org.python.core.PySequenceList)3 PyArray (org.python.core.PyArray)2 PyNone (org.python.core.PyNone)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Complex (org.apache.commons.math3.complex.Complex)1 Slice (org.eclipse.january.dataset.Slice)1 SliceND (org.eclipse.january.dataset.SliceND)1 PyComplex (org.python.core.PyComplex)1 PyEllipsis (org.python.core.PyEllipsis)1 PyException (org.python.core.PyException)1 PyInteger (org.python.core.PyInteger)1 PyIterator (org.python.core.PyIterator)1 PyList (org.python.core.PyList)1 PySlice (org.python.core.PySlice)1 PyString (org.python.core.PyString)1 PyXRange (org.python.core.PyXRange)1