public static bool Equals(MidiChunk midiChunk1, MidiChunk midiChunk2, MidiChunkEqualityCheckSettings settings, out string message)
        {
            message = null;

            if (ReferenceEquals(midiChunk1, midiChunk2))
            {
                return(true);
            }

            if (ReferenceEquals(null, midiChunk1) || ReferenceEquals(null, midiChunk2))
            {
                message = "One of chunks is null.";
                return(false);
            }

            var type1 = midiChunk1.GetType();
            var type2 = midiChunk2.GetType();

            if (type1 != type2)
            {
                message = $"Types of chunks are different ({type1} vs {type2}).";
                return(false);
            }

            var trackChunk1 = midiChunk1 as TrackChunk;

            if (trackChunk1 != null)
            {
                var trackChunk2 = (TrackChunk)midiChunk2;

                var events1 = trackChunk1.Events;
                var events2 = trackChunk2.Events;

                if (events1.Count != events2.Count)
                {
                    message = $"Counts of events in track chunks are different ({events1.Count} vs {events2.Count}).";
                    return(false);
                }

                for (var i = 0; i < events1.Count; i++)
                {
                    var event1 = events1[i];
                    var event2 = events2[i];

                    string eventsComparingMessage;
                    if (!MidiEvent.Equals(event1, event2, settings.EventEqualityCheckSettings, out eventsComparingMessage))
                    {
                        message = $"Events at position {i} in track chunks are different. {eventsComparingMessage}";
                        return(false);
                    }
                }

                return(true);
            }

            var unknownChunk1 = midiChunk1 as UnknownChunk;

            if (unknownChunk1 != null)
            {
                var unknownChunk2 = (UnknownChunk)midiChunk2;

                var chunkId1 = unknownChunk1.ChunkId;
                var chunkId2 = unknownChunk2.ChunkId;

                if (chunkId1 != chunkId2)
                {
                    message = $"IDs of unknown chunks are different ({chunkId1} vs {chunkId2}).";
                    return(false);
                }

                if (!ArrayUtilities.Equals(unknownChunk1.Data, unknownChunk2.Data))
                {
                    message = "Unknown chunks data are different.";
                    return(false);
                }

                return(true);
            }

            var result = midiChunk1.Equals(midiChunk2);

            if (!result)
            {
                message = $"Chunks {midiChunk1} and {midiChunk2} are not equal by result of Equals call on first chunk.";
            }

            return(result);
        }
Esempio n. 2
0
        public static bool Equals(MidiChunk midiChunk1, MidiChunk midiChunk2, MidiChunkEqualityCheckSettings settings, out string message)
        {
            message = null;

            if (ReferenceEquals(midiChunk1, midiChunk2))
            {
                return(true);
            }

            if (ReferenceEquals(null, midiChunk1) || ReferenceEquals(null, midiChunk2))
            {
                message = "One of chunks is null.";
                return(false);
            }

            var type1 = midiChunk1.GetType();
            var type2 = midiChunk2.GetType();

            if (type1 != type2)
            {
                message = $"Types of chunks are different ({type1} vs {type2}).";
                return(false);
            }

            var trackChunk1 = midiChunk1 as TrackChunk;

            if (trackChunk1 != null)
            {
                var trackChunk2 = (TrackChunk)midiChunk2;
                return(EventsCollectionEquality.Equals(trackChunk1.Events, trackChunk2.Events, settings.EventEqualityCheckSettings, out message));
            }

            var unknownChunk1 = midiChunk1 as UnknownChunk;

            if (unknownChunk1 != null)
            {
                var unknownChunk2 = (UnknownChunk)midiChunk2;

                var chunkId1 = unknownChunk1.ChunkId;
                var chunkId2 = unknownChunk2.ChunkId;

                if (chunkId1 != chunkId2)
                {
                    message = $"IDs of unknown chunks are different ({chunkId1} vs {chunkId2}).";
                    return(false);
                }

                if (!ArrayUtilities.Equals(unknownChunk1.Data, unknownChunk2.Data))
                {
                    message = "Unknown chunks data are different.";
                    return(false);
                }

                return(true);
            }

            var result = midiChunk1.Equals(midiChunk2);

            if (!result)
            {
                message = $"Chunks {midiChunk1} and {midiChunk2} are not equal by result of Equals call on first chunk.";
            }

            return(result);
        }
    private void ProcessBatch()
    {
        while (_requestsBatch.Count > 0)
        {
            var request        = _requestsBatch.Dequeue();
            var graphicsFormat = GraphicsFormatUtility.GetGraphicsFormat(request.renderTexture.format, false);
            var pixelSize      = GraphicsFormatUtility.GetBlockSize(graphicsFormat);
            var channels       = GraphicsFormatUtility.GetComponentCount(graphicsFormat);
            var channelSize    = pixelSize / channels;
            var rect           = new Rect(0, 0, request.renderTexture.width, request.renderTexture.height);

            if (channels >= 3 && channels <= 4)
            {
                if (request.texture == null)
                {
                    request.texture = new Texture2D(request.renderTexture.width, request.renderTexture.height, request.renderTexture.graphicsFormat, TextureCreationFlags.None);
                }
                RenderTexture.active = request.renderTexture;
                request.texture.ReadPixels(rect, 0, 0);
                request.InvokeCallback(request.texture.GetRawTextureData());
                RenderTexture.active = null;
            }
            else
            {
                Debug.Assert(channels == 1, "Can only handle a single channel RT.");

                // Read pixels must be one of RGBA32, ARGB32, RGB24, RGBAFloat or RGBAHalf.
                // So R16 and RFloat will be converted to RGBAFloat.
                var texture = new Texture2D(request.renderTexture.width, request.renderTexture.height, TextureFormat.RGBAFloat, false);
                RenderTexture.active = request.renderTexture;
                texture.ReadPixels(rect, 0, 0);
                RenderTexture.active = null;

                var length = request.renderTexture.width * request.renderTexture.height;
                var input  = ArrayUtilities.Cast <float>(texture.GetRawTextureData());
                UnityEngine.Object.Destroy(texture);

                int index = 0;
                switch (channelSize)
                {
                case 2:
                    short[] shorts    = ArrayUtilities.Allocate <short>(length);
                    var     si        = 0;
                    var     numerator = (1 << 16) - 1;
                    while (index < length)
                    {
                        shorts[index++] = (short)(numerator * input[si]);
                        si += 4;
                    }
                    var shortOutputNativeArray = new NativeArray <byte>(ArrayUtilities.Cast <byte>(shorts), Allocator.Persistent);
                    request.InvokeCallback(ArrayUtilities.Cast <byte>(shorts));
                    break;

                case 4:
                    float[] floats = ArrayUtilities.Allocate <float>(length);
                    var     fi     = 0;
                    while (index < length)
                    {
                        floats[index++] = input[fi];
                        fi += 4;
                    }
                    var floatOutputNativeArray = new NativeArray <byte>(ArrayUtilities.Cast <byte>(floats), Allocator.Persistent);
                    request.InvokeCallback(ArrayUtilities.Cast <byte>(floats));
                    break;

                default:
                    throw new NotSupportedException();
                }
            }


            _requestsPool.Enqueue(request);
        }
    }
    public IEnumerator CaptureTest_CaptureColorAndDepth32_FastAndSlow_CheckConsistency()
    {
        byte[]  colorBufferFast = null;
        float[] depthBufferFast = null;

        byte[]  colorBufferSlow = null;
        float[] depthBufferSlow = null;

        var useAsyncReadbackIfSupported = CaptureOptions.useAsyncReadbackIfSupported;

        CaptureOptions.useAsyncReadbackIfSupported = true;
        yield return(CaptureTest_CaptureColorAndDepthParametric
                     (
                         32,
                         GraphicsFormat.R8G8B8A8_UNorm,
                         (AsyncRequest <CaptureCamera.CaptureState> request) =>
        {
            Debug.Assert(request.error == false, "Async request failed");
            colorBufferFast = ArrayUtilities.Cast <byte>(request.data.colorBuffer as Array);
            depthBufferFast = ArrayUtilities.Cast <float>(request.data.depthBuffer as Array);
        }
                     ));

        CaptureOptions.useAsyncReadbackIfSupported = false;
        yield return(CaptureTest_CaptureColorAndDepthParametric
                     (
                         32,
                         GraphicsFormat.R8G8B8A8_UNorm,
                         (AsyncRequest <CaptureCamera.CaptureState> request) =>
        {
            Debug.Assert(request.error == false, "Async request failed");
            colorBufferSlow = ArrayUtilities.Cast <byte>(request.data.colorBuffer as Array);
            depthBufferSlow = ArrayUtilities.Cast <float>(request.data.depthBuffer as Array);
        }
                     ));

        CaptureOptions.useAsyncReadbackIfSupported = useAsyncReadbackIfSupported;

        int count = 0;

        for (var i = 0; i < colorBufferFast.Length; ++i)
        {
            if (colorBufferFast[i] != colorBufferSlow[i])
            {
                ++count;
            }
        }

        Debug.Assert(count == 0, "color buffers differ by " + count);

        count = 0;
        for (var i = 0; i < ArrayUtilities.Count <float>(depthBufferFast); ++i)
        {
            if (Math.Abs(depthBufferFast[i] - depthBufferSlow[i]) > 1e-6f)
            {
                ++count;
            }
        }

        Debug.Assert(count == 0, "depth buffers differ by " + count);
    }
        private DirectorySnapshot ApplyDirectorySnapshotDelta(DirectorySnapshot oldDirectory)
        {
            var oldDirectoryPath = oldDirectory.DirectoryName.RelativePath;

            // Create lists of created dirs and files. We have to access the file system to know
            // if each path is a file or a directory.
            List <IFileInfoSnapshot> createDirs   = null;
            List <IFileInfoSnapshot> createdFiles = null;

            foreach (var path in _pathChanges.GetCreatedEntries(oldDirectoryPath).ToForeachEnum())
            {
                _cancellationToken.ThrowIfCancellationRequested(); // cancellation

                var info = _fileSystem.GetFileInfoSnapshot(_project.RootPath.Combine(path));
                if (info.IsDirectory)
                {
                    if (createDirs == null)
                    {
                        createDirs = new List <IFileInfoSnapshot>();
                    }
                    createDirs.Add(info);
                }
                else if (info.IsFile)
                {
                    if (createdFiles == null)
                    {
                        createdFiles = new List <IFileInfoSnapshot>();
                    }
                    createdFiles.Add(info);
                }
            }

            // Recursively create new directory entires for previous (non deleted)
            // entries.
            var childDirectories = oldDirectory.ChildDirectories
                                   .Where(dir => !_pathChanges.IsDeleted(dir.DirectoryName.RelativePath))
                                   .Select(dir => ApplyDirectorySnapshotDelta(dir))
                                   .ToList();

            // Add created directories
            if (createDirs != null)
            {
                foreach (var info in createDirs.ToForeachEnum())
                {
                    _cancellationToken.ThrowIfCancellationRequested(); // cancellation

                    var createdDirectoryName = _fileSystemNameFactory.CreateDirectoryName(oldDirectory.DirectoryName, info.Path.FileName);
                    var childSnapshot        = CreateDirectorySnapshot(createdDirectoryName, info.IsSymLink);

                    // Note: File system change notifications are not always 100%
                    // reliable. We may get a "create" event for directory we already know
                    // about.
                    var index = childDirectories.FindIndex(x => SystemPathComparer.EqualsNames(x.DirectoryName.Name, createdDirectoryName.Name));
                    if (index >= 0)
                    {
                        childDirectories.RemoveAt(index);
                    }
                    childDirectories.Add(childSnapshot);
                }

                // We need to re-sort the array since we added new entries
                childDirectories.Sort((x, y) => SystemPathComparer.Compare(x.DirectoryName.Name, y.DirectoryName.Name));
            }

            // Match non deleted files
            // Sepcial case: if no file deleted or created, just re-use the list.
            IList <FileName> newFileList;

            if (_pathChanges.GetDeletedEntries(oldDirectoryPath).Count == 0 && createdFiles == null)
            {
                newFileList = oldDirectory.ChildFiles;
            }
            else
            {
                // Copy the list of previous children, minus deleted files.
                var newFileListTemp = oldDirectory.ChildFiles
                                      .Where(x => !_pathChanges.IsDeleted(x.RelativePath))
                                      .ToList();

                // Add created files
                if (createdFiles != null)
                {
                    foreach (var info in createdFiles.ToForeachEnum())
                    {
                        var name = _fileSystemNameFactory.CreateFileName(oldDirectory.DirectoryName, info.Path.FileName);
                        newFileListTemp.Add(name);
                    }

                    // We need to re-sort the array since we added new entries
                    newFileListTemp.Sort((x, y) => SystemPathComparer.Compare(x.Name, y.Name));

                    // Note: File system change notifications are not always 100%
                    // reliable. We may get a "create" event for files we already know
                    // about.
                    ArrayUtilities.RemoveDuplicates(newFileListTemp, (x, y) => SystemPathComparer.EqualsNames(x.Name, y.Name));
                }
                newFileList = newFileListTemp;
            }

            var newData = new DirectoryData(oldDirectory.DirectoryName, oldDirectory.IsSymLink);

            return(new DirectorySnapshot(
                       newData,
                       childDirectories.ToReadOnlyList(),
                       newFileList.ToReadOnlyList()));
        }
    CustomerOrder CreateRandomOrder()
    {
        CustomerOrder newOrder = new CustomerOrder();

        newOrder.OrderNumber     = _ordersIssued + 1;
        newOrder.SpawnDescriptor = CreatureDescriptor.CreateRandomCreatureDescriptor();

        int numDesiredChanges = OrderDesiredChanges.RandomValue;

        CustomerDesire.DesireType[] desiredChangeSequence = new CustomerDesire.DesireType[4] {
            CustomerDesire.DesireType.ChangeColor,
            CustomerDesire.DesireType.ChangeShape,
            CustomerDesire.DesireType.ChangeSize,
            CustomerDesire.DesireType.ChangeAttachments
        };
        ArrayUtilities.KnuthShuffle <CustomerDesire.DesireType>(desiredChangeSequence);

        newOrder.CustomerDesires = new CustomerDesire[numDesiredChanges];
        for (int desireIndex = 0; desireIndex < numDesiredChanges; ++desireIndex)
        {
            CustomerDesire.DesireType desiredChangeType = desiredChangeSequence[desireIndex];
            switch (desiredChangeType)
            {
            case CustomerDesire.DesireType.ChangeAttachments:
            {
                CreatureAttachmentDesire newDesire = new CreatureAttachmentDesire();
                newDesire.AttachmentDescriptor = CritterSpawner.Instance.PickRandomAttachmentDescriptor();
                newDesire.Count = DesiredAttachmentCount.RandomValue;

                newOrder.CustomerDesires[desireIndex] = newDesire;
            }
            break;

            case CustomerDesire.DesireType.ChangeColor:
            {
                CustomerColorDesire newDesire = new CustomerColorDesire();
                newDesire.DesiredColor = CritterConstants.PickRandomCreatureColor();

                newOrder.CustomerDesires[desireIndex] = newDesire;
            }
            break;

            case CustomerDesire.DesireType.ChangeShape:
            {
                CustomerShapeDesire newDesire = new CustomerShapeDesire();
                newDesire.DesiredShape = CritterConstants.PickRandomCreatureShape();

                newOrder.CustomerDesires[desireIndex] = newDesire;
            }
            break;

            case CustomerDesire.DesireType.ChangeSize:
            {
                CustomerSizeDesire newDesire = new CustomerSizeDesire();
                newDesire.DesiredSize = CritterConstants.PickRandomCreatureSize();

                newOrder.CustomerDesires[desireIndex] = newDesire;
            }
            break;
            }
        }

        return(newOrder);
    }
Esempio n. 7
0
        private static void UpdateTagsInFolder(string folderId, string baseFolderId, string serverUrl, OpcNode[] opcNodes)
        {
            // get the set of tags + folders within this folder, so that missing ones can be removed:
            HashSet <string> oldTagNames    = new HashSet <string>(FolderService.GetFolderEntities <OPCTag>(folderId).Select(x => x.EntityName));
            HashSet <string> oldFolderNames = new HashSet <string>(FolderService.GetFolderEntities <Folder>(folderId).Select(x => x.EntityName));

            foreach (OpcNode node in opcNodes)
            {
                if (ArrayUtilities.IsEmpty(node.Children))
                { // create or update tag
                    oldTagNames.Remove(node.Name);
                    OPCTag tag = FolderService.GetFolderEntities <OPCTag>(folderId).FirstOrDefault(x => x.EntityName == node.Name)
                                 ?? new OPCTag();

                    tag.EntityName     = node.Name;
                    tag.EntityFolderID = folderId;
                    tag.Path           = node.FullPath;
                    tag.TypeName       = node.TypeName;
                    tag.ServerUrl      = serverUrl;
                    tag.Store();
                }
                else
                {
                    oldFolderNames.Remove(node.Name);
                    Folder sf = FolderService.GetFolderEntities <Folder>(folderId).FirstOrDefault(x => x.EntityName == node.Name);
                    if (sf == null)
                    {
                        new Folder
                        {
                            EntityName         = node.Name,
                            EntityFolderID     = folderId,
                            FolderBehaviorType = typeof(DefaultFolderBehavior).FullName
                        }.Store();

                        // create, then fetch to make sure id is present.
                        sf = FolderService.GetFolderEntities <Folder>(folderId).FirstOrDefault(x => x.EntityName == node.Name);
                        if (sf == null)
                        {
                            throw new Exception($"Folder {node.Name} could not be created in {folderId}.");
                        }
                    }
                    UpdateTagsInFolder(sf.FolderID, baseFolderId, serverUrl, node.Children);
                }
            }

            foreach (string removedTag in oldTagNames)
            {
                OPCTag tag = FolderService.GetFolderEntities <OPCTag>(folderId).FirstOrDefault(x => x.EntityName == removedTag);
                if (tag != null)
                { // delete tag & matching flow constant:
                    tag.Delete();
                    string flowConstantId = $"OPCTAG__{baseFolderId}__{tag.Path}";
                    new ORM <FlowConstant>().Delete(flowConstantId);
                }
            }
            foreach (string removedFolder in oldFolderNames)
            {
                Folder folder = FolderService.GetFolderEntities <Folder>(folderId).FirstOrDefault(x => x.EntityName == removedFolder);
                if (folder != null)
                {
                    folder.Delete();
                }
            }
        }
 public void Flatten_WhenUninitializedArraySegment_ShouldThrowException()
 {
     ArrayUtilities <int> .Flatten(new[] { new ArraySegment <int>() });
 }
Esempio n. 9
0
 public EuclideanMatrix4Variable(Func <int, int, Variable> initializer) : base(initializer)
 {
     this.variables = ArrayUtilities.Initialize(4, 4, initializer);
 }
Esempio n. 10
0
 /// <summary>
 /// Gets the npv for a collection of coupon cashlows provided.
 /// </summary>
 /// <param name="paymentDiscountFactors">The discount factors.</param>
 /// <param name="yearFractions">The year fractions.</param>
 /// <param name="forwardRates">The forward rates.</param>
 /// <param name="notionals">The notionals</param>
 /// <returns>The break even rate.</returns>
 public static double NPV(double[] notionals, double[] forwardRates, double[] paymentDiscountFactors, double[] yearFractions)
 {
     return(ArrayUtilities.SumProduct(notionals, forwardRates, paymentDiscountFactors, yearFractions));
 }
Esempio n. 11
0
 /// <summary>
 /// Equivalent of .Where(predicate).ToArray(), but optimized to work with ArraySegments.
 /// </summary>
 public TRow[] Materialize(Func <TRow, bool> predicate)
 {
     return(ArrayUtilities <TRow> .Flatten(Segments, predicate));
 }
Esempio n. 12
0
 /// <summary>
 /// Equivalent of .ToArray(), but optimized to work with ArraySegments.
 /// </summary>
 public TRow[] Materialize()
 {
     return(ArrayUtilities <TRow> .Flatten(Segments));
 }
Esempio n. 13
0
    public void SpawnAttachments(CreatureDescriptor SpawnDescriptor)
    {
        // Clear off any previous attachments
        if (attachments.Count > 0)
        {
            for (int i = 0; i < attachments.Count; i++)
            {
                Destroy(attachments[i]);
            }
            attachments.Clear();
        }

        // Gather all available attach points
        attachPoints.Clear();
        attachPoints.AddRange(transform.GetComponentsInChildren <ProtoAttachPoint>(false));

        // Shuffle the indices of the attach points
        int[]  shuffledAttachPointIndices = ArrayUtilities.MakeShuffledIntSequence(0, attachPoints.Count - 1);
        bool[] usedAttachPoint            = new bool[attachPoints.Count];

        // Spawn each attachment using the first valid attach point we can find
        for (int attachmentIndex = 0; attachmentIndex < SpawnDescriptor.Attachments.Length; attachmentIndex++)
        {
            // Get the prefab and the constraints for the attachment
            CreatureAttachmentDescriptor attachmentDescriptor = SpawnDescriptor.AttachmentTypes[attachmentIndex];
            GameObject attachmentPrefab = SpawnDescriptor.Attachments[attachmentIndex];

            // Find the first spawn point that satisfies the constraints
            for (int shuffledListIndex = 0; shuffledListIndex < shuffledAttachPointIndices.Length; ++shuffledListIndex)
            {
                int attachPointIndex = shuffledAttachPointIndices[shuffledListIndex];

                // Skip any attach points that are already used
                if (usedAttachPoint[attachPointIndex])
                {
                    continue;
                }

                // Skip any attach points that violate our max pitch constraint
                ProtoAttachPoint attachPoint      = attachPoints[attachPointIndex];
                Vector3          attachPointUp    = attachPoint.transform.forward;
                float            attachPointPitch = Mathf.Abs(90.0f - Vector3.Angle(attachPointUp, Vector3.up));
                if (attachPointPitch > attachmentDescriptor.MaxAllowedPitchAngle)
                {
                    continue;
                }

                // Spawn the attachment
                GameObject attachment = Instantiate(attachmentPrefab, attachPoint.transform) as GameObject;
                attachment.transform.localPosition    = Vector3.zero;
                attachment.transform.localEulerAngles = new Vector3(0f, 0f, Random.Range(RotateMinMax.x, RotateMinMax.y));
                attachment.transform.localScale      *= Random.Range(ScaleMinMax.x, ScaleMinMax.y);
                attachments.Add(attachment);

                // Mark the attach point as used
                usedAttachPoint[attachPointIndex] = true;

                // Stop searching for a valid attach point
                break;
            }
        }
    }
Esempio n. 14
0
 public void TestArrayContains()
 {
     string[] countries = { "JP", "KR", "CH" };
     Assert.IsTrue(ArrayUtilities <string> .ArrayContains("JP", countries));
 }
 ///<summary>
 ///</summary>
 ///<param name="x"></param>
 ///<param name="y"></param>
 ///<exception cref="NotImplementedException"></exception>
 public void Initialize(double[] x, double[] y)
 {
     Initialize(ArrayUtilities.ArrayToDecimal(x), ArrayUtilities.ArrayToDecimal(y));
 }
Esempio n. 16
0
 public void TotalCount_WhenNotEmpty_ShouldSumTheCount()
 {
     Assert.AreEqual(1, ArrayUtilities <int> .TotalCount(new[] { SegmentOneValue }));
     Assert.AreEqual(10, ArrayUtilities <int> .TotalCount(new[] { TenValues }));
     Assert.AreEqual(3, ArrayUtilities <int> .TotalCount(new[] { SegmentOneValue, SegmentTwoValue }));
 }
Esempio n. 17
0
        private void AddNodeForChildren(FileSystemEntry entry, NodeViewModel oldParent, NodeViewModel newParent)
        {
            Debug.Assert(entry != null);
            Debug.Assert(newParent != null);
            Debug.Assert(newParent.Children.Count == 0);

            // Create children nodes
            var directoryEntry = entry as DirectoryEntry;

            if (directoryEntry != null)
            {
                foreach (var childEntry in directoryEntry.Entries.ToForeachEnum())
                {
                    var child = CreateNodeViewModel(childEntry, newParent);
                    newParent.AddChild(child);
                }
            }

            // Note: It is correct to compare the "Name" property only for computing
            // diffs, as we are guaranteed that both nodes have the same parent, hence
            // are located in the same directory. We also use the
            // System.Reflection.Type to handle the fact a directory can be deleted
            // and then a name with the same name can be added. We need to consider
            // that as a pair of "delete/add" instead of a "no-op".
            var diffs = ArrayUtilities.BuildArrayDiffs(
                oldParent == null ? ArrayUtilities.EmptyList <NodeViewModel> .Instance : oldParent.Children,
                newParent.Children,
                NodeTypeAndNameComparer.Instance);

            foreach (var item in diffs.LeftOnlyItems.ToForeachEnum())
            {
                _changes.DeletedItems.Add(item.ItemId);
            }

            foreach (var newChild in diffs.RightOnlyItems.ToForeachEnum())
            {
                newChild.ItemId = _newNodeNextItemId;
                _newNodeNextItemId++;
                newChild.IsExpanded = newParent.IsRoot;
                _newNodes.AddNode(newChild);

                if (oldParent != null)
                {
                    _changes.AddedItems.Add(newChild.ItemId);
                }
            }

            foreach (var pair in diffs.CommonItems.ToForeachEnum())
            {
                pair.RigthtItem.ItemId     = pair.LeftItem.ItemId;
                pair.RigthtItem.IsExpanded = pair.LeftItem.IsExpanded;
                _newNodes.AddNode(pair.RigthtItem);
            }

            // Call recursively on all children
            if (directoryEntry != null)
            {
                Debug.Assert(directoryEntry.Entries.Count == newParent.Children.Count);
                for (var i = 0; i < newParent.Children.Count; i++)
                {
                    var childEntry   = directoryEntry.Entries[i];
                    var newChildNode = newParent.Children[i];
                    var oldChildNode = GetCommonOldNode(newParent, i, diffs, newChildNode);

                    AddNodeForChildren(childEntry, oldChildNode, newChildNode);
                }
            }
        }
        public void TestIOBindingWithOrtAllocation()
        {
            var inputName  = "data_0";
            var outputName = "softmaxout_1";
            var allocator  = OrtAllocator.DefaultInstance;

            // From the model
            using (var dispList = new DisposableListTest <IDisposable>())
            {
                var tuple       = OpenSessionSqueezeNet();
                var session     = tuple.Item1;
                var inputData   = tuple.Item2;
                var inputTensor = tuple.Item3;
                var outputData  = tuple.Item4;
                dispList.Add(session);
                var runOptions = new RunOptions();
                dispList.Add(runOptions);

                var inputMeta    = session.InputMetadata;
                var outputMeta   = session.OutputMetadata;
                var outputTensor = new DenseTensor <float>(outputData, outputMeta[outputName].Dimensions);

                var ioBinding = session.CreateIoBinding();
                dispList.Add(ioBinding);

                var ortAllocationInput = allocator.Allocate((uint)inputData.Length * sizeof(float));
                dispList.Add(ortAllocationInput);
                var inputShape = Array.ConvertAll <int, long>(inputMeta[inputName].Dimensions, d => d);
                var shapeSize  = ArrayUtilities.GetSizeForShape(inputShape);
                Assert.Equal(shapeSize, inputData.Length);
                PopulateNativeBufferFloat(ortAllocationInput, inputData);

                // Create an external allocation for testing OrtExternalAllocation
                var    cpuMemInfo  = OrtMemoryInfo.DefaultInstance;
                var    sizeInBytes = shapeSize * sizeof(float);
                IntPtr allocPtr    = Marshal.AllocHGlobal((int)sizeInBytes);
                dispList.Add(new OrtSafeMemoryHandle(allocPtr));
                PopulateNativeBuffer(allocPtr, inputData);

                var ortAllocationOutput = allocator.Allocate((uint)outputData.Length * sizeof(float));
                dispList.Add(ortAllocationOutput);

                var outputShape = Array.ConvertAll <int, long>(outputMeta[outputName].Dimensions, i => i);

                // Test 1. Bind the output to OrtAllocated buffer
                using (FixedBufferOnnxValue fixedInputBuffer = FixedBufferOnnxValue.CreateFromTensor(inputTensor))
                {
                    ioBinding.BindInput(inputName, fixedInputBuffer);
                    ioBinding.BindOutput(outputName, Tensors.TensorElementType.Float, outputShape, ortAllocationOutput);
                    ioBinding.SynchronizeBoundInputs();
                    using (var outputs = session.RunWithBindingAndNames(runOptions, ioBinding))
                    {
                        ioBinding.SynchronizeBoundOutputs();
                        Assert.Equal(1, outputs.Count);
                        var output = outputs.ElementAt(0);
                        Assert.Equal(outputName, output.Name);
                        var tensor = output.AsTensor <float>();
                        Assert.True(tensor.IsFixedSize);
                        Assert.Equal(outputData, tensor.ToArray <float>(), new FloatComparer());
                    }
                }

                // Test 2. Bind the input to memory allocation and output to a fixedBuffer
                {
                    ioBinding.BindInput(inputName, Tensors.TensorElementType.Float, inputShape, ortAllocationInput);
                    ioBinding.BindOutput(outputName, Tensors.TensorElementType.Float, outputShape, ortAllocationOutput);
                    ioBinding.SynchronizeBoundInputs();
                    using (var outputs = session.RunWithBindingAndNames(runOptions, ioBinding))
                    {
                        ioBinding.SynchronizeBoundOutputs();
                        Assert.Equal(1, outputs.Count);
                        var output = outputs.ElementAt(0);
                        Assert.Equal(outputName, output.Name);
                        var tensor = output.AsTensor <float>();
                        Assert.True(tensor.IsFixedSize);
                        Assert.Equal(outputData, tensor.ToArray <float>(), new FloatComparer());
                    }
                }
                // 3. Test external allocation
                {
                    var externalInputAllocation = new OrtExternalAllocation(cpuMemInfo, inputShape,
                                                                            Tensors.TensorElementType.Float, allocPtr, sizeInBytes);

                    ioBinding.BindInput(inputName, externalInputAllocation);
                    ioBinding.BindOutput(outputName, Tensors.TensorElementType.Float, outputShape, ortAllocationOutput);
                    ioBinding.SynchronizeBoundInputs();
                    using (var outputs = session.RunWithBindingAndNames(runOptions, ioBinding))
                    {
                        ioBinding.SynchronizeBoundOutputs();
                        Assert.Equal(1, outputs.Count);
                        var output = outputs.ElementAt(0);
                        Assert.Equal(outputName, output.Name);
                        var tensor = output.AsTensor <float>();
                        Assert.True(tensor.IsFixedSize);
                        Assert.Equal(outputData, tensor.ToArray <float>(), new FloatComparer());
                    }
                }
                // 4. Some negative tests for external allocation
                {
                    // Small buffer size
                    Action smallBuffer = delegate()
                    {
                        new OrtExternalAllocation(cpuMemInfo, inputShape,
                                                  Tensors.TensorElementType.Float, allocPtr, sizeInBytes - 10);
                    };

                    Assert.Throws <OnnxRuntimeException>(smallBuffer);

                    Action stringType = delegate()
                    {
                        new OrtExternalAllocation(cpuMemInfo, inputShape,
                                                  Tensors.TensorElementType.String, allocPtr, sizeInBytes);
                    };

                    Assert.Throws <OnnxRuntimeException>(stringType);
                }
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Process a CapFloor ATM parvVols structure.
        /// The process Bootstraps the parVols using the supplied ratecurve
        /// </summary>
        /// <param name="logger">The logger</param>
        /// <param name="cache">The cache.</param>
        /// <param name="nameSpace"></param>
        /// <param name="rateCurve"></param>
        /// <param name="capFloor"></param>
        /// <returns></returns>
        private static Market ProcessCapFloorATM(ILogger logger, ICoreCache cache, string nameSpace, Market rateCurve, CapFloorATMMatrix capFloor)
        {
            var id     = capFloor.id;
            var expiry = capFloor.GetExpiries();
            var vols   = capFloor.GetVolatilities();
            var mkt    = rateCurve;
            var curve  = new SimpleRateCurve(mkt);
            var discountFactorDates = curve.GetDiscountFactorDates();
            var discountFactors     = curve.GetDiscountFactors();
            var volType             = capFloor.GetVolatilityTypes();
            var atmVols             = new Dictionary <string, decimal>();
            var settings            = CreateCapFloorProperties(capFloor.GetVolatilitySettings());
            // Create an internal matrix object from the raw data
            var matrix = new CapFloorVolatilityMatrix(id, expiry, volType, vols, null,
                                                      discountFactorDates, ArrayUtilities.ArrayToDecimal(discountFactors));
            // Create an ATM engine from the matrix
            var engines = CreateEngines(logger, cache, nameSpace, id, matrix, settings);
            // Add the default interpolation to use
            const ExpiryInterpolationType volatilityInterpolation = ExpiryInterpolationType.Linear;

            if (engines != null)
            {
                var vol = new CapletExpiryInterpolatedVolatility(engines[0], volatilityInterpolation);
                // List the values so we can build our ATM vols
                var keys = ExpiryKeys.Split(',');
                // Create a calendar to use to modify the date
                var businessCalendar = settings.GetValue("BusinessCalendar", "AUSY");
                var bc = BusinessCenterHelper.ToBusinessCalendar(cache, new[] { businessCalendar }, nameSpace);
                // Use some logic to get the spot date to use
                // LPM Spot lag is 2 days (modfollowing)
                var spotDate       = curve.GetSpotDate();
                var rollConvention = settings.GetValue("RollConvention", BusinessDayConventionEnum.FOLLOWING);
                spotDate = spotDate == curve.GetBaseDate() ? bc.Roll(spotDate.Add(new TimeSpan(2, 0, 0, 0)), rollConvention) : spotDate;
                //// Extract each surface and build an ATM engine therefrom
                //// Build a list of all possible engines
                foreach (var key in keys)
                {
                    // Calculate the volatility for each target key
                    var tv     = PeriodHelper.Parse(key);
                    var target = tv.period == PeriodEnum.D ? bc.Roll(spotDate.AddDays(Convert.ToInt32(tv.periodMultiplier)), rollConvention) :
                                 bc.Roll(spotDate.AddMonths(Convert.ToInt32(tv.periodMultiplier)), rollConvention);
                    atmVols.Add(key, vol.ComputeCapletVolatility(target));
                }
            }
            var outputVols = new object[atmVols.Count + 1, 2];
            var i          = 1;

            //Expiry	0
            outputVols[0, 0] = "Expiry";
            outputVols[0, 1] = "0";
            foreach (var key in atmVols.Keys)
            {
                outputVols[i, 0] = key;
                outputVols[i, 1] = atmVols[key];
                i++;
            }
            DateTime buildDateTime = rateCurve.Items1[0].buildDateTime;
            var      volSurface    = new VolatilitySurface(outputVols, new VolatilitySurfaceIdentifier(id), curve.BaseDate, buildDateTime);

            return(CreateMarketDocument(volSurface.GetFpMLData()));
        }
Esempio n. 20
0
    public IEnumerator CaptureTestsRedux_Parametric(bool async, bool batched, bool useRT, Channel channel, int depthBits = 32)
    {
        Debug.Assert(depthBits == 16 || depthBits == 32);

        CaptureOptions.useAsyncReadbackIfSupported = async;
#if UNITY_2019_3_OR_NEWER
        CaptureOptions.useBatchReadback = batched;
        if (batched)
        {
            BatchReadback.Instance.BatchSize = 1;
        }
#endif

        var scene = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("Capture.Test.Scene"));

        var block1 = scene.transform.Find("Top Cube").gameObject;
        Debug.Assert(block1 != null);
        var unlitRed = new Material(Shader.Find("Hidden/DataCaptureTestsUnlitShader"));
        unlitRed.color = Color.red;
        block1.GetComponent <MeshRenderer>().sharedMaterial = unlitRed;


        var block2 = scene.transform.Find("Bottom Cube").gameObject;
        Debug.Assert(block2 != null);
        var unlitBlack = new Material(Shader.Find("Hidden/DataCaptureTestsUnlitShader"));
        unlitBlack.color = Color.black;
        block2.GetComponent <MeshRenderer>().sharedMaterial = unlitBlack;

        var camera = Camera.main;
        Debug.Assert(camera != null);

        camera.depthTextureMode = DepthTextureMode.Depth | DepthTextureMode.DepthNormals | DepthTextureMode.MotionVectors;

        var delay = 20;
        while (delay-- > 0)
        {
            yield return(null);
        }

        var colorFormat  = GraphicsFormat.R8G8B8A8_UNorm;
        var depthFormat  = depthBits == 16 ? GraphicsFormat.R16_UNorm : GraphicsFormat.R32_SFloat;
        var motionFormat = GraphicsFormat.R8G8B8A8_UNorm;

        var label = $"CaptureTestRedux_{(async?"Async":"Slow")}{(batched?"Batched":"")}{(useRT?"RT":"")}_{channel.ToString()}_Orientation{(channel == Channel.Depth ? $"_Depth{depthBits}" : "")}";
        var path  = Path.Combine(Application.persistentDataPath, label);

        var request = CaptureDataForChannel(camera, channel, colorFormat, depthFormat, motionFormat);

        var count = 20;
        while (!request.completed && count-- > 0)
        {
            yield return(null);
        }
        Assert.False(request.error);

        switch (channel)
        {
        case Channel.Color:
            Assert.NotNull(request.data.colorBuffer);
            Assert.True(BufferNotEmpty((byte[])request.data.colorBuffer, ArrayUtilities.Count <byte>((byte[])request.data.colorBuffer)));
            Test_Color_Orientation(request.data.camera, colorFormat, (byte[])request.data.colorBuffer, label, path);
            break;

        case Channel.Depth:
            Assert.NotNull(request.data.depthBuffer);
            switch (depthFormat)
            {
            case GraphicsFormat.R32_SFloat:
                Assert.True(BufferNotEmpty(ArrayUtilities.Cast <float>((Array)request.data.depthBuffer), ArrayUtilities.Count((byte[])request.data.depthBuffer)));
                Test_Depth_Orientation_float(request.data.camera, depthFormat, (byte[])request.data.depthBuffer, label, path);
                break;

            case GraphicsFormat.R16_UNorm:
                Assert.True(BufferNotEmpty(ArrayUtilities.Cast <ushort>((Array)request.data.depthBuffer), ArrayUtilities.Count((byte[])request.data.depthBuffer)));
                Test_Depth_Orientation_ushort(request.data.camera, depthFormat, (byte[])request.data.depthBuffer, label, path);
                break;

            default:
                throw new InvalidOperationException("Invalid depth format");
            }
            break;

        case Channel.Motion:
            Assert.Ignore();
            break;
        }

        var rt = camera.targetTexture;
        if (rt != null)
        {
            rt.Release();
            camera.targetTexture = null;
        }

        UnityEngine.Object.DestroyImmediate(scene);
        scene = null;
    }
Esempio n. 21
0
 public FormattableString(string format, object arg1, object arg2, object arg3, object arg4, params object[] args)
 {
     _format = format.EnsureNotNull(nameof(format));
     _args   = ArrayUtilities.ConcatArray(new[] { arg1, arg2, arg3, arg4 }, args ?? __EmptyArgsArray);
 }