ValueMapperDispose <TRowInput, TRowOutput> GetMapperDispose() { var firstView = _sourceToReplace ?? DataViewHelper.GetFirstView(_transform); var schema = SchemaDefinition.Create(typeof(TRowOutput), SchemaDefinition.Direction.Read); var inputView = new InfiniteLoopViewCursorRow <TRowInput>(null, firstView.Schema, overwriteRowGetter: GetterSetterHelper.GetGetter <TRowInput>()); // This is extremely time consuming as the transform is serialized and deserialized. var outputView = _sourceToReplace == _transform.Source ? ApplyTransformUtils.ApplyTransformToData(_computeEnv, _transform, inputView) : ApplyTransformUtils.ApplyAllTransformsToData(_computeEnv, _transform, inputView, _sourceToReplace); // We assume all columns are needed, otherwise they should be removed. using (var cur = outputView.GetRowCursor(i => true)) { Delegate[] dels; try { dels = new TRowOutput().GetCursorGetter(cur); } catch (InvalidOperationException e) { throw new InvalidOperationException($"Unable to create getter for the schema\n{SchemaHelper.ToString(cur.Schema)}", e); } return(new ValueMapperDispose <TRowInput, TRowOutput>((in TRowInput src, ref TRowOutput dst) => { inputView.Set(in src); cur.MoveNext(); dst.Set(dels); }, new IDisposable[] { cur }));
ValueMapperDispose <TSrc, TDst> GetMapperDispose <TSrc, TDst>() { var firstView = _sourceToReplace ?? DataViewHelper.GetFirstView(_transform); var inputView = new InfiniteLoopViewCursorColumn <TSrc>(_inputIndex, firstView.Schema, ignoreOtherColumn: _ignoreOtherColumn); // This is extremely time consuming as the transform is serialized and deserialized. var outputView = _sourceToReplace == _transform.Source ? ApplyTransformUtils.ApplyTransformToData(_computeEnv, _transform, inputView) : ApplyTransformUtils.ApplyAllTransformsToData(_computeEnv, _transform, inputView, _sourceToReplace); int index; if (!outputView.Schema.TryGetColumnIndex(_outputColumn, out index)) { throw _env.Except("Unable to find column '{0}' in output schema.", _outputColumn); } int newOutputIndex = index; var cur = outputView.GetRowCursor(i => i == newOutputIndex); var getter = cur.GetGetter <TDst>(newOutputIndex); if (getter == null) { throw _env.Except("Unable to get a getter on the transform for type {0}", default(TDst).GetType()); } return(new ValueMapperDispose <TSrc, TDst>((in TSrc src, ref TDst dst) => { inputView.Set(in src); cur.MoveNext(); getter(ref dst); }, new IDisposable[] { cur }));
/// <summary> /// Create a TransformModel containing the given (optional) transforms applied to the /// given root schema. /// </summary> public TransformModel(IHostEnvironment env, Schema schemaRoot, IDataTransform[] xfs) { Contracts.CheckValue(env, nameof(env)); env.CheckValue(schemaRoot, nameof(schemaRoot)); env.CheckValueOrNull(xfs); IDataView view = new EmptyDataView(env, schemaRoot); _schemaRoot = view.Schema; if (Utils.Size(xfs) > 0) { foreach (var xf in xfs) { env.AssertValue(xf, "xfs", "Transforms should not be null"); view = ApplyTransformUtils.ApplyTransformToData(env, xf, view); } } _chain = view; }
ValueMapper <DataFrame, DataFrame> GetMapperRow() { var firstView = _sourceToReplace ?? DataViewHelper.GetFirstView(_transform); var schema = firstView.Schema; var inputView = new InfiniteLoopViewCursorDataFrame(null, firstView.Schema); // This is extremely time consuming as the transform is serialized and deserialized. var outputView = _sourceToReplace == _transform.Source ? ApplyTransformUtils.ApplyTransformToData(_computeEnv, _transform, inputView) : ApplyTransformUtils.ApplyAllTransformsToData(_computeEnv, _transform, inputView, _sourceToReplace); // We assume all columns are needed, otherwise they should be removed. using (var cur = outputView.GetRowCursor(i => true)) { var getRowFiller = DataFrame.GetRowFiller(cur); return((in DataFrame src, ref DataFrame dst) => { if (dst is null) { dst = new DataFrame(outputView.Schema, src.Length); } else if (!dst.CheckSharedSchema(outputView.Schema)) { throw _env.Except($"DataFrame does not share the same schema, expected {SchemaHelper.ToString(outputView.Schema)}."); } dst.Resize(src.Length); inputView.Set(src); for (int i = 0; i < src.Length; ++i) { cur.MoveNext(); getRowFiller(dst, i); } });