Beispiel #1
0
        private static void AssignFromSeq(IEnumerable <Object> seq, ndarray result,
                                          int dim, long offset)
        {
            if (dim >= result.ndim)
            {
                throw new RuntimeException(String.Format("Source dimensions ({0}) exceeded target array dimensions ({1}).", dim, result.ndim));
            }

            if (seq is ndarray && seq.GetType() != typeof(ndarray))
            {
                // Convert to an array to ensure the dimensionality reduction
                // assumption works.
                ndarray array = NpyCoreApi.FromArray((ndarray)seq, null, NPYARRAYFLAGS.NPY_ENSUREARRAY);
                seq = (IEnumerable <object>)array;
            }

            if (seq.Count() != result.Dim(dim))
            {
                throw new RuntimeException("AssignFromSeq: sequence/array shape mismatch.");
            }

            long stride = result.Stride(dim);

            if (dim < result.ndim - 1)
            {
                // Sequence elements should be additional sequences
                seq.Iteri((o, i) =>
                          AssignFromSeq((IEnumerable <Object>)o, result, dim + 1, offset + stride * i));
            }
            else
            {
                seq.Iteri((o, i) => result.Dtype.f.setitem(offset + i * stride, o, result.Array));
            }
        }
Beispiel #2
0
 internal static ndarray EnsureArray(this NpyArray a, object o)
 {
     if (o == null)
     {
         return(null);
     }
     if (o.GetType() == typeof(ndarray))
     {
         return((ndarray)o);
     }
     if (o is ndarray)
     {
         return(NpyCoreApi.FromArray((ndarray)o, null, NPYARRAYFLAGS.NPY_ENSUREARRAY));
     }
     return(np.FromAny(o, flags: NPYARRAYFLAGS.NPY_ENSUREARRAY));
 }
Beispiel #3
0
        private static ndarray Concatenate(IEnumerable <ndarray> arrays, int?axis = 0)
        {
            int i;

            bool MustFlatten = false;

            if (axis == null)
            {
                MustFlatten = true;
                axis        = 0;
            }

            try
            {
                arrays.First();
            }
            catch (InvalidOperationException)
            {
                throw new ArgumentException("concatenation of zero-length sequence is impossible");
            }

            ndarray[] mps = NpyUtil_ArgProcessing.ConvertToCommonType(arrays);
            int       n   = mps.Length;

            if (MustFlatten)
            {
                // Flatten the arrays
                for (i = 0; i < n; i++)
                {
                    mps[i] = mps[i].Ravel(NPY_ORDER.NPY_CORDER);
                }
                axis = 0;
            }
            else if (axis != 0)
            {
                // Swap to make the axis 0
                for (i = 0; i < n; i++)
                {
                    mps[i] = NpyCoreApi.FromArray(mps[i].SwapAxes(axis.Value, 0), null, NPYARRAYFLAGS.NPY_C_CONTIGUOUS);
                }
            }


            npy_intp[] dims = mps[0].dims;
            if (dims.Length == 0)
            {
                throw new ArgumentException("0-d arrays can't be concatenated");
            }

            npy_intp new_dim = dims[0];

            for (i = 1; i < n; i++)
            {
                npy_intp[] dims2 = mps[i].dims;
                if (dims.Length != dims2.Length)
                {
                    throw new ArgumentException("arrays must have same number of dimensions");
                }
                bool eq = Enumerable.Zip(dims.Skip(1), dims2.Skip(1), (a1, b) => (a1 == b)).All(x => x);
                if (!eq)
                {
                    throw new ArgumentException("array dimensions do not agree");
                }
                new_dim += dims2[0];
            }
            dims[0] = new_dim;


            ndarray result = NpyCoreApi.AllocArray(mps[0].Dtype, dims.Length, dims, false);

            if (NpyCoreApi.CombineInto(result, mps) < 0)
            {
                throw new ArgumentException("unable to concatenate these arrays");
            }

            if (0 < axis && axis < NpyDefs.NPY_MAXDIMS || axis < 0)
            {
                result = result.SwapAxes(axis.Value, 0);
            }


            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Constructs a new array from multiple input types, like lists, arrays, etc.
        /// </summary>
        /// <param name="src"></param>
        /// <param name="descr"></param>
        /// <param name="minDepth"></param>
        /// <param name="maxDepth"></param>
        /// <param name="requires"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        internal static ndarray FromAny(Object src, dtype descr = null, int minDepth     = 0,
                                        int maxDepth            = 0, NPYARRAYFLAGS flags = 0, Object context = null)
        {
            ndarray result = null;

            if (src == null)
            {
                return(np.empty(new shape(0), NpyCoreApi.DescrFromType(NPY_TYPES.NPY_OBJECT)));
            }

            Type t = src.GetType();

            if (t != typeof(PythonTuple))
            {
                if (src is ndarray)
                {
                    result = NpyCoreApi.FromArray((ndarray)src, descr, flags);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                if (t.IsArray)
                {
                    result = asanyarray(src);
                }

                dtype newtype = (descr ?? FindScalarType(src));
                if (descr == null && newtype != null)
                {
                    if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
                    {
                        throw UpdateIfCopyError();
                    }
                    result = FromPythonScalar(src, newtype);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                //result = FromScalar(src, descr, context);
                if (result != null)
                {
                    if (descr != null && !NpyCoreApi.EquivTypes(descr, result.Dtype) || flags != 0)
                    {
                        result = NpyCoreApi.FromArray(result, descr, flags);
                        return(FromAnyReturn(result, minDepth, maxDepth));
                    }
                }
            }

            bool is_object = false;

            if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
            {
                throw UpdateIfCopyError();
            }
            if (descr == null)
            {
                descr = FindArrayType(src, null);
            }
            else if (descr.TypeNum == NPY_TYPES.NPY_OBJECT)
            {
                is_object = true;
            }

            if (result == null)
            {
                bool seq = false;
                if (src is IEnumerable <object> )
                {
                    try
                    {
                        result = FromIEnumerable((IEnumerable <object>)src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0, minDepth, maxDepth);
                        seq    = true;
                    }
                    catch (InsufficientMemoryException)
                    {
                        throw;
                    }
                    catch
                    {
                        if (is_object)
                        {
                            result = FromNestedList(src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0);
                            seq    = true;
                        }
                    }
                }
                if (!seq)
                {
                    result = FromScalar(src, descr, null);
                }
            }
            return(FromAnyReturn(result, minDepth, maxDepth));
        }
Beispiel #5
0
        /// <summary>
        /// Constructs a new array from multiple input types, like lists, arrays, etc.
        /// </summary>
        /// <param name="src"></param>
        /// <param name="descr"></param>
        /// <param name="minDepth"></param>
        /// <param name="maxDepth"></param>
        /// <param name="requires"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static ndarray FromAny(Object src, dtype descr = null, int minDepth     = 0,
                                      int maxDepth            = 0, NPYARRAYFLAGS flags = 0, Object context = null)
        {
            ndarray result = null;

            if (src == null)
            {
                return(np.empty(new shape(0), NpyCoreApi.DescrFromType(NPY_TYPES.NPY_OBJECT)));
            }

            Type t = src.GetType();

            if (t != typeof(PythonTuple))
            {
                if (src is ndarray)
                {
                    result = NpyCoreApi.FromArray((ndarray)src, descr, flags);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                if (t.IsArray)
                {
                    result = asanyarray(src);
                }

                if (src is ScalarGeneric)
                {
                    if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
                    {
                        throw UpdateIfCopyError();
                    }
                    result = FromScalar((ScalarGeneric)src, descr);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                dtype newtype = (descr ?? FindScalarType(src));
                if (descr == null && newtype != null)
                {
                    if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
                    {
                        throw UpdateIfCopyError();
                    }
                    result = FromPythonScalar(src, newtype);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                //result = FromScalar(src, descr, context);
                if (result != null)
                {
                    if (descr != null && !NpyCoreApi.EquivTypes(descr, result.Dtype) || flags != 0)
                    {
                        result = NpyCoreApi.FromArray(result, descr, flags);
                        return(FromAnyReturn(result, minDepth, maxDepth));
                    }
                }
            }

            bool is_object = false;

            if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
            {
                throw UpdateIfCopyError();
            }
            if (descr == null)
            {
                descr = FindArrayType(src, null);
            }
            else if (descr.TypeNum == NPY_TYPES.NPY_OBJECT)
            {
                is_object = true;
            }

            if (result == null)
            {
                // Hack required because in C# strings are enumerations of chars, not objects.
                // However, we want to keep src as a string if we are building a string or object array.
                if (!is_object &&
                    (descr.TypeNum != NPY_TYPES.NPY_STRING || descr.Type == NPY_TYPECHAR.NPY_CHARLTR) &&
                    descr.TypeNum != NPY_TYPES.NPY_UNICODE && src is string && ((string)src).Length > 1)
                {
                    src = ((string)src).Cast <object>();
                }

                bool seq = false;
                if (src is IEnumerable <object> )
                {
                    try
                    {
                        result = FromIEnumerable((IEnumerable <object>)src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0, minDepth, maxDepth);
                        seq    = true;
                    }
                    catch (InsufficientMemoryException)
                    {
                        throw;
                    }
                    catch
                    {
                        if (is_object)
                        {
                            result = FromNestedList(src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0);
                            seq    = true;
                        }
                    }
                }
                if (!seq)
                {
                    result = FromScalar(src, descr, null);
                }
            }
            return(FromAnyReturn(result, minDepth, maxDepth));
        }