< Summary

Class:Azure.AI.FormRecognizer.Training.CustomFormModel
Assembly:Azure.AI.FormRecognizer
File(s):C:\Git\azure-sdk-for-net\sdk\formrecognizer\Azure.AI.FormRecognizer\src\CustomFormModel.cs
Covered lines:53
Uncovered lines:0
Coverable lines:53
Total lines:133
Line coverage:100% (53 of 53)
Covered branches:22
Total branches:22
Branch coverage:100% (22 of 22)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
get_ModelId()-100%100%
get_Status()-100%100%
get_TrainingStartedOn()-100%100%
get_TrainingCompletedOn()-100%100%
get_Submodels()-100%100%
get_TrainingDocuments()-100%100%
get_Errors()-100%100%
ConvertToSubmodels(...)-100%100%
ConvertFromUnlabeled(...)-100%100%
ConvertFromLabeled(...)-100%100%
ConvertToTrainingDocuments(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\formrecognizer\Azure.AI.FormRecognizer\src\CustomFormModel.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using Azure.AI.FormRecognizer.Models;
 7
 8namespace Azure.AI.FormRecognizer.Training
 9{
 10    /// <summary>
 11    /// Represents a model trained from custom forms.
 12    /// </summary>
 13    public class CustomFormModel
 14    {
 14415        internal CustomFormModel(Model model)
 16        {
 14417            ModelId = model.ModelInfo.ModelId;
 14418            Status = model.ModelInfo.Status;
 14419            TrainingStartedOn = model.ModelInfo.TrainingStartedOn;
 14420            TrainingCompletedOn = model.ModelInfo.TrainingCompletedOn;
 14421            Submodels = ConvertToSubmodels(model);
 22
 23            // TrainResult can be null if model is not ready yet.
 24
 14425            TrainingDocuments = model.TrainResult != null
 14426                ? ConvertToTrainingDocuments(model.TrainResult)
 14427                : new List<TrainingDocumentInfo>();
 28
 14429            Errors = model.TrainResult?.Errors ?? new List<FormRecognizerError>();
 14430        }
 31
 32        /// <summary>
 33        /// The unique identifier of this model.
 34        /// </summary>
 15635        public string ModelId { get; }
 36
 37        /// <summary>
 38        /// A status indicating this model's readiness for use.
 39        /// </summary>
 14840        public CustomFormModelStatus Status { get; }
 41
 42        /// <summary>
 43        /// The date and time (UTC) when model training was started.
 44        /// </summary>
 3245        public DateTimeOffset TrainingStartedOn { get; }
 46
 47        /// <summary>
 48        /// The date and time (UTC) when model training completed.
 49        /// </summary>
 3250        public DateTimeOffset TrainingCompletedOn { get; }
 51
 52        /// <summary>
 53        /// A list of submodels that are part of this model, each of which can recognize and extract fields from a diffe
 54        /// </summary>
 36055        public IReadOnlyList<CustomFormSubmodel> Submodels { get; }
 56
 57        /// <summary>
 58        /// A list of meta-data about each of the documents used to train the model.
 59        /// </summary>
 14860        public IReadOnlyList<TrainingDocumentInfo> TrainingDocuments { get; }
 61
 62        /// <summary>
 63        /// A list of errors ocurred during the training operation.
 64        /// </summary>
 6065        public IReadOnlyList<FormRecognizerError> Errors { get; }
 66
 67        private static IReadOnlyList<CustomFormSubmodel> ConvertToSubmodels(Model model)
 68        {
 14469            if (model.Keys != null)
 7270                return ConvertFromUnlabeled(model.Keys);
 71
 7272            if (model.TrainResult != null)
 6873                return ConvertFromLabeled(model);
 74
 475            return null;
 76        }
 77
 78        private static IReadOnlyList<CustomFormSubmodel> ConvertFromUnlabeled(KeysResult keys)
 79        {
 7280            var subModels = new List<CustomFormSubmodel>();
 81
 30482            foreach (var cluster in keys.Clusters)
 83            {
 8084                var fieldMap = new Dictionary<string, CustomFormModelField>();
 328085                for (int i = 0; i < cluster.Value.Count; i++)
 86                {
 156087                    string fieldName = "field-" + i;
 156088                    string fieldLabel = cluster.Value[i];
 156089                    fieldMap.Add(fieldName, new CustomFormModelField(fieldName, fieldLabel, default));
 90                }
 8091                subModels.Add(new CustomFormSubmodel(
 8092                    $"form-{cluster.Key}",
 8093                    default,
 8094                    fieldMap));
 95            }
 7296            return subModels;
 97        }
 98
 99        private static IReadOnlyList<CustomFormSubmodel> ConvertFromLabeled(Model model)
 100        {
 68101            var fieldMap = new Dictionary<string, CustomFormModelField>();
 102
 68103            if (model.TrainResult.Fields != null)
 104            {
 1984105                foreach (var formFieldsReport in model.TrainResult.Fields)
 106                {
 924107                    fieldMap.Add(formFieldsReport.Name, new CustomFormModelField(formFieldsReport.Name, null, formFields
 108                }
 109            }
 110
 68111            return new List<CustomFormSubmodel> {
 68112                new CustomFormSubmodel(
 68113                    $"form-{model.ModelInfo.ModelId}",
 68114                    model.TrainResult.AverageModelAccuracy,
 68115                    fieldMap)};
 116        }
 117
 118        private static IReadOnlyList<TrainingDocumentInfo> ConvertToTrainingDocuments(TrainResult trainResult)
 119        {
 140120            var trainingDocs = new List<TrainingDocumentInfo>();
 1648121            foreach (var docs in trainResult.TrainingDocuments)
 122            {
 684123                trainingDocs.Add(
 684124                    new TrainingDocumentInfo(
 684125                        docs.Name,
 684126                        docs.PageCount,
 684127                        docs.Errors ?? new List<FormRecognizerError>(),
 684128                        docs.Status));
 129            }
 140130            return trainingDocs;
 131        }
 132    }
 133}