Esempio n. 1
0
        internal static ExecutionReport Export(string fileName, FunctionOutput <Output <int, Peak, PeakData> > results, string header, string separator = "\t")
        {
            int intervalCount = 0;

            _stopWatch.Restart();
            if (!File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            using (File.Create(fileName)) { }
            using (var writter = new StreamWriter(fileName))
            {
                writter.WriteLine(header);
                foreach (var chr in results.Chrs)
                {
                    foreach (var strand in chr.Value)
                    {
                        foreach (var interval in strand.Value)
                        {
                            writter.WriteLine(
                                chr.Key + separator +
                                interval.interval.left.ToString() + separator +
                                interval.interval.right.ToString() + separator +
                                interval.count.ToString() + separator +
                                strand.Key);
                            intervalCount++;
                        }
                    }
                }
            }

            _stopWatch.Stop();
            return(new ExecutionReport(intervalCount, _stopWatch.Elapsed));
        }
Esempio n. 2
0
        public FunctionOutput<string>[] FastReplace(Excel.Range com, DAG dag, InputSample original, InputSample sample, AST.Address[] outputs, bool replace_original)
        {
            FunctionOutput<string>[] fo_arr;
            if (!_d.TryGetValue(sample, out fo_arr))
            {
                // replace the COM value
                ReplaceExcelRange(com, sample);

                // initialize array
                fo_arr = new FunctionOutput<string>[outputs.Length];

                // grab all outputs
                for (var k = 0; k < outputs.Length; k++)
                {
                    // save the output
                    fo_arr[k] = new FunctionOutput<string>(dag.readCOMValueAtAddress(outputs[k]), sample.GetExcludes());
                }

                // Add function values to cache
                // Don't care about return value
                _d.Add(sample, fo_arr);

                // restore the COM value
                if (replace_original)
                {
                    ReplaceExcelRange(com, original);
                }
            }
            return fo_arr;
        }
Esempio n. 3
0
        public bool Cover(string[] args)
        {
            if (args.Length < 6)
            {
                Herald.Announce(Herald.MessageType.Error, string.Format("Missing parameter."));
                return(false);
            }

            char   strand;
            int    minAcc, maxAcc;
            string coverOrSummit = args[0].ToLower();

            string resultFile = "";

            if (!ExtractResultsFile(args[1], out resultFile))
            {
                return(false);                                              // invalid file URI.
            }
            if (!char.TryParse(args[2], out strand))
            {
                Herald.Announce(Herald.MessageType.Error, string.Format("Invalid strand parameter."));
                return(false);
            }
            if (!int.TryParse(args[3], out minAcc))
            {
                Herald.Announce(Herald.MessageType.Error, string.Format("Invalid minimum accumulation parameter."));
                return(false);
            }
            if (!int.TryParse(args[4], out maxAcc))
            {
                Herald.Announce(Herald.MessageType.Error, string.Format("Invalid maximum accumulation parameter."));
                return(false);
            }

            Aggregate agg = Aggregate.Count;

            if (!String2Aggregate(args[5], out agg))
            {
                return(false);
            }

            FunctionOutput <Output <int, Peak, PeakData> > result = null;

            switch (coverOrSummit)
            {
            case "cover":
                Herald.AnnounceExeReport("Cover", di4B.Cover(CoverVariation.Cover, strand, minAcc, maxAcc, agg, out result, _maxDegreeOfParallelism), Herald.SpeedUnit.bookmarkPerSecond);
                break;

            case "summit":
                Herald.AnnounceExeReport("Summit", di4B.Cover(CoverVariation.Summit, strand, minAcc, maxAcc, agg, out result, _maxDegreeOfParallelism), Herald.SpeedUnit.bookmarkPerSecond);
                break;
            }

            Herald.AnnounceExeReport("Export", Exporter.Export(resultFile, result, "chr\tleft\tright\tcount\tstrand"));

            return(true);
        }
Esempio n. 4
0
        private FunctionOutput ProcessOutput(FunctionOutput output)
        {
            if (!IsRationalToIncludeMetaData(output))
            {
                return(output);
            }

            return(IncludeMetaData(output));
        }
Esempio n. 5
0
        // attempts to convert all of the bootstraps for FunctionOutput[function_idx, input_idx, _] to doubles
        public static FunctionOutput<double>[] ConvertToNumericOutput(FunctionOutput<string>[] boots)
        {
            var fi_boots = new FunctionOutput<double>[boots.Length];

            for (int b = 0; b < boots.Length; b++)
            {
                FunctionOutput<string> boot = boots[b];
                double value = System.Convert.ToDouble(boot.GetValue());
                fi_boots[b] = new FunctionOutput<double>(value, boot.GetExcludes());
            }
            return fi_boots;
        }
Esempio n. 6
0
        private FunctionOutput IncludeMetaData(FunctionOutput output)
        {
            output.MetaData = new Dictionary <string, object>();

            output.MetaData["resultType"] = RpcDataType.ResolveRpcDataType(output.Result);

            if (RpcService.Configuration.DebugMode)
            {
                output.MetaData["clrResultType"]       = output.Result?.GetType().FullName;
                output.MetaData["internalException"]   = internalException?.GetType().FullName;
                output.MetaData["exceptionStackTrace"] = internalException?.StackTrace;
            }

            return(output);
        }
Esempio n. 7
0
        private void UseFunctionResults(IReadOnlyList <Result> traceResults)
        {
            #region Using Function Results

            // Get the FunctionOutputResult from the trace results
            FunctionOutputResult functionOutputResult = traceResults.OfType <FunctionOutputResult>().First();

            // First() can be used here if only one Function was included in the TraceConfiguration.Functions collection.
            // Otherwise you will have to search the list for the correct FunctionOutput object.
            FunctionOutput functionOutput = functionOutputResult.FunctionOutputs.First();

            // Extract the total load from the GlobalValue property
            double totalLoad = (double)functionOutput.Value;

            #endregion
        }
Esempio n. 8
0
        private async Task <FunctionOutput> TryCallFunction(MethodInfo callMethod, object[] args)
        {
            object result;

            try
            {
                if (functionInfo.IsCallMethodAsync)
                {
                    result = await ExecuteFunctionAsync(callMethod, args);
                }
                else
                {
                    result = ExecuteFunctionSync(callMethod, args);
                }
            }
            catch (ArgumentException)
            {
                return(FunctionOutput.WithError(RpcErrorCode.InvalidArguments));
            }
            catch (TargetParameterCountException)
            {
                return(FunctionOutput.WithError(RpcErrorCode.InvalidArguments));
            }
            catch (FunctionException ex)
            {
                return(FunctionOutput.WithError(ex.Code));
            }
            catch (TargetInvocationException ex)
            {
                if (ex.InnerException is FunctionException fEx)
                {
                    return(FunctionOutput.WithError(fEx.Code));
                }

                internalException = ex.InnerException;
                return(FunctionOutput.WithError(RpcErrorCode.InternalServerError));
            }
            catch (Exception ex)
            {
                internalException = ex;
                return(FunctionOutput.WithError(RpcErrorCode.InternalServerError));
            }

            return(FunctionOutput.WithResult(result));
        }
Esempio n. 9
0
        public async Task <FunctionOutput> Post()
        {
            Prepare();

            Call = await GetFunctionCall();

            var callMethod = functionInfo.CallMethod;

            if (functionInfo.RequiresAuthorization && !ValidateAuthorizationToken())
            {
                return(FunctionOutput.WithError(RpcErrorCode.AuthorizationError));
            }

            var args = GetArguments(callMethod);

            var output = await TryCallFunction(callMethod, args);

            return(ProcessOutput(output));
        }
Esempio n. 10
0
        private void BenchmarkCover()
        {
            int tries = 10;
            var agg   = Aggregate.Count;
            FunctionOutput <Output <int, Peak, PeakData> > result = null;
            var combinations = new List <int[]>
            {
                new int[] { 1, 2 },
                new int[] { 5, 10 },
                new int[] { 10, 20 },
                new int[] { 50, 60 },
                new int[] { 80, 90 },
                new int[] { 100, 200 },
                new int[] { 200, 220 },
                new int[] { 300, 320 },
                new int[] { 400, 500 },
                new int[] { 550, 1000 },
                new int[] { 1, 1 },
                new int[] { 10, 10 },
                new int[] { 25, 25 },
                new int[] { 50, 50 },
                new int[] { 60, 60 },
                new int[] { 80, 80 },
                new int[] { 100, 100 },
                new int[] { 150, 150 },
                new int[] { 200, 200 },
                new int[] { 500, 500 },
            };

            foreach (var c in combinations)
            {
                for (int i = 0; i < tries; i++)
                {
                    Herald.AnnounceExeReport(c[0] + "," + c[1], di4B.Cover(CoverVariation.Cover, '*', c[0], c[1], agg, out result, _maxDegreeOfParallelism), Herald.SpeedUnit.bookmarkPerSecond);
                }
            }
        }
Esempio n. 11
0
 // Sort numeric bootstrap values
 public static FunctionOutput<double>[] SortBootstraps(FunctionOutput<double>[] boots)
 {
     return boots.OrderBy(b => b.GetValue()).ToArray();
 }
Esempio n. 12
0
 private async Task SendErrorOutput(HttpResponse response, RpcErrorCode code)
 {
     response.ContentType = "application/json";
     await response.WriteAsync(JsonConvert.SerializeObject(FunctionOutput.WithError(code)));
 }
Esempio n. 13
0
            public void threadPoolCallback(Object threadContext)
            {
                // perform hypothesis tests
                hypothesisTests();

                // OK to dealloc fields; this object lives on because it is
                // needed for job control
                _bs = null;
                _initial_outputs = null;
                _input = null;
                _outputs = null;

                // notify
                _mre.Set();
            }
Esempio n. 14
0
 public DataDebugJob(
     DAG dag,
     FunctionOutput<String>[][] bs,
     Dictionary<AST.Address, string> initial_outputs,
     AST.Range input,
     AST.Address[] output_arr,
     bool weighted,
     double significance,
     ManualResetEvent mre)
 {
     _dag = dag;
     _bs = bs;
     _initial_outputs = initial_outputs;
     _input = input;
     _outputs = output_arr;
     _weighted = weighted;
     _significance = significance;
     _mre = mre;
     _score = new TreeScore();
 }
Esempio n. 15
0
        public static TreeScore StringHypothesisTest(DAG dag, AST.Range rangeNode, AST.Address functionNode, FunctionOutput<string>[] boots, string initial_output, bool weighted, double significance)
        {
            // this function's input cells
            var input_cells = rangeNode.Addresses();

            // scores
            var iexc_scores = new TreeScore();

            var inputs_sz = input_cells.Count();

            // exclude each index, in turn
            for (int i = 0; i < inputs_sz; i++)
            {
                // default weight
                int weight = 1;

                // add weight to score if test fails
                AST.Address xtree = input_cells[i];
                if (weighted)
                {
                    // the weight of the function value of interest
                    weight = dag.getWeight(functionNode);
                }

                if (RejectNullHypothesis(boots, initial_output, i, significance))
                {

                    if (iexc_scores.ContainsKey(xtree))
                    {
                        iexc_scores[xtree] += weight;
                    }
                    else
                    {
                        iexc_scores.Add(xtree, weight);
                    }
                }
                else
                {
                    // we need to at least add the value to the tree
                    if (!iexc_scores.ContainsKey(xtree))
                    {
                        iexc_scores.Add(xtree, 0);
                    }
                }
            }

            return iexc_scores;
        }
Esempio n. 16
0
 public FunctionOutput Get()
 {
     return(FunctionOutput.WithError(RpcErrorCode.PreCallChecksFailed));
 }
Esempio n. 17
0
 // are all of the values numeric?
 public static bool FunctionOutputsAreNumeric(FunctionOutput<string>[] boots)
 {
     for (int i = 0; i < boots.Length; i++)
     {
         double d;
         if (!Double.TryParse(boots[i].GetValue(), out d))
         {
             return false;
         }
     };
     return true;
 }
Esempio n. 18
0
        // Exclude specified input index, compute multinomial probabilty vector, and return true if probability is below threshold
        public static bool RejectNullHypothesis(FunctionOutput<string>[] boots, string original_output, int exclude_index, double significance)
        {
            // get bootstrap fingerprint for exclude_index
            var xfp = BigInteger.One << exclude_index;

            // filter bootstraps which include exclude_index
            var boots_exc = boots.Where(b => (b.GetExcludes() & xfp) == xfp);

            // get p_value vector
            var freq = BootstrapFrequency(boots_exc);

            // what is the probability of seeing the original output?
            double p_val;
            if (!freq.TryGetValue(original_output, out p_val))
            {
                p_val = 0.0;
            }

            // test H_0
            return p_val < 1.0 - significance;
        }
Esempio n. 19
0
        public static TreeScore NumericHypothesisTest(DAG dag, AST.Range rangeNode, AST.Address functionNode, FunctionOutput<string>[] boots, string initial_output, bool weighted, double significance)
        {
            // this function's input cells
            var input_cells = rangeNode.Addresses();

            var inputs_sz = input_cells.Count();

            // scores
            var input_exclusion_scores = new TreeScore();

            // convert to numeric
            var numeric_boots = ConvertToNumericOutput(boots);

            // sort
            var sorted_num_boots = SortBootstraps(numeric_boots);

            // for each excluded index, test whether the original input
            // falls outside our bootstrap confidence bounds
            for (int i = 0; i < inputs_sz; i++)
            {
                // default weight
                int weight = 1;

                // add weight to score if test fails
                AST.Address xtree = input_cells[i];
                if (weighted)
                {
                    // the weight of the function value of interest
                    weight = dag.getWeight(functionNode);
                }

                double outlieriness = RejectNullHypothesis(sorted_num_boots, initial_output, i, significance);

                if (outlieriness != 0.0)
                {
                    // get the xth indexed input in input_rng i
                    if (input_exclusion_scores.ContainsKey(xtree))
                    {
                        input_exclusion_scores[xtree] += (int)(weight * outlieriness);
                    }
                    else
                    {
                        input_exclusion_scores.Add(xtree, (int)(weight * outlieriness));
                    }
                }
                else
                {
                    // we need to at least add the value to the tree
                    if (!input_exclusion_scores.ContainsKey(xtree))
                    {
                        input_exclusion_scores.Add(xtree, 0);
                    }
                }
            }
            return input_exclusion_scores;
        }
Esempio n. 20
0
        public static TreeScore Inference(
            int num_bootstraps,
            InputSample[][] resamples,
            Dictionary<AST.Range, InputSample> initial_inputs,
            Dictionary<AST.Address, string> initial_outputs,
            AST.Range[] input_arr,
            AST.Address[] output_arr,
            DAG dag,
            bool weighted,
            double significance,
            ProgBar pb)
        {
            // synchronization token
            object lock_token = new Object();

            // init thread event notification array
            var mres = new ManualResetEvent[input_arr.Length];

            // init job storage
            var ddjs = new DataDebugJob[input_arr.Length];

            // init started jobs count
            var sjobs = 0;

            // init completed jobs count
            var cjobs = 0;

            // last-ditch effort flag
            bool last_try = false;

            // init score storage
            var scores = new TreeScore();

            for (int i = 0; i < input_arr.Length; i++)
            {
                try
                {
                    #region BOOTSTRAP
                    // bootstrapping is done in the parent STA thread because
                    // the .NET threading model prohibits thread pools (which
                    // are MTA) from accessing STA COM objects directly.

                    // alloc bootstrap storage for each output (f), for each resample (b)
                    FunctionOutput<string>[][] bs = new FunctionOutput<string>[initial_outputs.Count][];
                    for (int f = 0; f < initial_outputs.Count; f++)
                    {
                        bs[f] = new FunctionOutput<string>[num_bootstraps];
                    }

                    // init memoization table for input vector i
                    var memo = new BootMemo();

                    // fetch the input range TreeNode
                    var input = input_arr[i];

                    // fetch the input range COM object
                    var com = dag.getCOMRefForRange(input).Range;

                    // compute outputs
                    // replace the values of the COM object with the jth bootstrap,
                    // save all function outputs, and
                    // restore the original input
                    for (var b = 0; b < num_bootstraps; b++)
                    {
                        // lookup outputs from memo table; otherwise do replacement, compute outputs, store them in table, and return them
                        FunctionOutput<string>[] fos = memo.FastReplace(com, dag, initial_inputs[input], resamples[i][b], output_arr, false);
                        for (var f = 0; f < output_arr.Length; f++)
                        {
                            bs[f][b] = fos[f];
                        }
                    }

                    // restore the original inputs; faster to do once, after bootstrapping is done
                    BootMemo.ReplaceExcelRange(com, initial_inputs[input]);

                    // TODO: restore formulas if it turns out that they were overwrittern
                    //       this should never be the case
                    #endregion BOOTSTRAP

                    #region HYPOTHESIS_TEST
                    // cancellation token
                    mres[i] = new ManualResetEvent(false);

                    // set up job
                    ddjs[i] = new DataDebugJob(
                                dag,
                                bs,
                                initial_outputs,
                                input_arr[i],
                                output_arr,
                                weighted,
                                significance,
                                mres[i]
                                );

                    sjobs++;

                    // hand job to thread pool
                    ThreadPool.QueueUserWorkItem(ddjs[i].threadPoolCallback, i);
                    #endregion HYPOTHESIS_TEST

                    // update progress bar
                    pb.IncrementProgress();
                }
                catch (System.OutOfMemoryException e)
                {
                    if (!last_try)
                    {
                        // If there are no more jobs running, but
                        // we still can't allocate memory, try invoking
                        // GC and then trying again
                        cjobs = mres.Count(mre => mre.WaitOne(0));
                        if (sjobs - cjobs == 0)
                        {
                            GC.Collect();
                            last_try = true;
                        }
                    }
                    else
                    {
                        // we just don't have enough memory
                        throw e;
                    }

                    // wait for any of the 0..i-1 work items
                    // to complete and try again
                    WaitHandle.WaitAny(mres.Take(i).ToArray());
                }
            }

            // Do not proceed until all hypothesis tests are done.
            // WaitHandle.WaitAll cannot be called on an STA thread which
            // is why we call WaitOne in a loop.
            // Merge scores as data becomes available.
            for (int i = 0; i < input_arr.Length; i++)
            {
                mres[i].WaitOne();
                scores = DictAdd(scores, ddjs[i].Result);
            }

            return scores;
        }
Esempio n. 21
0
        // Exclude a specified input index, compute quantiles, and check position of original input
        public static double RejectNullHypothesis(FunctionOutput<double>[] boots, string original_output, int exclude_index, double significance)
        {
            // low
            double low_thresh = (1.0 - significance) / 2.0;

            // high
            double hi_thresh = significance + low_thresh;

            // get bootstrap fingerprint for exclude_index
            var xfp = BigInteger.One << exclude_index;

            // filter bootstraps that include exclude_index
            var boots_exc = boots.Where(b => (b.GetExcludes() & xfp) == xfp);

            var exc_count = boots_exc.Count();

            // return neutral (0.5) if we are having a sparsity problem
            if (exc_count == 0)
            {
                return 0.5;
            }
            // index for value greater than 2.5% of the lowest values; we want to round down here
            var low_index = System.Convert.ToInt32(Math.Floor((float)(exc_count - 1) * low_thresh));
            // index for value greater than 97.5% of the lowest values; we want to round up here
            var high_index = System.Convert.ToInt32(Math.Ceiling((float)(exc_count - 1) * hi_thresh));

            var low_value = boots_exc.ElementAt(low_index).GetValue();
            var high_value = boots_exc.ElementAt(high_index).GetValue();

            var lowest_value = boots_exc.ElementAt(0).GetValue();
            var highest_value = boots_exc.ElementAt(exc_count - 1).GetValue();

            double original_output_d;
            Double.TryParse(original_output, out original_output_d);

            // truncate the values to deal with floating point imprecision
            var low_value_tr = Math.Truncate(low_value * 10000) / 10000;
            var high_value_tr = Math.Truncate(high_value * 10000) / 10000;
            var original_tr = Math.Truncate(original_output_d * 10000) / 10000;

            var lowest_value_tr = Math.Truncate(lowest_value * 10000) / 10000;
            var highest_value_tr = Math.Truncate(highest_value * 10000) / 10000;

            // reject or fail to reject H_0
            if (original_tr > high_value_tr)
            {
                if (highest_value_tr != high_value_tr)
                {
                    return Math.Abs((original_tr - high_value_tr) / Math.Abs(high_value_tr - highest_value_tr)); //normalize by the highest 2.5%
                }
                else //can't normalize
                {
                    return Math.Abs(original_tr - high_value_tr);
                }
            }
            else if (original_tr < low_value_tr)
            {
                if (lowest_value_tr != low_value_tr)
                {
                    return Math.Abs((original_tr - low_value_tr) / Math.Abs(low_value_tr - lowest_value_tr));  //normalize by the lowest 2.5%
                }
                else //can't normalize
                {
                    return Math.Abs(original_tr - low_value_tr);
                }
            }

            return 0.0;
        }
Esempio n. 22
0
 private bool IsRationalToIncludeMetaData(FunctionOutput output)
 {
     return(RpcService.Configuration.AlwaysIncludeMetadata ||
            RpcService.Configuration.DebugMode ||
            output.Result is JwtAuthorizationTicket);
 }