Ejemplo n.º 1
0
            public void UnfoldAndLabelCurvedArcLoft()
            {
                Surface testsweep = UnfoldTestUtils.SetupArcLoft();

                var surfaces = new List <Surface>()
                {
                    testsweep
                };
                //handle tesselation here
                var pointtuples = Tesselation.Tessellate(surfaces, -1, 512);
                //convert triangles to surfaces
                List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();


                var unfoldObject = PlanarUnfolder.Unfold(trisurfaces);

                var unfoldsurfaces = unfoldObject.UnfoldedSurfaceSet;

                Console.WriteLine("generating labels");

                // generate labels
                var labels = unfoldObject.StartingUnfoldableFaces.Select(x =>
                                                                         new PlanarUnfolder.UnfoldableFaceLabel <EdgeLikeEntity, FaceLikeEntity>(x)).ToList();

                UnfoldTestUtils.AssertLabelsGoodStartingLocationAndOrientation(labels);

                // next check the positions of the translated labels

                var transformedGeo = labels.Select(x => PlanarUnfolder.MapGeometryToUnfoldingByID(unfoldObject, x.AlignedLabelGeometry, x.ID)).ToList();

                UnfoldTestUtils.AssertLabelsGoodFinalLocationAndOrientation(labels, transformedGeo, unfoldObject);
            }
Ejemplo n.º 2
0
            public void UnfoldAndLabelCurvedArcLoft()
            {
                Surface testsweep = UnfoldTestUtils.SetupArcLoft();

                var surfaces = new List <Surface>()
                {
                    testsweep
                };
                //handle tesselation here
                var pointtuples = Tesselation.Tessellate(surfaces, -1, 512);
                //convert triangles to surfaces
                List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();


                var unfoldObject = PlanarUnfolder.Unfold(trisurfaces);

                Console.WriteLine("generating tabs");

                // generate tabs
                var tabDict  = TabGeneration.GenerateTabSurfacesFromUnfold <EdgeLikeEntity, FaceLikeEntity>(unfoldObject);
                var justTabs = tabDict.Keys.SelectMany(x => tabDict[x]).ToList();

                Console.WriteLine("tabs generated");
                // next check the positions of the translated tab,

                UnfoldTestUtils.AssertTabsGoodFinalLocation <EdgeLikeEntity, FaceLikeEntity>(justTabs, unfoldObject);
            }
Ejemplo n.º 3
0
            public void UnfoldCurvedArcSweep()
            {
                Surface testsweep = UnfoldTestUtils.SetupArcCurveSweep();

                var surfaces = new List <Surface>()
                {
                    testsweep
                };
                //handle tesselation here
                var pointtuples = Tesselation.Tessellate(surfaces, -1, 30);
                //convert triangles to surfaces
                List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();


                var unfoldsurfaces = PlanarUnfolder.Unfold(trisurfaces).UnfoldedSurfaceSet;

                UnfoldTestUtils.CheckAllUnfoldedFacesForCorrectUnfold(unfoldsurfaces);

                foreach (IDisposable item in trisurfaces)
                {
                    item.Dispose();
                }
            }
            public void GenBFSTreeFromArcLoft()
            {
                Surface testsweep = UnfoldTestUtils.SetupArcLoft();

                var surfaces = new List <Surface>()
                {
                    testsweep
                };
                //handle tesselation here
                var pointtuples = Tesselation.Tessellate(surfaces, -1, 512);
                //convert triangles to surfaces
                List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();


                var graph = ModelTopology.GenerateTopologyFromSurfaces(trisurfaces);

                List <Object> face_objs = trisurfaces.Select(x => x as Object).ToList();

                UnfoldTestUtils.GraphHasVertForEachFace(graph, face_objs);

                var    nodereturn = ModelGraph.BFS <EdgeLikeEntity, FaceLikeEntity>(graph);
                object tree       = nodereturn;

                var casttree = tree as List <GraphVertex <EdgeLikeEntity, FaceLikeEntity> >;

                UnfoldTestUtils.GraphHasVertForEachFace(casttree, face_objs);
                UnfoldTestUtils.AssertAllFinishingTimesSet(graph);

                var sccs = GraphUtilities.TarjansAlgo <EdgeLikeEntity, FaceLikeEntity> .CycleDetect(casttree, GraphUtilities.EdgeType.Tree);

                UnfoldTestUtils.IsAcylic <EdgeLikeEntity, FaceLikeEntity>(sccs, casttree);
            }
Ejemplo n.º 5
0
            public void UnfoldAndLabel2ArcLofts()
            {
                // unfold cube
                Surface testloft  = UnfoldTestUtils.SetupArcLoft();
                Surface testloft2 = UnfoldTestUtils.SetupArcLoft();
                var     surfaces  = new List <Surface>()
                {
                    testloft
                };
                var surfaces2 = new List <Surface>()
                {
                    testloft
                };
                //handle tesselation here
                var pointtuples  = Tesselation.Tessellate(surfaces, -1, 512);
                var pointtuples2 = Tesselation.Tessellate(surfaces, -1, 512);
                //convert triangles to surfaces
                List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();
                List <Surface> trisurfaces2 = pointtuples2.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();

                var unfoldObject1 = PlanarUnfolder.Unfold(trisurfaces);
                var unfoldObject2 = PlanarUnfolder.Unfold(trisurfaces2);

                var unfoldsurfaces = unfoldObject1.UnfoldedSurfaceSet.Concat(unfoldObject2.UnfoldedSurfaceSet).ToList();

                Console.WriteLine("merging unfolds");
                var unfoldObject = PlanarUnfolder.PlanarUnfolding <EdgeLikeEntity, FaceLikeEntity> .
                                   MergeUnfoldings(new List <PlanarUnfolder.PlanarUnfolding <EdgeLikeEntity, FaceLikeEntity> >() { unfoldObject1, unfoldObject2 });

                AssertMergeHasCorrectNumberOfSurfaces(unfoldObject, trisurfaces.Count * 2);
                AssertMergeHasCorrectNumberOfMaps(unfoldObject, new List <PlanarUnfolder.PlanarUnfolding <EdgeLikeEntity, FaceLikeEntity> >()
                {
                    unfoldObject1, unfoldObject2
                });

                Console.WriteLine("generating labels");

                // generate labels
                var labels = unfoldObject.StartingUnfoldableFaces.Select(x =>
                                                                         new PlanarUnfolder.UnfoldableFaceLabel <EdgeLikeEntity, FaceLikeEntity>(x)).ToList();

                UnfoldTestUtils.AssertLabelsGoodStartingLocationAndOrientation(labels);

                // next check the positions of the translated labels,

                var transformedGeo = labels.Select(x => PlanarUnfolder.MapGeometryToUnfoldingByID
                                                       (unfoldObject, x.AlignedLabelGeometry, x.ID)).ToList();

                UnfoldTestUtils.AssertLabelsGoodFinalLocationAndOrientation(labels, transformedGeo, unfoldObject);
            }
Ejemplo n.º 6
0
        // exposes node to tesselate surfaces in dynamo, returns triangular surfaces
        // Peter will hate this :)
        /// <summary>
        /// This method exposes the tessellation algorithm that is used to display
        /// geometry in Dynamo's watch3d and background preview displays as a node.
        /// It is possible this node and all dependent code in the unfolding library
        /// will be replaced by faster triangle representation that does not require
        /// conversion to a surface.
        /// </summary>
        /// <param name="surfaces"></param>
        /// <param name="tolerance"> tolerance between the surface and mesh representation</param>
        /// <param name="maxGridLines">maximum number of surface divisons that define one direction of the mesh </param>
        /// <returns name = "Surfaces"> a list of trimmed planar surfaces that represent triangles</returns>
        public static List <Surface> _TesselateSurfaces(List <Surface> surfaces, double tolerance = -1, int maxGridLines = 512)
        {
            var pointtuples = Tesselation.Tessellate(surfaces, tolerance, maxGridLines);
            //convert triangles to surfaces
            List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
            {
                x[0], x[1], x[2]
            })).ToList();

            return(trisurfaces);
        }
Ejemplo n.º 7
0
        public void VolatileGradientDescent()
        {
            var Config = new ConfigParams("");

            Program.Config = Config;
            var res = Program.RandomizeCoordinatesAndSave(20, true);

            var T    = new Tesselation(res.Item1.Select(v => (Coordinate)v).ToList(), res.Item2, null, Config);
            var cell = T.ComputeCellFromCoordinateVolatile(new Coordinate(0.0, 0.0));

            Console.WriteLine(cell.CenterOfMass);
        }
Ejemplo n.º 8
0
        private void SecondPass(State state)
        {
            int set         = 0;
            int index       = 0;
            var lookupShape = default(Shape <TNode, TPosition, TKey>);

            var shape = default(Shape <TNode, TPosition, TKey>);
            var node  = default(TNode);
            var tesselationSurroundEnumerable = default(IEnumerable <TPosition>);
            var tesselationSurroundEnumerator = default(IEnumerator <TPosition>);
            var neighbor = default(TPosition);

            for (int shapeIndex = state.Shapes.Count - 1; shapeIndex >= 0; shapeIndex--)
            {
                shape = state.Shapes[shapeIndex];
                for (int nodeIndex = shape.Nodes.Count - 1; nodeIndex >= 0; nodeIndex--)
                {
                    set   = 0;
                    index = 0;
                    node  = shape.Nodes[nodeIndex];

                    tesselationSurroundEnumerable = Tesselation.Surround(node.Point);
                    tesselationSurroundEnumerator = tesselationSurroundEnumerable.GetEnumerator();
                    while (tesselationSurroundEnumerator.MoveNext())
                    {
                        neighbor = tesselationSurroundEnumerator.Current;
                        if (Tesselation.Valid(neighbor))
                        {
                            lookupShape = state.GetShape(neighbor);
                        }
                        else
                        {
                            lookupShape = null;
                        }
                        set *= 2;
                        if (lookupShape != shape)
                        {
                            set++;
                        }
                        node[index++] = lookupShape != null ? lookupShape.Area : null;
                    }
                    tesselationSurroundEnumerator.Dispose();

                    if (CollisionGenerator.OuterNode(set))
                    {
                        shape.AddOuterNode(node);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Method for taking a list of surfaces,tesselating them at max tesselation level,
        /// and then unfolding them.
        /// </summary>
        /// <param name="surfaces"> the surfaces to be tesselated and unfolded</param>
        /// <returns name = "surfaces"> the unfolded surfaces </returns>
        public static List <List <Surface> > __UnfoldCurvedSurfacesByTesselation(List <Surface> surfaces)
        {
            surfaces.RemoveAll(item => item == null);
            //handle tesselation here
            var pointtuples = Tesselation.Tessellate(surfaces, -1, 512);
            //convert triangles to surfaces
            List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
            {
                x[0], x[1], x[2]
            })).ToList();

            var unfoldsurfaces = PlanarUnfolder.Unfold(trisurfaces);

            return(unfoldsurfaces.UnfoldedSurfaceSet);
        }
Ejemplo n.º 10
0
            public void UnfoldOf300TriSurface()
            {
                var surface = UnfoldTestUtils.SetupArcLoft();

                var surfaces = new List <Surface>()
                {
                    surface
                };

                //handle tesselation here
                var pointtuples = Tesselation.Tessellate(surfaces, -1, 512);
                //convert triangles to surfaces
                List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();

                Assert.DoesNotThrow(() => PlanarUnfolder.Unfold(trisurfaces));
            }
Ejemplo n.º 11
0
            public void FullyUnfoldConeTallFromTriSurfaces()
            {
                Solid          testCone = UnfoldTestUtils.SetupTallCone();
                List <Face>    faces    = testCone.Faces.ToList();
                List <Surface> surfaces = faces.Select(x => x.SurfaceGeometry()).ToList();

                //handle tesselation here
                var pointtuples = Tesselation.Tessellate(surfaces, -1, 512);
                //convert triangles to surfaces
                List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();


                var unfoldsurfaces = PlanarUnfolder.Unfold(trisurfaces).UnfoldedSurfaceSet;

                UnfoldTestUtils.CheckAllUnfoldedFacesForCorrectUnfold(unfoldsurfaces);
            }
Ejemplo n.º 12
0
        public static Dictionary <string, object> __UnfoldCurvedSurfacesByTessellation_AndReturnTransforms(List <Surface> surfaces)
        {
            surfaces.RemoveAll(item => item == null);
            //handle tesselation here
            var pointtuples = Tesselation.Tessellate(surfaces, -1, 512);
            //convert triangles to surfaces
            List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
            {
                x[0], x[1], x[2]
            })).ToList();

            var unfolding = PlanarUnfolder.Unfold(trisurfaces);

            return(new Dictionary <string, object>
            {
                { "surfaces", (unfolding.UnfoldedSurfaceSet) },
                { "unfoldingObject", (unfolding) },
            });
        }
Ejemplo n.º 13
0
    public void UpdateSculp()
    {
        GameObject terrain = GameObject.Find("Terrain");
        Sculp      sculp   = terrain.GetComponent <Terrain>().sculp;

        compute.SetFloat("sculpImpact", sculp.impact);
        compute.SetFloat("sculpArea", sculp.area);
        compute.SetFloat("sculpHeightMaxLimit", sculp.max);
        compute.SetFloat("sculpHeightMinLimit", sculp.min);
        compute.SetBool("sculpUp", sculp.up);
        editing = sculp.editing;

        Tesselation tesselation = terrain.GetComponent <Terrain>().tesselation;

        material.SetFloat("_Smooth", tesselation.DispValue);

        material.SetFloat("sculpArea", sculp.area / chunkSize);
        material.SetInt("editing", editing == true ? 1 : 0);
    }
Ejemplo n.º 14
0
            public void UnfoldEachPairOfTriangularSurfacesInAConeWideParentAsRefFace()
            {
                Solid          testCone = UnfoldTestUtils.SetupLargeCone();
                List <Face>    faces    = testCone.Faces.ToList();
                List <Surface> surfaces = faces.Select(x => x.SurfaceGeometry()).ToList();

                //handle tesselation here
                var pointtuples = Tesselation.Tessellate(surfaces, -1, 512);
                //convert triangles to surfaces
                List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
                {
                    x[0], x[1], x[2]
                })).ToList();

                //generate a graph of the cube
                var graph = ModelTopology.GenerateTopologyFromSurfaces(trisurfaces);


                UnfoldTestUtils.AssertEachFacePairUnfoldsCorrectly(graph);
            }
Ejemplo n.º 15
0
        // The following methods may be removed from Import eventually
        #region explorationdebug
        // method is for debugging the BFS output visually in dynamo, very useful
        public static object __BFSTestTesselation(List <Surface> surfaces, double tolerance = -1, int maxGridLines = 512)
        {
            //handle tesselation here
            var pointtuples = Tesselation.Tessellate(surfaces, tolerance, maxGridLines);
            //convert triangles to surfaces
            List <Surface> trisurfaces = pointtuples.Select(x => Surface.ByPerimeterPoints(new List <Point>()
            {
                x[0], x[1], x[2]
            })).ToList();

            var graph = ModelTopology.GenerateTopologyFromSurfaces(trisurfaces);

            //perform BFS on the graph and get back the tree
            var nodereturn = ModelGraph.BFS <EdgeLikeEntity, FaceLikeEntity>(graph);
            var tree       = nodereturn;

            var treegeo = ModelGraph.ProduceGeometryFromGraph <EdgeLikeEntity, FaceLikeEntity>
                              (tree as List <GraphVertex <EdgeLikeEntity, FaceLikeEntity> >);


            return(treegeo);
        }
Ejemplo n.º 16
0
        private List <SpatialmHGResult> Solve2DProblem(List <Coordinate> coords, List <bool> labels, List <Coordinate3D> projectedFrom = null, PrincipalComponentAnalysis pca = null)
        {
            var T = new Tesselation(coords, labels, new List <string>(), Config)
            {
                pca = pca
            };

            if (projectedFrom != null)
            {
                T.ProjectedFrom = projectedFrom.Cast <ICoordinate>().ToList();
            }

            IEnumerable <Cell> topResults = null;

            if ((Config.ActionList & Actions.Search_CellSkipping) != 0)
            {
                topResults = T.GradientSkippingSweep(numStartCoords: 20, numThreads: Environment.ProcessorCount - 1);
                Tesselation.Reset();
                return(topResults.Select(t => new SpatialmHGResult(t)).ToList());
            }
            if ((Config.ActionList & Actions.Search_Exhaustive) != 0)
            {
                T.GenerateFromCoordinates();
            }
            if ((Config.ActionList & Actions.Search_Originals) != 0)
            {
                //mHGOnOriginalPoints(args, coordinates, labels, numcoords);
            }
            if ((Config.ActionList & Actions.Search_FixedSet) != 0)
            {
                /*
                 * var avgX = coordinates.Select(c => c.GetDimension(0)).Average();
                 * var avgY = coordinates.Select(c => c.GetDimension(1)).Average();
                 * var cord = new Coordinate(avgX, avgY);
                 * mHGOnOriginalPoints(args, coordinates, labels, numcoords, new List<ICoordinate>() { cord });
                 */
            }
            if ((Config.ActionList & Actions.Search_LineSweep) != 0)
            {
                T.LineSweep();
            }
            if ((Config.ActionList & Actions.Search_EmpricalSampling) != 0)
            {
                var problem     = coords.Zip(labels, (a, b) => new Tuple <ICoordinate, bool>(a, b)).ToList();
                var gr          = new Gridding();
                var problemSize = MathExtensions.Binomial(Line.Count, 2) + Line.Count + 1;
                gr.GenerateEmpricialDensityGrid((long)Math.Min(problemSize, 100000), problem);
                var results = new ConcurrentPriorityQueue <double, SpatialmHGResult>();
                Parallel.ForEach(gr.GetPivots(), pivot =>
                {
                    var binvec = problem.OrderBy(c => c.Item1.EuclideanDistance(pivot)).Select(c => c.Item2).ToArray();
                    var res    = mHGJumper.minimumHypergeometric(binvec);
                    results.Enqueue(res.Item1, new SpatialmHGResult(res.Item1, res.Item2, (Coordinate)pivot));
                    while (results.Count > Config.GetTopKResults)
                    {
                        results.TryDequeue(out var junk);
                    }
                });
                return(results.Select(v => v.Value).ToList());
            }

            return(null);
        }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            var options = new CommandlineParameters();
            var isValid = Parser.Default.ParseArgumentsStrict(args, options);

            //args = new[] {@"c:\Users\shaybe\Dropbox\Thesis-PHd\SpatialEnrichment\Datasets\usStatesBordersData.csv"};
            //args = new[] { @"c:\Users\shaybe\Dropbox\Thesis-PHd\SpatialEnrichment\Caulobacter\transferases\acetyltransferase.csv" };
            var numcoords = 300;

            Config = new ConfigParams("");

            if ((Config.ActionList & Actions.Experiment_ComparePivots) != 0)
            {
                Console.WriteLine(@"Running pivot comparison experiment");
                Experiments.CompareExhaustiveWithPivots(numcoords, numiter: 30);
                return;
            }
            if ((Config.ActionList & Actions.Experiment_SampleLines) != 0)
            {
                Console.WriteLine(@"Running sampling comparison experiment");
                var subsamples = new[] { 10, 20, 30 };
                var population = new[] { 40, 60, 100 };
                var counter    = 0;
                foreach (var nu in subsamples)
                {
                    foreach (var N in population)
                    {
                        Experiments.CompareExahustiveWithSubsamplingInput(N, nu, 50, counter++.ToString());
                    }
                }
                return;
            }

            if (Config.SKIP_SLACK != 0)
            {
                Console.WriteLine(@"Warning! Current configuration uses CONST_SKIP_SLACK={0}", Config.SKIP_SLACK);
            }
            if (StaticConfigParams.WriteToCSV)
            {
                Console.WriteLine(@"Warning! Current configuration writes cells to CSV - this is SLOW.");
            }

            #region init
            StaticConfigParams.rnd = (Config.ActionList & Actions.Program_RandomConstSeed) != 0 ? new SafeRandom(1) : new SafeRandom();
            Config.timer.Start();
            #endregion

            foreach (var dir in new List <string>()
            {
                "Cells", "Planes"
            })
            {
                var di = new DirectoryInfo(dir);
                if (!di.Exists)
                {
                    di.Create();
                }
                foreach (FileInfo file in di.GetFiles())
                {
                    file.Delete();
                }
            }
            foreach (var filemask in new List <string>()
            {
                "lines_*.csv", "coordSample_*.csv "
            })
            {
                FileInfo[] taskFiles = new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles(filemask);
                foreach (FileInfo file in taskFiles)
                {
                    file.Delete();
                }
            }
            //Load coordinates and labels
            var infile     = Path.GetFileNameWithoutExtension(args.Length > 0?args[0]:"");
            var identities = new List <string>();
            for (var instanceIter = 0; instanceIter < 1; instanceIter++)
            {
                var         coordinates = new List <ICoordinate>();
                List <bool> labels      = null;
                StaticConfigParams.filenamesuffix = instanceIter.ToString();
                Console.WriteLine("File {0}", instanceIter);
                if (args.Length > 0)
                {
                    if (File.Exists(args[0]))
                    {
                        var res = LoadCoordinatesFromFile(args, ref numcoords, identities);
                        coordinates = res.Item1;
                        labels      = res.Item2;
                    }
                    else
                    {
                        throw new ArgumentException("Input file not found!");
                    }
                }
                else
                {
                    var res = RandomizeCoordinatesAndSave(numcoords);
                    coordinates = res.Item1;
                    labels      = res.Item2;
                }

                var zeros       = labels.Count(l => l == false);
                var filterCount = (int)(Config.FilterKFurthestZeros * zeros);
                if (filterCount > 0)
                {
                    Console.WriteLine("Filtering {0} far away points", filterCount);
                    var positives = new List <ICoordinate>();
                    var negatives = new List <ICoordinate>();
                    var negIds    = new List <int>();
                    for (var i = 0; i < coordinates.Count; i++)
                    {
                        if (labels[i])
                        {
                            positives.Add(coordinates[i]);
                        }
                        else
                        {
                            negatives.Add(coordinates[i]);
                            negIds.Add(i);
                        }
                    }
                    var negMinDist = new HashSet <int>(negatives.Zip(negIds, (a, b) => new { PosMinDist = positives.Select(p => p.EuclideanDistance(a)).Min(), Id = b })
                                                       .OrderByDescending(n => n.PosMinDist).Select(t => t.Id).Take(filterCount));
                    coordinates = coordinates.Where((a, b) => !negMinDist.Contains(b)).ToList();
                    labels      = labels.Where((a, b) => !negMinDist.Contains(b)).ToList();
                    numcoords  -= filterCount;
                }

                //Actual work starts here
                var ones      = labels.Count(l => l);
                var linecount = ones * (numcoords - ones);
                Config.Cellcount = ((long)linecount * (linecount - 1)) / 2.0 + linecount + 1;

                //Look at lazy caretaker numbers. Note, we don't actually cover open cells
                //so its -linecount as each line has two open cells on either side of it,
                //and each open cell is made up of two lines.

                mHGJumper.Initialize(ones, numcoords - ones);
                mHGJumper.optHGT = Config.SIGNIFICANCE_THRESHOLD;// / Cellcount; //for bonferonni
                //alpha is the Bonferonni (union-bound) corrected significance level

                //Debugging.debug_mHG(numcoords,ones);
                Tesselation T                    = null;
                var         coordType            = coordinates.First().GetType();
                var         ew                   = new EnrichmentWrapper(Config);
                List <ISpatialmHGResult> results = null;
                if (coordType == typeof(Coordinate3D))
                {
                    Config.Cellcount += MathExtensions.Binomial(linecount, 3);
                    Console.WriteLine(@"Projecting 3D problem to collection of 2D {0} coordinates with {1} 1's (|cells|={2:n0}, alpha={3}).", numcoords, ones, Config.Cellcount, mHGJumper.optHGT);
                    results = ew.SpatialmHGWrapper3D(coordinates.Zip(labels,
                                                                     (a, b) => new Tuple <double, double, double, bool>(a.GetDimension(0), a.GetDimension(1),
                                                                                                                        a.GetDimension(2), b)).ToList(), options.BatchMode);
                }
                else if (coordType == typeof(Coordinate))
                {
                    Console.WriteLine(@"Starting work on {0} coordinates with {1} 1's (|cells|={2:n0}, alpha={3}).", numcoords, ones, Config.Cellcount, mHGJumper.optHGT);
                    results = ew.SpatialmHGWrapper(coordinates.Zip(labels, (a, b) =>
                                                                   new Tuple <double, double, bool>(a.GetDimension(0), a.GetDimension(1), b)).ToList());
                }
                for (var resid = 0; resid < results.Count; resid++)
                {
                    results[resid].SaveToCSV($@"Cells\{infile}_Cell_{resid}_{StaticConfigParams.filenamesuffix}.csv");
                }
                using (var outfile = new StreamWriter($"{infile}_mhglist_{StaticConfigParams.filenamesuffix}.csv"))
                    foreach (var res in Config.mHGlist.Where(t => t != null))
                    {
                        outfile.WriteLine("{0},{1}", res.Item2, res.Item1);
                    }
                if (options.BatchMode)
                {
                    AzureBatchExecution.UploadFileToContainer($"{infile}_mhglist_{StaticConfigParams.filenamesuffix}.csv", options.SaasUrl);
                    for (var resid = 0; resid < results.Count; resid++)
                    {
                        AzureBatchExecution.UploadFileToContainer($@"Cells\{infile}_Cell_{resid}_{StaticConfigParams.filenamesuffix}.csv", options.SaasUrl);
                    }
                }
            }

            //Finalize
            if (args.Length == 0 || Debugger.IsAttached)
            {
                Console.WriteLine("Total elapsed time: {0:g}.\nPress any key to continue.", Config.timer.Elapsed);
                Console.ReadKey();
            }
        }
Ejemplo n.º 18
0
        private void FirstPass(State state)
        {
            var skippedPoints = new HashSet <TPosition>(PositionComparer)
            {
                default(TPosition)
            };

            var activePoints    = new HashSet <TPosition>(PositionComparer);
            var newActivePoints = new HashSet <TPosition>(PositionComparer);
            var inactivePoints  = new HashSet <TPosition>(PositionComparer);

            var index     = 0;
            var testIndex = 0;
            var reference = default(TPosition);
            var key       = default(TKey);
            var shape     = default(Shape <TNode, TPosition, TKey>);

            while (skippedPoints.Count > 0)
            {
                reference = GetReference(skippedPoints);
                key       = Tesselation.Key(reference);
                shape     = state.CreateShape(key);

                activePoints.Clear();
                activePoints.Add(reference);

                while (activePoints.Count > 0)
                {
                    newActivePoints.Clear();

                    foreach (var v in activePoints)
                    {
                        skippedPoints.Remove(v);
                        inactivePoints.Add(v);
                        state.AddNodeToShape(shape, NodeFactory, v);

                        index = 0;
                        foreach (var vs in Tesselation.Surround(v))
                        {
                            testIndex = index++;
                            if (Tesselation.Valid(vs) && Tesselation.Tesselate(testIndex) && !(newActivePoints.Contains(vs) || inactivePoints.Contains(vs)))
                            {
                                if (KeyComparer.Equals(key, Tesselation.Key(vs)))
                                {
                                    newActivePoints.Add(vs);
                                }
                                else
                                {
                                    skippedPoints.Add(vs);
                                }
                            }
                        }
                    }

                    activePoints.Clear();
                    foreach (var item in newActivePoints)
                    {
                        activePoints.Add(item);
                    }
                }
            }
        }
Ejemplo n.º 19
0
        public static void CompareExhaustiveWithPivots(int numcoords = 50, int numiter = 500)
        {
            Config = new ConfigParams("");
            #region init
            StaticConfigParams.rnd = (Config.ActionList & Actions.Program_RandomConstSeed) != 0 ? new SafeRandom(1) : new SafeRandom();
            Config.timer.Start();
            #endregion
            //Load coordinates and labels
            var identities = new List <string>();
            var resultPairedDiff = new List <double>();
            int victories = 0, ties = 0;
            using (var fileout = new StreamWriter(@"pivot_vs_exhaustive.csv"))
                for (var instanceIter = 0; instanceIter < numiter; instanceIter++)
                {
                    var coordinates = new List <ICoordinate>();
                    var labels      = new List <bool>();
                    StaticConfigParams.filenamesuffix = instanceIter.ToString();
                    Console.WriteLine("File {0}", instanceIter);
                    var res = Program.RandomizeCoordinatesAndSave(numcoords, false);
                    coordinates = res.Item1;
                    labels      = res.Item2;
                    var zeros       = labels.Count(l => l == false);
                    var filterCount = (int)(Config.FilterKFurthestZeros * zeros);
                    if (filterCount > 0)
                    {
                        Console.WriteLine("Filtering {0} far away points", filterCount);
                        var positives = new List <ICoordinate>();
                        var negatives = new List <ICoordinate>();
                        var negIds    = new List <int>();
                        for (var i = 0; i < coordinates.Count; i++)
                        {
                            if (labels[i])
                            {
                                positives.Add(coordinates[i]);
                            }
                            else
                            {
                                negatives.Add(coordinates[i]);
                                negIds.Add(i);
                            }
                        }
                        var negMinDist = new HashSet <int>(negatives.Zip(negIds, (a, b) => new { PosMinDist = positives.Select(p => p.EuclideanDistance(a)).Min(), Id = b })
                                                           .OrderByDescending(n => n.PosMinDist).Select(t => t.Id).Take(filterCount));
                        coordinates = coordinates.Where((a, b) => !negMinDist.Contains(b)).ToList();
                        labels      = labels.Where((a, b) => !negMinDist.Contains(b)).ToList();
                        numcoords  -= filterCount;
                    }

                    //Actual work starts here
                    var ones      = labels.Count(l => l);
                    var linecount = ones * (numcoords - ones);
                    Config.Cellcount = ((long)linecount * (linecount - 1)) / 2.0 + linecount + 1;

                    mHGJumper.Initialize(ones, numcoords - ones);
                    mHGJumper.optHGT = Config.SIGNIFICANCE_THRESHOLD; // / Cellcount; //for bonferonni
                                                                      //alpha is the Bonferonni (union-bound) corrected significance level
                    Tesselation T  = null;
                    var         ew = new EnrichmentWrapper(Config);
                    Console.WriteLine(@"Starting work on {0} coordinates with {1} 1's (|cells|={2:n0}, alpha={3}).", numcoords, ones, Config.Cellcount, mHGJumper.optHGT);
                    var instanceData      = coordinates.Zip(labels, (a, b) => new Tuple <double, double, bool>(a.GetDimension(0), a.GetDimension(1), b)).ToList();
                    var resultsExhaustive = ew.SpatialmHGWrapper(instanceData).Select(v => (SpatialmHGResult)v).First();
                    var resultsPivot      = ew.mHGPivotWrapper(instanceData).Select(v => (SpatialmHGResult)v).First();
                    fileout.WriteLine($"{resultsExhaustive.pvalue}, {resultsPivot.pvalue}");

                    if (resultsExhaustive.pvalue < resultsPivot.pvalue)
                    {
                        victories++;
                    }
                    else if (resultsExhaustive.pvalue == resultsPivot.pvalue)
                    {
                        ties++;
                    }
                    else
                    {
                        Console.WriteLine($"Debug me");
                    }
                    resultPairedDiff.Add(Math.Log10(resultsPivot.pvalue) - Math.Log10(resultsExhaustive.pvalue));
                }

            Console.WriteLine($"Out of {numiter} iterations, spatial enrichment won in {victories} and tied in {ties}.");
            Console.WriteLine("Total elapsed time: {0:g}.\nPress any key to continue.", Config.timer.Elapsed);
            File.WriteAllLines("experiment_pvaldiffs.txt", resultPairedDiff.Select(v => v.ToString()).ToArray());
            Console.ReadKey();
        }
Ejemplo n.º 20
0
        /// <summary>
        /// 3d subsample from 50 points 20. run 100 times. compare to opt.
        /// </summary>
        /// <param name="numcoords"></param>a
        /// <param name="numiter"></param>
        public static List <double> CompareExahustiveWithSubsamplingInput(int numcoords = 50, int subsampleSize = 20, int numiter = 100, string suffix = "0")
        {
            Config = new ConfigParams("");
            #region init
            StaticConfigParams.rnd = (Config.ActionList & Actions.Program_RandomConstSeed) != 0 ? new SafeRandom(1) : new SafeRandom();
            Config.timer.Start();
            #endregion
            //Load coordinates and labels
            var resultPairedDiff = new List <double>();
            var extraAnalyses = new List <string>();
            int victories = 0, ties = 0;
            var coordinates = new List <ICoordinate>();
            var labels      = new List <bool>();
            Program.Config = Config;
            StaticConfigParams.filenamesuffix = suffix;
            var res = Program.RandomizeCoordinatesAndSave(numcoords, true);
            coordinates = res.Item1;
            labels      = res.Item2;
            var zeros       = labels.Count(l => l == false);
            var filterCount = (int)(Config.FilterKFurthestZeros * zeros);
            if (filterCount > 0)
            {
                Console.WriteLine("Filtering {0} far away points", filterCount);
                var positives = new List <ICoordinate>();
                var negatives = new List <ICoordinate>();
                var negIds    = new List <int>();
                for (var i = 0; i < coordinates.Count; i++)
                {
                    if (labels[i])
                    {
                        positives.Add(coordinates[i]);
                    }
                    else
                    {
                        negatives.Add(coordinates[i]);
                        negIds.Add(i);
                    }
                }
                var negMinDist = new HashSet <int>(negatives.Zip(negIds, (a, b) => new { PosMinDist = positives.Select(p => p.EuclideanDistance(a)).Min(), Id = b })
                                                   .OrderByDescending(n => n.PosMinDist).Select(t => t.Id).Take(filterCount));
                coordinates = coordinates.Where((a, b) => !negMinDist.Contains(b)).ToList();
                labels      = labels.Where((a, b) => !negMinDist.Contains(b)).ToList();
                numcoords  -= filterCount;
            }

            var instanceDataCoords = coordinates.Zip(labels, (a, b) => new Tuple <ICoordinate, bool>(a, b)).ToList();
            var instanceData       = coordinates.Zip(labels, (a, b) => new Tuple <double, double, bool>(a.GetDimension(0), a.GetDimension(1), b)).ToList();
            Config.SKIP_SLACK = -1000;
            var ew = new EnrichmentWrapper(Config);
            var resultsExhaustive = ew.SpatialmHGWrapper(instanceData).Select(v => (SpatialmHGResult)v).First();
            //Actual work starts here
            var ones      = labels.Count(l => l);
            var linecount = ones * (numcoords - ones);
            Config.Cellcount = ((long)linecount * (linecount - 1)) / 2.0 + linecount + 1;

            mHGJumper.Initialize(ones, numcoords - ones);
            mHGJumper.optHGT = Config.SIGNIFICANCE_THRESHOLD; // / Cellcount; //for bonferonni
                                                              //alpha is the Bonferonni (union-bound) corrected significance level

            using (var fileout = new StreamWriter($"sample_vs_exhaustive_{suffix}.csv"))
                for (var instanceIter = 1; instanceIter < numiter; instanceIter++)
                {
                    StaticConfigParams.filenamesuffix = instanceIter.ToString();
                    Console.WriteLine("File {0}", instanceIter);

                    var sampleCoords = coordinates
                                       .Zip(labels, (a, b) => new { Coords = a, Labels = b, Rand = StaticConfigParams.rnd.Next() })
                                       .OrderBy(v => v.Rand).Take(subsampleSize).ToList();
                    while (sampleCoords.All(v => v.Labels) || !sampleCoords.Any(v => v.Labels))
                    {
                        sampleCoords = coordinates
                                       .Zip(labels, (a, b) => new { Coords = a, Labels = b, Rand = StaticConfigParams.rnd.Next() })
                                       .OrderBy(v => v.Rand).Take(subsampleSize).ToList();
                    }
                    if (StaticConfigParams.WriteToCSV)
                    {
                        Generics.SaveToCSV(sampleCoords.Select(t => t.Coords.ToString() + "," + Convert.ToDouble(t.Labels)),
                                           $@"coords_{StaticConfigParams.filenamesuffix}.csv");
                    }

                    ones             = sampleCoords.Count(l => l.Labels);
                    linecount        = ones * (subsampleSize - ones);
                    Config.Cellcount = ((long)linecount * (linecount - 1)) / 2.0 + linecount + 1;

                    Console.WriteLine(@"Starting work on {0} coordinates with {1} 1's (|cells|={2:n0}, alpha={3}).", numcoords, ones, Config.Cellcount, mHGJumper.optHGT);

                    mHGJumper.optHGT = Config.SIGNIFICANCE_THRESHOLD;
                    Tesselation T = new Tesselation(sampleCoords.Select(v => (Coordinate)v.Coords).ToList(), sampleCoords.Select(v => v.Labels).ToList(), null, Config)
                    {
                        ProjectedFrom = coordinates,
                        SourceLabels  = labels.ToArray()
                    };
                    var topResults = T.GradientSkippingSweep(numStartCoords: 20, numThreads: Environment.ProcessorCount - 1).First();
                    Line.Reset();


                    if (resultsExhaustive.pvalue < topResults.mHG.Item1)
                    {
                        victories++;
                    }
                    else if (resultsExhaustive.pvalue == topResults.mHG.Item1)
                    {
                        ties++;
                    }
                    else
                    {
                        Console.WriteLine($"Debug me");
                    }
                    var pdiff = Math.Log10(topResults.mHG.Item1) - Math.Log10(resultsExhaustive.pvalue);
                    resultPairedDiff.Add(pdiff);

                    mHGJumper.optHGT = Config.SIGNIFICANCE_THRESHOLD;
                    Console.Write($"Uniform grid strategy @{Config.Cellcount} pivots... ");
                    var uniformGridFactory = new Gridding();
                    uniformGridFactory.GeneratePivotGrid(Convert.ToInt64(Config.Cellcount));
                    var uniformGridPivotlst = uniformGridFactory.GetPivots().ToList();
                    var uniformGridPivot    = uniformGridPivotlst.AsParallel().Max(p => - Math.Log10(EnrichmentAtPivot(instanceDataCoords, p)));
                    Console.WriteLine($"p={uniformGridPivot:e}");
                    Console.Write($"Empirical grid strategy @{Config.Cellcount} pivots... ");
                    mHGJumper.optHGT = Config.SIGNIFICANCE_THRESHOLD;
                    var empiricalGridFactory = new Gridding();
                    empiricalGridFactory.GenerateEmpricialDensityGrid(Convert.ToInt64(Config.Cellcount), instanceDataCoords);
                    var empiricalGridPivotlst = empiricalGridFactory.GetPivots().ToList();
                    var empiricalGridPivot    = empiricalGridPivotlst.AsParallel().Max(p => - Math.Log10(EnrichmentAtPivot(instanceDataCoords, p)));
                    Console.WriteLine($"p={empiricalGridPivot:e}");
                    //extraAnalyses.Add($"{-Math.Log10(resultsExhaustive.pvalue)}, {-Math.Log10(topResults.mHG.Item1)}, {uniformGridPivot}, {empiricalGridPivot}");
                    fileout.WriteLine($"{-Math.Log10(resultsExhaustive.pvalue)}, {-Math.Log10(topResults.mHG.Item1)}, {uniformGridPivot}, {empiricalGridPivot}");
                }

            Console.WriteLine($"Out of {numiter} iterations, spatial enrichment won in {victories} and tied in {ties}.");
            Console.WriteLine("Total elapsed time: {0:g}.\nPress any key to continue.", Config.timer.Elapsed);
            //File.WriteAllLines($"experiment_pvaldiffs_{suffix}.txt", resultPairedDiff.Select(v => v.ToString()).ToArray());
            //File.WriteAllLines($"experimentsAll_{suffix}.txt", extraAnalyses);
            return(resultPairedDiff);
        }