Beispiel #1
0
        internal static NpyArray_Descr NpyArray_SmallType(NpyArray_Descr chktype, NpyArray_Descr mintype)
        {
            NpyArray_Descr outtype;
            NPY_TYPES      outtype_num;
            NPY_TYPES      save_num;

            Debug.Assert(Validate(chktype) && Validate(mintype));

            if (NpyArray_EquivTypes(chktype, mintype))
            {
                Npy_INCREF(mintype);
                return(mintype);
            }


            if (chktype.type_num > mintype.type_num)
            {
                outtype_num = chktype.type_num;
            }
            else
            {
                if (NpyTypeNum_ISOBJECT(chktype.type_num) &&
                    NpyDataType_ISSTRING(mintype))
                {
                    return(NpyArray_DescrFromType(NPY_TYPES.NPY_OBJECT));
                }
                else
                {
                    outtype_num = mintype.type_num;
                }
            }

            save_num = outtype_num;
            while (outtype_num < NPY_TYPES.NPY_NTYPES &&
                   !(NpyArray_CanCastSafely(chktype.type_num, outtype_num) &&
                     NpyArray_CanCastSafely(mintype.type_num, outtype_num)))
            {
                outtype_num++;
            }
            if (outtype_num == NPY_TYPES.NPY_NTYPES)
            {
                outtype = NpyArray_DescrFromType(save_num);
            }
            else
            {
                outtype = NpyArray_DescrFromType(outtype_num);
            }
            if (NpyTypeNum_ISEXTENDED(outtype.type_num))
            {
                int testsize = outtype.elsize;
                int chksize, minsize;
                chksize = chktype.elsize;
                minsize = mintype.elsize;

                /*
                 * Handle string.unicode case separately
                 * because string itemsize is 4* as large
                 */
                if (outtype.type_num == NPY_TYPES.NPY_UNICODE &&
                    mintype.type_num == NPY_TYPES.NPY_STRING)
                {
                    testsize = Math.Max(chksize, 4 * minsize);
                }
                else if (chktype.type_num == NPY_TYPES.NPY_STRING &&
                         mintype.type_num == NPY_TYPES.NPY_UNICODE)
                {
                    testsize = Math.Max(chksize * 4, minsize);
                }
                else
                {
                    testsize = Math.Max(chksize, minsize);
                }
                if (testsize != outtype.elsize)
                {
                    NpyArray_DESCR_REPLACE(ref outtype);
                    outtype.elsize = testsize;
                    NpyArray_DescrDeallocNamesAndFields(outtype);
                }
            }
            return(outtype);
        }
Beispiel #2
0
 internal static int NpyArray_SetDescr(NpyArray self, NpyArray_Descr newtype)
 {
     return(numpyinternal.NpyArray_SetDescr(self, newtype));
 }
Beispiel #3
0
 internal static NpyArray NpyArray_CastToType(NpyArray mp, NpyArray_Descr at, bool fortran)
 {
     return(numpyinternal.NpyArray_CastToType(mp, at, fortran));
 }
Beispiel #4
0
 internal static int NpyArrayAccess_AddField(NpyDict fields, List <string> names, int i, string name, NpyArray_Descr fieldType, int offset, string title)
 {
     return(numpyinternal.NpyArrayAccess_AddField(fields, names, i, name, fieldType, offset, title));
 }
Beispiel #5
0
 internal static int NpyArrayAccess_GetFieldOffset(NpyArray_Descr descr, string fieldName, ref NpyArray_Descr pDescr)
 {
     return(numpyinternal.NpyArrayAccess_GetFieldOffset(descr, fieldName, ref pDescr));
 }
Beispiel #6
0
 internal static int NpyArray_RegisterCanCast(NpyArray_Descr descr, NPY_TYPES totype, NPY_SCALARKIND scalar)
 {
     return(numpyinternal.NpyArray_RegisterCanCast(descr, totype, scalar));
 }
Beispiel #7
0
 internal static NpyArray NpyArrayAccess_FromStream(Stream fileStream, NpyArray_Descr dtype, int count, string sep)
 {
     return(numpyinternal.NpyArrayAccess_FromStream(fileStream, dtype, count, sep));
 }
Beispiel #8
0
        internal static int NpyArray_SetDescr(NpyArray self, NpyArray_Descr newtype)
        {
            npy_intp newdim;
            int      index;
            string   msg = "new type not compatible with array.";

            Npy_INCREF(newtype);

            if (NpyDataType_FLAGCHK(newtype, NpyArray_Descr_Flags.NPY_ITEM_HASOBJECT) ||
                NpyDataType_FLAGCHK(newtype, NpyArray_Descr_Flags.NPY_ITEM_IS_POINTER) ||
                NpyDataType_FLAGCHK(NpyArray_DESCR(self), NpyArray_Descr_Flags.NPY_ITEM_HASOBJECT) ||
                NpyDataType_FLAGCHK(NpyArray_DESCR(self), NpyArray_Descr_Flags.NPY_ITEM_IS_POINTER))
            {
                NpyErr_SetString(npyexc_type.NpyExc_TypeError, "Cannot change data-type for object array.");
                Npy_DECREF(newtype);
                return(-1);
            }

            if (newtype.elsize == 0)
            {
                NpyErr_SetString(npyexc_type.NpyExc_TypeError, "data-type must not be 0-sized");
                Npy_DECREF(newtype);
                return(-1);
            }


            if ((newtype.elsize != NpyArray_ITEMSIZE(self)) &&
                (NpyArray_NDIM(self) == 0 || !NpyArray_ISONESEGMENT(self) || newtype.subarray != null))
            {
                goto fail;
            }
            if (NpyArray_ISCONTIGUOUS(self))
            {
                index = NpyArray_NDIM(self) - 1;
            }
            else
            {
                index = 0;
            }
            if (newtype.elsize < NpyArray_ITEMSIZE(self))
            {
                /*
                 * if it is compatible increase the size of the
                 * dimension at end (or at the front for FORTRAN)
                 */
                if (NpyArray_ITEMSIZE(self) % newtype.elsize != 0)
                {
                    goto fail;
                }
                newdim = (npy_intp)(NpyArray_ITEMSIZE(self) / newtype.elsize);
                newdim = newdim * NpyArray_DIM(self, index);
                NpyArray_DIM_Update(self, index, (int)newdim);
                NpyArray_STRIDE_Update(self, index, newtype.elsize);
            }
            else if (newtype.elsize > NpyArray_ITEMSIZE(self))
            {
                /*
                 * Determine if last (or first if FORTRAN) dimension
                 * is compatible
                 */
                newdim = (npy_intp)(NpyArray_DIM(self, index) * NpyArray_ITEMSIZE(self));
                if ((newdim % newtype.elsize) != 0)
                {
                    goto fail;
                }
                NpyArray_DIM_Update(self, index, (int)(newdim / newtype.elsize));
                NpyArray_STRIDE_Update(self, index, newtype.elsize);
            }

            /* fall through -- adjust type*/
            Npy_DECREF(NpyArray_DESCR(self));
            if (newtype.subarray != null)
            {
                /*
                 * create new array object from data and update
                 * dimensions, strides and descr from it
                 */
                NpyArray temp;

                /*
                 * We would decref newtype here.
                 * temp will steal a reference to it
                 */
                temp =
                    NpyArray_NewFromDescr(newtype, NpyArray_NDIM(self),
                                          NpyArray_DIMS(self), NpyArray_STRIDES(self),
                                          NpyArray_BYTES(self), NpyArray_FLAGS(self),
                                          true, null,
                                          null);
                if (temp == null)
                {
                    return(-1);
                }
                NpyDimMem_FREE(NpyArray_DIMS(self));
                NpyArray_DIMS_Update(self, NpyArray_DIMS(temp));
                NpyArray_NDIM_Update(self, NpyArray_NDIM(temp));
                NpyArray_STRIDES_Update(self, NpyArray_STRIDES(temp));
                newtype = NpyArray_DESCR(temp);
                Npy_INCREF(newtype);
                /* Fool deallocator not to delete these*/
                NpyArray_NDIM_Update(temp, 0);
                NpyArray_DIMS_Update(temp, null);
                Npy_DECREF(temp);
            }

            NpyArray_DESCR_Update(self, newtype);
            NpyArray_UpdateFlags(self, NPYARRAYFLAGS.NPY_UPDATE_ALL);
            return(0);

fail:
            NpyErr_SetString(npyexc_type.NpyExc_ValueError, msg);
            Npy_DECREF(newtype);
            return(-1);
        }
Beispiel #9
0
        internal static NpyArray NpyArray_FromBinaryStream(Stream fileStream, NpyArray_Descr descr, npy_intp num)
        {
            if (NpyDataType_REFCHK(descr))
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "Cannot read into object array");
                Npy_DECREF(descr);
                return(null);
            }
            if (descr.elsize == 0)
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "The elements are 0-sized.");
                Npy_DECREF(descr);
                return(null);
            }

            int index = 0;
            var data  = NpyDataMem_NEW(descr.type_num, (ulong)((fileStream.Length - fileStream.Position) / descr.elsize), false);

            using (BinaryReader sr = new BinaryReader(fileStream))
            {
                while (true)
                {
                    try
                    {
                        switch (descr.type_num)
                        {
                        case NPY_TYPES.NPY_BOOL:
                        {
                            bool[] bdata = data.datap as bool[];
                            bdata[index] = sr.ReadBoolean();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_BYTE:
                        {
                            sbyte[] bdata = data.datap as sbyte[];
                            bdata[index] = sr.ReadSByte();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_UBYTE:
                        {
                            byte[] bdata = data.datap as byte[];
                            bdata[index] = sr.ReadByte();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_INT16:
                        {
                            Int16[] bdata = data.datap as Int16[];
                            bdata[index] = sr.ReadInt16();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_UINT16:
                        {
                            UInt16[] bdata = data.datap as UInt16[];
                            bdata[index] = sr.ReadUInt16();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_INT32:
                        {
                            Int32[] bdata = data.datap as Int32[];
                            bdata[index] = sr.ReadInt32();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_UINT32:
                        {
                            UInt32[] bdata = data.datap as UInt32[];
                            bdata[index] = sr.ReadUInt32();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_INT64:
                        {
                            Int64[] bdata = data.datap as Int64[];
                            bdata[index] = sr.ReadInt64();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_UINT64:
                        {
                            UInt64[] bdata = data.datap as UInt64[];
                            bdata[index] = sr.ReadUInt32();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_FLOAT:
                        {
                            float[] bdata = data.datap as float[];
                            bdata[index] = sr.ReadSingle();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_DOUBLE:
                        {
                            double[] bdata = data.datap as double[];
                            bdata[index] = sr.ReadDouble();
                            index++;
                            break;
                        }

                        case NPY_TYPES.NPY_DECIMAL:
                        {
                            decimal[] bdata = data.datap as decimal[];
                            bdata[index] = sr.ReadDecimal();
                            index++;
                            break;
                        }
                        }
                    }
                    catch (Exception ex)
                    {
                        break;
                    }

                    if (num > 0 && num <= index)
                    {
                        break;
                    }
                }
            }


            npy_intp[] dims = new npy_intp[] { index };

            NpyArray newArr = NpyArray_NewFromDescr(descr, 1, dims, null, data, NPYARRAYFLAGS.NPY_CARRAY, false, null, null);

            return(newArr);
        }
 internal static int NpyArray_ScanFunc(FileInfo fp, object dptr, string ignore, NpyArray_Descr a)
 {
     return(2);
 }
 internal static int NpyArray_FromStrFunc(string s, object dptr, object[] endptr, NpyArray_Descr a)
 {
     return(2);
 }
Beispiel #12
0
 internal static void NpyArray_DescrSetNames(NpyArray_Descr self, List <string> nameslist)
 {
     self.names = NpyArray_DescrNamesCopy(nameslist);
 }
Beispiel #13
0
 internal static int NpyArray_DescrReplaceNames(NpyArray_Descr self, List <string> nameslist)
 {
     self.names = NpyArray_DescrNamesCopy(nameslist);
     return(1);
 }
Beispiel #14
0
        internal static NpyArray_Descr NpyArray_DescrNewByteorder(NpyArray_Descr self, char newendian)
        {
            NpyArray_Descr _new;
            char           endian;

            _new   = NpyArray_DescrNew(self);
            endian = _new.byteorder;
            if (endian != NPY_IGNORE)
            {
                if (newendian == NPY_SWAP)
                {
                    /* swap byteorder */
                    if (NpyArray_ISNBO(endian))
                    {
                        endian = NPY_OPPBYTE;
                    }
                    else
                    {
                        endian = NPY_NATBYTE;
                    }
                    _new.byteorder = endian;
                }
                else if (newendian != NPY_IGNORE)
                {
                    _new.byteorder = newendian;
                }
            }
            if (null != _new.names)
            {
                NpyDict_KVPair KVPair = new NpyDict_KVPair();
                NpyArray_Descr newdescr;
                NpyDict_Iter   pos = new NpyDict_Iter();

                NpyDict_IterInit(pos);
                while (NpyDict_IterNext(_new.fields, pos, KVPair))
                {
                    NpyArray_DescrField value = KVPair.value as NpyArray_DescrField;
                    string key = KVPair.key as string;

                    if (null != value.title && key.CompareTo(value.title) != 0)
                    {
                        continue;
                    }
                    newdescr = NpyArray_DescrNewByteorder(value.descr, newendian);
                    if (newdescr == null)
                    {
                        Npy_DECREF(_new);
                        return(null);
                    }
                    Npy_DECREF(value.descr);
                    value.descr = newdescr;
                }
            }
            if (null != _new.subarray)
            {
                NpyArray_Descr old = _new.subarray._base;
                _new.subarray._base = NpyArray_DescrNewByteorder(self.subarray._base, newendian);
                Npy_DECREF(old);
            }
            return(_new);
        }
Beispiel #15
0
 internal static bool NpyArray_RegisterDataType(NpyArray_Descr descr)
 {
     return(numpyinternal.NpyArray_RegisterDataType(descr));
 }
Beispiel #16
0
 internal static bool Validate(NpyArray_Descr descr)
 {
     return(null != descr && npy_defs.NPY_VALID_MAGIC == descr.nob_magic_number);
 }
Beispiel #17
0
 internal static int NpyArray_RegisterCastFunc(NpyArray_Descr descr, NPY_TYPES totype, NpyArray_VectorUnaryFunc castfunc)
 {
     return(numpyinternal.NpyArray_RegisterCastFunc(descr, totype, castfunc));
 }
Beispiel #18
0
 internal static NpyArray NpyArray_GetField(NpyArray self, NpyArray_Descr typed, int offset)
 {
     return(numpyinternal.NpyArray_GetField(self, typed, offset));
 }
Beispiel #19
0
 internal static NpyArray NpyArrayAccess_FromFile(string fileName, NpyArray_Descr dtype, int count, string sep)
 {
     return(numpyinternal.NpyArrayAccess_FromFile(fileName, dtype, count, sep));
 }
Beispiel #20
0
 internal static int NpyArray_SetField(NpyArray self, NpyArray_Descr dtype, int offset, NpyArray val)
 {
     return(numpyinternal.NpyArray_SetField(self, dtype, offset, val));
 }
Beispiel #21
0
 internal static void NpyArrayAccess_DescrReplaceFields(NpyArray_Descr descr, List <string> nameslist, NpyDict fields)
 {
     numpyinternal.NpyArrayAccess_DescrReplaceFields(descr, nameslist, fields);
 }
Beispiel #22
0
 internal static int NpyArray_AsCArray(ref NpyArray apIn, ref byte[] ptr, npy_intp[] dims, int nd, NpyArray_Descr typedescr)
 {
     return(numpyinternal.NpyArray_AsCArray(ref apIn, ref ptr, dims, nd, typedescr));
 }
Beispiel #23
0
 internal static int NpyArrayAccess_GetDescrField(NpyArray_Descr descr, string fieldName, ref NpyArray_DescrField pField)
 {
     return(numpyinternal.NpyArrayAccess_GetDescrField(descr, fieldName, ref pField));
 }
Beispiel #24
0
 internal static bool NpyArray_EquivTypes(NpyArray_Descr typ1, NpyArray_Descr typ2)
 {
     return(numpyinternal.NpyArray_EquivTypes(typ1, typ2));
 }
Beispiel #25
0
 internal static NpyArray NpyArray_View(NpyArray self, NpyArray_Descr type, object subtype)
 {
     return(numpyinternal.NpyArray_View(self, type, subtype));
 }
Beispiel #26
0
 internal static void NpyArray_Item_XDECREF(byte [] data, NpyArray_Descr descr)
 {
     numpyinternal.NpyArray_Item_XDECREF(data, descr);
 }
Beispiel #27
0
 internal static NpyArray_VectorUnaryFunc NpyArray_GetCastFunc(NpyArray_Descr descr, NPY_TYPES type_num)
 {
     return(numpyinternal.NpyArray_GetCastFunc(descr, type_num));
 }
Beispiel #28
0
 internal static NpyArray NpyUFunc_GenericReduction(NpyUFuncObject self, NpyArray arr, NpyArray indices,
                                                    NpyArray _out, int axis, NpyArray_Descr otype, GenericReductionOp operation)
 {
     return(numpyinternal.NpyUFunc_GenericReduction(self, arr, indices, _out, axis, otype, operation, false));
 }
Beispiel #29
0
 internal static bool NpyArray_CanCastTo(NpyArray_Descr from, NpyArray_Descr to)
 {
     return(numpyinternal.NpyArray_CanCastTo(from, to));
 }
Beispiel #30
0
 internal static bool NpyDataType_REFCHK(NpyArray_Descr dtype)
 {
     return(NpyDataType_FLAGCHK(dtype, NpyArray_Descr_Flags.NPY_ITEM_REFCOUNT));
 }