public static List<aim4_dotnet.ImagingPhysicalEntity> ToSelectedAnatomicEntitiesList(AimTemplateTreeAnatomicEntityNode node)
        {
            var selectedImagingPhysicalEntities = new List<aim4_dotnet.ImagingPhysicalEntity>();
            bool isValid = node.Valid;
            foreach (var term in node.SelectedAllowedTerms)
            {
                var imagingPhysicalEntity = new aim4_dotnet.ImagingPhysicalEntity
                    {
                        UniqueIdentifier = NewUid,
                        TypeCode = ToNativeCodeList(term),
                        QuestionTypeCode = ToNativeCodeList(node.QuestionType),
                        QuestionIndex = node.ItemNumber,
                        Label = node.Label,
                        IsPresent = true
                    };

                if (node.HasConfidence)
                    imagingPhysicalEntity.AnnotatorConfidence = null; // node.ConfidenceValue;

                //node
                var selectedImagingPhysicalEntityCharacteristics = SelectedAnatomicEntityCharacteristicsDictionary(node);

                // Sort characteristics first according to their ItemNumber
                List<int> anatomicEntityCharacteristicIndices =
                    CollectionUtils.Sort(selectedImagingPhysicalEntityCharacteristics.Keys, (i1, i2) => i1.CompareTo(i2));
                foreach (int characteristicIndex in anatomicEntityCharacteristicIndices)
                {
                    // Find definition of the selected characteristic in the component
                    int index = characteristicIndex;
                    AimTemplateTreeAnatomicEntityCharacteristicNode templateAnatomicEntityCharacteristic =
                        CollectionUtils.SelectFirst(node.AnatomicEntityCharacteristicTreeNodes,
                                                    item => item.ItemNumber == index);
                    isValid = templateAnatomicEntityCharacteristic != null &&
                              selectedImagingPhysicalEntityCharacteristics[characteristicIndex].Count >=
                              Math.Min(templateAnatomicEntityCharacteristic.CharacteristicQuantificationAllowedTerms.Count, templateAnatomicEntityCharacteristic.MinCardinality) &&
                              selectedImagingPhysicalEntityCharacteristics[characteristicIndex].Count <= templateAnatomicEntityCharacteristic.MaxCardinality;
                    if (!isValid)
                        break;

                    // Put all Anatomic Entity Characteristics into a single collection
                    imagingPhysicalEntity.ImagingPhysicalEntityCharacteristicCollection =
                        CollectionUtils.Concat<aim4_dotnet.ImagingPhysicalEntityCharacteristic>(
                            imagingPhysicalEntity.ImagingPhysicalEntityCharacteristicCollection ?? new List<aim4_dotnet.ImagingPhysicalEntityCharacteristic>(),
                            selectedImagingPhysicalEntityCharacteristics[characteristicIndex]
                            );
                }
                if (!isValid)
                    break;

                selectedImagingPhysicalEntities.Add(imagingPhysicalEntity);
            }
            return selectedImagingPhysicalEntities;
        }
 public static Dictionary<int, List<aim4_dotnet.ImagingPhysicalEntityCharacteristic>> SelectedAnatomicEntityCharacteristicsDictionary(AimTemplateTreeAnatomicEntityNode anatomicEntity)
 {
     return anatomicEntity.SelectedAnatomicEntityCharacteristicTreeNodes.ToDictionary(aec => aec.ItemNumber,
         aec => new List<aim4_dotnet.ImagingPhysicalEntityCharacteristic>(SelectedAnatomicEntityCharacteristics(aec)));
 }