public async Task <IActionResult> OnPostRenameAsync(int?id)
        {
            if (id == null)
            {
                return(RedirectToPage("Datasets"));
            }

            Models.Dataset dataset = await _context.Datasets.FirstOrDefaultAsync(x => x.Id == id);

            if (await _context.Datasets.FirstOrDefaultAsync(x => x.Name == Name && (x.AccountDatasets.FirstOrDefault(y => y.AccountId == HttpContext.Session.GetInt32("id")) != null)) != null)
            {
                ModelState.AddModelError("Name", "You already have a dataset with this name.");
                Dataset = await _context.Datasets.FirstOrDefaultAsync(x => x.Id == id);

                Name     = Dataset.Name;
                Articles = await _context.Articles.Where(x => x.DatasetId == Dataset.Id).ToListAsync();

                return(Page());
            }

            if (dataset != null && await _context.AccountDatasets.FirstOrDefaultAsync(x => x.DatasetId == dataset.Id && x.AccountId == HttpContext.Session.GetInt32("id")) != null)
            {
                _context.Attach(dataset).State = EntityState.Modified;
                dataset.Name = Name;
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("Datasets"));
        }
Example #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (await _context.AccountDatasets.FirstOrDefaultAsync(x => x.DatasetId == DatasetId && x.AccountId == HttpContext.Session.GetInt32("id")) == null)
            {
                return(RedirectToPage("Error"));
            }

            Models.Dataset dataset = await _context.Datasets.FirstOrDefaultAsync(x => x.Id == DatasetId);

            Models.Account account = await _context.Accounts.FirstOrDefaultAsync(x => x.Email == Email && !x.IsAdmin);

            if (account == null)
            {
                ModelState.AddModelError("Email", "No account with this email address exists.");
            }
            else if (await _context.Datasets.FirstOrDefaultAsync(x => x.Name == dataset.Name && (x.AccountDatasets.FirstOrDefault(y => y.AccountId == account.Id) != null)) != null)
            {
                ModelState.AddModelError("DatasetId", "User already has a dataset with this name.");
            }

            if (!ModelState.IsValid)
            {
                Datasets = await _context.Datasets.Where(x => x.AccountDatasets.Any(y => y.DatasetId == x.Id && y.AccountId == HttpContext.Session.GetInt32("id"))).ToListAsync();

                return(Page());
            }

            _context.AccountDatasets.Add(new Models.AccountDataset()
            {
                Account = account, Dataset = dataset
            });
            await _context.SaveChangesAsync();

            return(RedirectToPage("Datasets"));
        }
        public async Task <IActionResult> OnPostDownloadAsync(int?id)
        {
            if (id == null)
            {
                return(RedirectToPage("Datasets"));
            }

            Models.Dataset dataset = await _context.Datasets.FindAsync(id);

            if (dataset != null && await _context.AccountDatasets.FirstOrDefaultAsync(x => x.DatasetId == dataset.Id && x.AccountId == HttpContext.Session.GetInt32("id")) != null)
            {
                List <Models.Article> articles = await _context.Articles.Where(x => x.DatasetId == dataset.Id).ToListAsync();

                StringBuilder fileContents = new StringBuilder();

                foreach (Models.Article article in dataset.Articles)
                {
                    fileContents.Append($"{article.Pmid},{(article.Classification ? "1" : "0")}\r\n");
                }

                return(File(Encoding.UTF8.GetBytes(fileContents.ToString()), "text/csv", $"{dataset.Name}.csv"));
            }

            return(RedirectToPage("Error"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (await _context.Datasets.FirstOrDefaultAsync(x => x.Name == Name && (x.AccountDatasets.FirstOrDefault(y => y.AccountId == HttpContext.Session.GetInt32("id")) != null)) != null)
            {
                ModelState.AddModelError("Name", "You already have a dataset with this name.");
            }

            if (DatasetId1 == DatasetId2)
            {
                ModelState.AddModelError("DatasetId2", "You must select two different datasets.");
            }

            if (!ModelState.IsValid)
            {
                Datasets = await _context.Datasets.Where(x => x.AccountDatasets.Any(y => y.DatasetId == x.Id && y.AccountId == HttpContext.Session.GetInt32("id"))).Include(x => x.Articles).ToListAsync();

                return(Page());
            }

            if (await _context.AccountDatasets.FirstOrDefaultAsync(x => x.DatasetId == DatasetId1 && x.AccountId == HttpContext.Session.GetInt32("id")) == null || await _context.AccountDatasets.FirstOrDefaultAsync(x => x.DatasetId == DatasetId2 && x.AccountId == HttpContext.Session.GetInt32("id")) == null)
            {
                return(RedirectToPage("Error"));
            }

            List <Models.Article> queriedArticles = await _context.Articles.Where(x => x.DatasetId == DatasetId1 || x.DatasetId == DatasetId2).ToListAsync();

            List <Models.Article> combinedArticles = new List <Models.Article>();

            foreach (Models.Article article in queriedArticles)
            {
                combinedArticles.Add(new Models.Article()
                {
                    Abstract       = article.Abstract,
                    Classification = article.Classification,
                    Pmid           = article.Pmid,
                    Title          = article.Title,
                    Date           = article.Date
                });
            }

            Models.Dataset dataset = new Models.Dataset()
            {
                Name     = Name,
                Articles = combinedArticles,
                Date     = DateTime.Now
            };

            Models.Account account = await _context.Accounts.FirstOrDefaultAsync(x => x.Id == HttpContext.Session.GetInt32("id"));

            _context.Datasets.Add(dataset);
            _context.AccountDatasets.Add(new Models.AccountDataset()
            {
                Account = account, Dataset = dataset
            });
            await _context.SaveChangesAsync();

            return(RedirectToPage("Datasets"));
        }
        public async Task <IActionResult> OnPostDeleteAsync(int?id)
        {
            if (id == null)
            {
                return(RedirectToPage("Datasets"));
            }

            Models.Dataset dataset = await _context.Datasets.FindAsync(id);

            if (dataset != null && await _context.AccountDatasets.FirstOrDefaultAsync(x => x.DatasetId == dataset.Id && x.AccountId == HttpContext.Session.GetInt32("id")) != null)
            {
                _context.Datasets.Remove(dataset);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("Datasets"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (await _context.AccountDatasets.FirstOrDefaultAsync(x => x.DatasetId == DatasetId && x.AccountId == HttpContext.Session.GetInt32("id")) == null)
            {
                return(RedirectToPage("Error"));
            }

            Models.Dataset dataset = await _context.Datasets.FirstOrDefaultAsync(x => x.Id == DatasetId);

            List <Models.Article> articles = await _context.Articles.Where(x => x.DatasetId == dataset.Id).ToListAsync();

            if (articles.Count(x => x.Classification) < 5 || articles.Count(x => !x.Classification) < 5)
            {
                ModelState.AddModelError("DatasetId", "Dataset must have at least 5 articles of each classification.");
            }

            if (!ModelState.IsValid)
            {
                Datasets = await _context.Datasets.Where(x => x.AccountDatasets.Any(y => y.DatasetId == x.Id && y.AccountId == HttpContext.Session.GetInt32("id"))).ToListAsync();

                return(Page());
            }

            List <ArticleData> data = articles.Select(x => new ArticleData()
            {
                Text = x.Title + " " + x.Abstract, Classification = x.Classification
            }).ToList();
            MLContext     mlContext     = new MLContext();
            IDataView     dataView      = mlContext.Data.LoadFromEnumerable(data);
            TrainTestData splitDataView = mlContext.Data.TrainTestSplit(dataView, 0.2);
            var           estimator     = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(ArticleData.Text))
                                          .Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features"));
            ITransformer model       = estimator.Fit(splitDataView.TrainSet);
            IDataView    predictions = model.Transform(splitDataView.TestSet);
            CalibratedBinaryClassificationMetrics metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label");
            var guid = Guid.NewGuid().ToString();
            var path = Path.Combine(_environment.WebRootPath, "classifiers", $"{guid}.zip");

            mlContext.Model.Save(model, dataView.Schema, path);

            Models.Classifier classifier = new Models.Classifier()
            {
                Name      = Name,
                Accuracy  = metrics.Accuracy,
                Precision = metrics.PositivePrecision,
                Recall    = metrics.PositiveRecall,
                Date      = DateTime.Now,
                Model     = guid
            };

            Models.Account account = await _context.Accounts.FirstOrDefaultAsync(x => x.Id == HttpContext.Session.GetInt32("id"));

            _context.Classifiers.Add(classifier);
            _context.AccountClassifiers.Add(new Models.AccountClassifier()
            {
                Account = account, Classifier = classifier
            });
            await _context.SaveChangesAsync();

            return(RedirectToPage("Classifiers"));
        }
Example #7
0
 public void UpdateTranslations(Models.Dataset register, Models.Dataset originalRegister)
 {
     originalRegister.Translations.Where(t => t.RegisterItemId != Guid.Empty).ToList().ForEach(x => _dbContext.Entry(x).State = EntityState.Deleted);
     originalRegister.Translations = register.Translations;
 }