public static (Dictionary <string, DataTree <object> >, Dictionary <string, List <string> >) TransposeThresholdMatrix(
            Dictionary <string, Dictionary <string, object> > resultsToTranspose)
        {
            var output = new Dictionary <string, DataTree <object> >();
            var legend = new Dictionary <string, List <string> >
            {
                { "season", new List <string>() },
                { "patch", new List <string>() },
                { "threshold", new List <string>() }
            };

            var seasons = WindThreshold.ThresholdSeasons();

            foreach (var thresholdKey in resultsToTranspose.Keys)
            {
                foreach (var patchKey in resultsToTranspose[thresholdKey].Keys)
                {
                    //var pointIndex = 0;
                    var pointValues   = (List <List <double> >)resultsToTranspose[thresholdKey][patchKey];
                    var seasonCounter = 0;
                    foreach (var seasonValue in pointValues)
                    {
                        var seasonKey = seasons[seasonCounter];

                        if (!legend["season"].Contains(seasonKey))
                        {
                            legend["season"].Add(seasonKey);
                        }

                        if (!legend["patch"].Contains(patchKey))
                        {
                            legend["patch"].Add(patchKey);
                        }

                        if (!legend["threshold"].Contains(thresholdKey))
                        {
                            legend["threshold"].Add(thresholdKey);
                        }

                        if (!output.ContainsKey(seasonKey))
                        {
                            output.Add(seasonKey, new DataTree <object>());
                        }

                        var patchCounter     = legend["patch"].IndexOf(patchKey);
                        var thresholdCounter = legend["threshold"].IndexOf(thresholdKey);

                        var path = new GH_Path(patchCounter, thresholdCounter);
                        output[seasonKey].AddRange(seasonValue.Select(elem => (object)elem), path);
                        seasonCounter++;
                    }
                }
            }

            return(output, legend);
        }
Beispiel #2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string inputJson   = null;
            var    epwFile     = "";
            var    patches     = new List <string>();
            var    thresholds  = new List <string>();
            var    cpus        = 4;
            var    dependentOn = "Probe";
            var    create      = false;

            if (!DA.GetData(0, ref inputJson))
            {
                return;
            }
            if (inputJson == "error")
            {
                return;
            }
            if (!DA.GetData(1, ref epwFile))
            {
                return;
            }
            if (!DA.GetDataList(2, patches))
            {
                patches.Add("set1");
            }

            DA.GetDataList(3, thresholds);
            DA.GetData(4, ref cpus);

            DA.GetData(5, ref dependentOn);
            DA.GetData(6, ref create);

            // Get Cache to see if we already did this
            var cacheKey     = string.Join("", patches) + epwFile + inputJson;
            var cachedValues = StringCache.getCache(cacheKey);

            DA.DisableGapLogic();

            if (cachedValues == null || create)
            {
                var queueName = "windThreshold";

                // Get queue lock
                var queueLock = StringCache.getCache(queueName);
                if (queueLock != "true")
                {
                    StringCache.setCache(queueName, "true");
                    StringCache.setCache(InstanceGuid.ToString(), "");
                    StringCache.setCache(cacheKey, null);
                    QueueManager.addToQueue(queueName, () =>
                    {
                        try
                        {
                            var results = WindThreshold.ComputeWindThresholds(
                                inputJson,
                                epwFile,
                                patches,
                                thresholds,
                                ComponentUtils.ValidateCPUs(cpus),
                                dependentOn,
                                create
                                );
                            cachedValues = results;
                            StringCache.setCache(cacheKey, cachedValues);
                            if (create)
                            {
                                StringCache.setCache(cacheKey + "create", "true");
                            }
                        }
                        catch (NoObjectFoundException)
                        {
                            StringCache.setCache(cacheKey + "create", "");
                        }
                        catch (Exception e)
                        {
                            StringCache.setCache(InstanceGuid.ToString(), e.Message);
                            StringCache.setCache(cacheKey, "error");
                            StringCache.setCache(cacheKey + "create", "");
                        }


                        ExpireSolutionThreadSafe(true);
                        Thread.Sleep(2000);
                        StringCache.setCache(queueName, "");
                    });
                }
            }

            HandleErrors();

            // Read from Cache
            if (cachedValues != null)
            {
                DA.SetData(0, cachedValues);
                Message = "";
                if (StringCache.getCache(cacheKey + "create") == "true")
                {
                    Message = "Task Created";
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string folder   = null;
            var    criteria = 0;
            var    thresholdsFrequencies = new List <string>();
            var    refresh = false;

            if (!DA.GetData(0, ref folder))
            {
                return;
            }
            DA.GetData(1, ref criteria);
            DA.GetDataList(2, thresholdsFrequencies);
            DA.GetData(3, ref refresh);

            var cacheKey     = folder + criteria + string.Join("", thresholdsFrequencies);
            var cachedValues = StringCache.getCache(cacheKey);

            DA.DisableGapLogic();

            if (cachedValues == null || refresh)
            {
                const string queueName = "thresholdResults";
                StringCache.setCache(InstanceGuid.ToString(), "");

                // Get queue lock
                var queueLock = StringCache.getCache(queueName);
                if (queueLock != "true")
                {
                    StringCache.setCache(queueName, "true");
                    StringCache.setCache(cacheKey + "progress", "Loading...");

                    QueueManager.addToQueue(queueName, () =>
                    {
                        try
                        {
                            var results = WindThreshold.ReadThresholdResults(folder);
                            if (criteria == 1)
                            {
                                _thresholdFrequencies = thresholdsFrequencies
                                                        .Select(frequency => new Thresholds.WindThreshold().FromJson(frequency)).ToList();
                                lawsonResults = WindThreshold.LawsonsCriteria(results, _thresholdFrequencies);
                            }
                            else
                            {
                                (thresholdOutput, thresholdLegend) = TransposeThresholdMatrix(results);
                            }
                            StringCache.setCache(cacheKey + "progress", "Done");
                            StringCache.setCache(cacheKey, "results");
                        }
                        catch (Exception e)
                        {
                            StringCache.setCache(InstanceGuid.ToString(), e.Message);
                            StringCache.setCache(cacheKey, "error");
                            StringCache.setCache(cacheKey + "progress", "");
                        }

                        ExpireSolutionThreadSafe(true);
                        Thread.Sleep(2000);
                        StringCache.setCache(queueName, "");
                    });
                    ExpireSolutionThreadSafe(true);
                }
            }

            HandleErrors();

            if (lawsonResults != null && criteria == 1)
            {
                foreach (var season in lawsonResults.Keys)
                {
                    var data = ConvertLawsonToDataTree(lawsonResults[season]);
                    AddToOutput(DA, season, data);
                }

                info = UpdateInfo(lawsonResults.First().Value.Keys.ToList(), _thresholdFrequencies, criteria);
                RemoveUnusedOutputs(lawsonResults.Keys.ToList());
            }

            if (thresholdOutput != null && criteria == 0)
            {
                foreach (var key in thresholdOutput.Keys)
                {
                    AddToOutput(DA, key, thresholdOutput[key]);
                }

                info = UpdateInfo(
                    thresholdLegend["patch"],
                    thresholdLegend["threshold"].Select(threshold => new Thresholds.WindThreshold {
                    Field = threshold
                })
                    .ToList(),
                    criteria
                    );
                RemoveUnusedOutputs(thresholdOutput.Keys.ToList());
            }

            if (info != null)
            {
                DA.SetDataTree(0, info);
            }
            Message = StringCache.getCache(cacheKey + "progress");
        }