Example #1
0
        public Estimator <TTupleInShape, TTupleNewOutShape, ITransformer> Append <[IsShape] TTupleNewOutShape>(Func <TTupleOutShape, TTupleNewOutShape> mapper)
        {
            Contracts.CheckValue(mapper, nameof(mapper));

            using (var ch = Env.Start(nameof(Append)))
            {
                var method = mapper.Method;

                // Construct the dummy column structure, then apply the mapping.
                var input = StaticPipeInternalUtils.MakeAnalysisInstance <TTupleOutShape>(out var fakeReconciler);
                KeyValuePair <string, PipelineColumn>[] inPairs = StaticPipeInternalUtils.GetNamesValues(input, method.GetParameters()[0]);

                // Initially we suppose we've only assigned names to the inputs.
                var inputColToName = new Dictionary <PipelineColumn, string>();
                foreach (var p in inPairs)
                {
                    inputColToName[p.Value] = p.Key;
                }
                string NameMap(PipelineColumn col)
                {
                    inputColToName.TryGetValue(col, out var val);
                    return(val);
                }

                var readerEst = StaticPipeUtils.GeneralFunctionAnalyzer(Env, ch, input, fakeReconciler, mapper, out var estTail, NameMap);
                ch.Assert(readerEst == null);
                ch.AssertValue(estTail);

                var est      = AsDynamic.Append(estTail);
                var newOut   = StaticSchemaShape.Make <TTupleNewOutShape>(method.ReturnParameter);
                var toReturn = new Estimator <TTupleInShape, TTupleNewOutShape, ITransformer>(Env, est, _inShape, newOut);
                ch.Done();
                return(toReturn);
            }
        }
        public static DataReader <TIn, T> AssertStatic <TIn, [IsShape] T>(this IDataReader <TIn> reader, IHostEnvironment env,
                                                                          Func <SchemaAssertionContext, T> outputDecl)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(reader, nameof(reader));
            env.CheckValue(outputDecl, nameof(outputDecl));

            var schema = StaticSchemaShape.Make <T>(outputDecl.Method.ReturnParameter);

            return(new DataReader <TIn, T>(env, reader, schema));
        }
        /// <summary>
        /// Asserts that a given data view has the indicated schema. If this method returns without
        /// throwing then the view has been validated to have columns with the indicated names and types.
        /// </summary>
        /// <typeparam name="T">The type representing the view's schema shape</typeparam>
        /// <param name="view">The view to assert the static schema on</param>
        /// <param name="env">The host environment to keep in the statically typed variant</param>
        /// <param name="outputDecl">The delegate through which we declare the schema, which ought to
        /// use the input <see cref="SchemaAssertionContext"/> to declare a <see cref="ValueTuple"/>
        /// of the <see cref="PipelineColumn"/> indices, properly named</param>
        /// <returns>A statically typed wrapping of the input view</returns>
        public static DataView <T> AssertStatic <[IsShape] T>(this IDataView view, IHostEnvironment env,
                                                              Func <SchemaAssertionContext, T> outputDecl)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(view, nameof(view));
            env.CheckValue(outputDecl, nameof(outputDecl));

            // We don't actually need to call the method, it's just there to give the declaration.
#if DEBUG
            outputDecl(SchemaAssertionContext.Inst);
#endif

            var schema = StaticSchemaShape.Make <T>(outputDecl.Method.ReturnParameter);
            return(new DataView <T>(env, view, schema));
        }
        public static Estimator <TIn, TOut, TTrans> AssertStatic <[IsShape] TIn, [IsShape] TOut, TTrans>(
            this IEstimator <TTrans> estimator, IHostEnvironment env,
            Func <SchemaAssertionContext, TIn> inputDecl,
            Func <SchemaAssertionContext, TOut> outputDecl)
            where TTrans : class, ITransformer
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(estimator, nameof(estimator));
            env.CheckValue(inputDecl, nameof(inputDecl));
            env.CheckValue(outputDecl, nameof(outputDecl));

            var inSchema  = StaticSchemaShape.Make <TIn>(inputDecl.Method.ReturnParameter);
            var outSchema = StaticSchemaShape.Make <TOut>(outputDecl.Method.ReturnParameter);

            return(new Estimator <TIn, TOut, TTrans>(env, estimator, inSchema, outSchema));
        }