Ejemplo n.º 1
0
        private static void AddUniqueName(
            string columnName,
            VBuffer <ReadOnlyMemory <char> > slotNames,
            ref ValueListBuilder <int> nameIndices,
            ref ValueListBuilder <byte> utf8Names)
        {
            var columnNameBytes = Encoding.UTF8.GetBytes(columnName);
            var dotBytes        = Encoding.UTF8.GetBytes(".");

            foreach (var kvp in slotNames.Items(true))
            {
                // REVIEW: Add the proper number of zeros to the slot index to make them sort in the right order.
                var slotName = (!kvp.Value.IsEmpty ? kvp.Value.ToString() : kvp.Key.ToString(CultureInfo.InvariantCulture));
                if (utf8Names.Capacity - utf8Names.Length < slotName.Length * 2 + columnNameBytes.Length + dotBytes.Length)
                {
                    utf8Names.Grow();
                }
                nameIndices.Append(utf8Names.Length);
                utf8Names.AppendRange(columnNameBytes);
                utf8Names.AppendRange(dotBytes);
                var bytesNumber = Encoding.UTF8.GetBytes(slotName, 0, slotName.Length, utf8Names.Buffer, utf8Names.Length);
                utf8Names.Length += bytesNumber;
                utf8Names.Append(0);
            }
        }
Ejemplo n.º 2
0
        private static void AddUniqueName(string name,
                                          ref ValueListBuilder <int> nameIndices,
                                          ref ValueListBuilder <byte> utf8Names)
        {
            if (utf8Names.Capacity - utf8Names.Length < name.Length * 2 + 2)
            {
                utf8Names.Grow();
            }

            nameIndices.Append(utf8Names.Length);
            var bytesNumber = Encoding.UTF8.GetBytes(name, 0, name.Length, utf8Names.Buffer, utf8Names.Length);

            utf8Names.Length += bytesNumber;
            utf8Names.Append(0);
        }
Ejemplo n.º 3
0
        private static unsafe void SendViewToNativeAsCsr(IChannel ch, EnvironmentBlock *penv, IDataView view)
        {
            Contracts.AssertValue(ch);
            Contracts.Assert(penv != null);
            Contracts.AssertValue(view);
            if (penv->dataSink == null)
            {
                // Environment doesn't want any data!
                return;
            }

            var dataSink = MarshalDelegate <DataSink>(penv->dataSink);

            var schema         = view.Schema;
            var colIndices     = new List <int>();
            var outputDataKind = InternalDataKind.R4;

            int numOutputRows = 0;
            int numOutputCols = 0;

            for (int col = 0; col < schema.Count; col++)
            {
                if (schema[col].IsHidden)
                {
                    continue;
                }

                var fullType   = schema[col].Type;
                var itemType   = fullType.GetItemType();
                int valueCount = fullType.GetValueCount();

                if (valueCount == 0)
                {
                    throw ch.ExceptNotSupp("Column has variable length vector: " +
                                           schema[col].Name + ". Not supported in python. Drop column before sending to Python");
                }

                if (itemType.IsStandardScalar())
                {
                    switch (itemType.GetRawKind())
                    {
                    default:
                        throw Contracts.Except("Data type {0} not supported", itemType.GetRawKind());

                    case InternalDataKind.I1:
                    case InternalDataKind.I2:
                    case InternalDataKind.U1:
                    case InternalDataKind.U2:
                    case InternalDataKind.R4:
                        break;

                    case InternalDataKind.I4:
                    case InternalDataKind.U4:
                    case InternalDataKind.I8:
                    case InternalDataKind.R8:
                        outputDataKind = InternalDataKind.R8;
                        break;
                    }
                }
                else
                {
                    throw Contracts.Except("Data type {0} not supported", itemType.GetRawKind());
                }

                colIndices.Add(col);
                numOutputCols += valueCount;
            }

            var nameIndices   = new ValueListBuilder <int>(10);
            var nameUtf8Bytes = new ValueListBuilder <byte>(100);

            AddUniqueName("data", ref nameIndices, ref nameUtf8Bytes);
            AddUniqueName("indices", ref nameIndices, ref nameUtf8Bytes);
            AddUniqueName("indptr", ref nameIndices, ref nameUtf8Bytes);
            AddUniqueName("shape", ref nameIndices, ref nameUtf8Bytes);

            var kindList = new List <InternalDataKind> {
                outputDataKind,
                InternalDataKind.I4,
                InternalDataKind.I4,
                InternalDataKind.I4
            };

            var valueCounts = new List <byte> {
                1, 1, 1, 1
            };

            var kinds            = kindList.ToArray();
            var nameBytes        = nameUtf8Bytes.AsSpan();
            var names            = new byte *[nameIndices.Length];
            var valueCountsBytes = valueCounts.ToArray();

            fixed(InternalDataKind *prgkind = kinds)
            fixed(byte *prgbNames      = nameBytes)
            fixed(byte **prgname       = names)
            fixed(byte *prgbValueCount = valueCountsBytes)
            {
                for (int iid = 0; iid < names.Length; iid++)
                {
                    names[iid] = prgbNames + nameIndices[iid];
                }

                DataViewBlock block;

                block.ccol        = nameIndices.Length;
                block.crow        = view.GetRowCount() ?? 0;
                block.names       = (sbyte **)prgname;
                block.kinds       = prgkind;
                block.keyCards    = null;
                block.valueCounts = prgbValueCount;

                dataSink(penv, &block, out var setters, out var keyValueSetter);

                if (setters == null)
                {
                    return;
                }

                using (var cursor = view.GetRowCursor(view.Schema.Where(col => colIndices.Contains(col.Index))))
                {
                    CsrData csrData = new CsrData(penv, setters, outputDataKind);
                    var     fillers = new CsrFillerBase[colIndices.Count];

                    for (int i = 0; i < colIndices.Count; i++)
                    {
                        var type = schema[colIndices[i]].Type;
                        fillers[i] = CsrFillerBase.Create(penv, cursor, colIndices[i], type, outputDataKind, csrData);
                    }

                    for (;; numOutputRows++)
                    {
                        if (!cursor.MoveNext())
                        {
                            break;
                        }

                        for (int i = 0; i < fillers.Length; i++)
                        {
                            fillers[i].Set();
                        }

                        csrData.IncrementRow();
                    }

                    csrData.SetShape(numOutputRows, numOutputCols);
                }
            }
        }
Ejemplo n.º 4
0
        private static unsafe void SendViewToNativeAsDataFrame(IChannel ch, EnvironmentBlock *penv, IDataView view, Dictionary <string, ColumnMetadataInfo> infos = null)
        {
            Contracts.AssertValue(ch);
            Contracts.Assert(penv != null);
            Contracts.AssertValue(view);
            Contracts.AssertValueOrNull(infos);
            if (penv->dataSink == null)
            {
                // Environment doesn't want any data!
                return;
            }

            var dataSink = MarshalDelegate <DataSink>(penv->dataSink);

            var schema        = view.Schema;
            var colIndices    = new List <int>(1000);
            var kindList      = new ValueListBuilder <InternalDataKind>(INDICES_BUFFER_SIZE);
            var keyCardList   = new ValueListBuilder <int>(INDICES_BUFFER_SIZE);
            var nameUtf8Bytes = new ValueListBuilder <byte>(UTF8_BUFFER_SIZE);
            var nameIndices   = new ValueListBuilder <int>(INDICES_BUFFER_SIZE);
            var expandCols    = new HashSet <int>(1000);
            var valueCounts   = new List <byte>(1000);

            for (int col = 0; col < schema.Count; col++)
            {
                if (schema[col].IsHidden)
                {
                    continue;
                }

                var fullType = schema[col].Type;
                var itemType = fullType.GetItemType();
                var name     = schema[col].Name;

                var kind = itemType.GetRawKind();
                int keyCard;

                byte valueCount = (fullType.GetValueCount() == 0) ? (byte)0 : (byte)1;

                if (itemType is KeyDataViewType)
                {
                    // Key types are returned as their signed counterparts in Python, so that -1 can be the missing value.
                    // For U1 and U2 kinds, we convert to a larger type to prevent overflow. For U4 and U8 kinds, we convert
                    // to I4 if the key count is known (since KeyCount is an I4), and to I8 otherwise.
                    switch (kind)
                    {
                    case InternalDataKind.U1:
                        kind = InternalDataKind.I2;
                        break;

                    case InternalDataKind.U2:
                        kind = InternalDataKind.I4;
                        break;

                    case InternalDataKind.U4:
                        // We convert known-cardinality U4 key types to I4.
                        kind = itemType.GetKeyCount() > 0 ? InternalDataKind.I4 : InternalDataKind.I8;
                        break;

                    case InternalDataKind.U8:
                        // We convert known-cardinality U8 key types to I4.
                        kind = itemType.GetKeyCount() > 0 ? InternalDataKind.I4 : InternalDataKind.I8;
                        break;
                    }

                    keyCard = itemType.GetKeyCountAsInt32();
                    if (!schema[col].HasKeyValues())
                    {
                        keyCard = -1;
                    }
                }
                else if (itemType.IsStandardScalar())
                {
                    switch (itemType.GetRawKind())
                    {
                    default:
                        throw Contracts.Except("Data type {0} not handled", itemType.GetRawKind());

                    case InternalDataKind.I1:
                    case InternalDataKind.I2:
                    case InternalDataKind.I4:
                    case InternalDataKind.I8:
                    case InternalDataKind.U1:
                    case InternalDataKind.U2:
                    case InternalDataKind.U4:
                    case InternalDataKind.U8:
                    case InternalDataKind.R4:
                    case InternalDataKind.R8:
                    case InternalDataKind.BL:
                    case InternalDataKind.TX:
                    case InternalDataKind.DT:
                        break;
                    }
                    keyCard = -1;
                }
                else
                {
                    throw Contracts.Except("Data type {0} not handled", itemType.GetRawKind());
                }

                int nSlots;
                ColumnMetadataInfo info;
                if (infos != null && infos.TryGetValue(name, out info) && info.Expand)
                {
                    expandCols.Add(col);
                    Contracts.Assert(fullType.IsKnownSizeVector());
                    nSlots = fullType.GetVectorSize();
                    if (info.SlotNames != null)
                    {
                        Contracts.Assert(info.SlotNames.Length == nSlots);
                        for (int i = 0; i < nSlots; i++)
                        {
                            AddUniqueName(info.SlotNames[i], ref nameIndices, ref nameUtf8Bytes);
                        }
                    }
                    else if (schema[col].HasSlotNames(nSlots))
                    {
                        var romNames = default(VBuffer <ReadOnlyMemory <char> >);
                        schema[col].Annotations.GetValue(AnnotationUtils.Kinds.SlotNames, ref romNames);
                        AddUniqueName(name, romNames, ref nameIndices, ref nameUtf8Bytes);
                    }
                    else
                    {
                        for (int i = 0; i < nSlots; i++)
                        {
                            AddUniqueName(name + "." + i, ref nameIndices, ref nameUtf8Bytes);
                        }
                    }
                }
                else
                {
                    nSlots = 1;
                    AddUniqueName(name, ref nameIndices, ref nameUtf8Bytes);
                }

                colIndices.Add(col);
                for (int i = 0; i < nSlots; i++)
                {
                    kindList.Append(kind);
                    keyCardList.Append(keyCard);
                    valueCounts.Add(valueCount);
                }
            }

            ch.Assert(kindList.Length == keyCardList.Length);
            ch.Assert(kindList.Length == nameIndices.Length);

            var kinds            = kindList.AsSpan();
            var keyCards         = keyCardList.AsSpan();
            var nameBytes        = nameUtf8Bytes.AsSpan();
            var names            = new byte *[nameIndices.Length];
            var valueCountsBytes = valueCounts.ToArray();

            fixed(InternalDataKind *prgkind = kinds)
            fixed(byte *prgbNames      = nameBytes)
            fixed(byte **prgname       = names)
            fixed(int *prgkeyCard      = keyCards)
            fixed(byte *prgbValueCount = valueCountsBytes)
            {
                for (int iid = 0; iid < names.Length; iid++)
                {
                    names[iid] = prgbNames + nameIndices[iid];
                }

                DataViewBlock block;

                block.ccol        = nameIndices.Length;
                block.crow        = view.GetRowCount() ?? 0;
                block.names       = (sbyte **)prgname;
                block.kinds       = prgkind;
                block.keyCards    = prgkeyCard;
                block.valueCounts = prgbValueCount;

                dataSink(penv, &block, out var setters, out var keyValueSetter);

                if (setters == null)
                {
                    // REVIEW: What should we do?
                    return;
                }
                ch.Assert(keyValueSetter != null);
                var kvSet = MarshalDelegate <KeyValueSetter>(keyValueSetter);

                using (var cursor = view.GetRowCursor(view.Schema.Where(col => colIndices.Contains(col.Index))))
                {
                    var fillers  = new BufferFillerBase[colIndices.Count];
                    var pyColumn = 0;
                    var keyIndex = 0;
                    for (int i = 0; i < colIndices.Count; i++)
                    {
                        var type     = schema[colIndices[i]].Type;
                        var itemType = type.GetItemType();
                        if ((itemType is KeyDataViewType) && schema[colIndices[i]].HasKeyValues())
                        {
                            ch.Assert(schema[colIndices[i]].HasKeyValues());
                            var keyValues = default(VBuffer <ReadOnlyMemory <char> >);
                            schema[colIndices[i]].Annotations.GetValue(AnnotationUtils.Kinds.KeyValues, ref keyValues);
                            for (int slot = 0; slot < type.GetValueCount(); slot++)
                            {
                                foreach (var kvp in keyValues.Items())
                                {
                                    if (kvp.Value.IsEmpty)
                                    {
                                        kvSet(penv, keyIndex, kvp.Key, null, 0);
                                    }
                                    else
                                    {
                                        byte[] bt = Encoding.UTF8.GetBytes(kvp.Value.ToString());

                                        fixed(byte *pt = bt)
                                        kvSet(penv, keyIndex, kvp.Key, (sbyte *)pt, bt.Length);
                                    }
                                }
                                keyIndex++;
                            }
                        }
                        fillers[i] = BufferFillerBase.Create(penv, cursor, pyColumn, colIndices[i], prgkind[pyColumn], type, setters[pyColumn]);

                        if ((type is VectorDataViewType) && (type.GetVectorSize() > 0))
                        {
                            pyColumn += type.GetVectorSize();
                        }
                        else
                        {
                            pyColumn++;
                        }
                    }
                    for (int crow = 0; ; crow++)
                    {
                        // Advance to the next row.
                        if (!cursor.MoveNext())
                        {
                            break;
                        }

                        // Fill values for the current row.
                        for (int i = 0; i < fillers.Length; i++)
                        {
                            fillers[i].Set();
                        }
                    }
                }
            }
        }