Example #1
0
 public static double[] Array(object convertible)
 {
     if (convertible is double[])
     {
         return(convertible as double[]);
     }
     else if (convertible is DateTime[])
     {
         return((convertible as DateTime[]).Select(t => t.ToOADate()).ToArray());
     }
     else if (convertible is IEnumerable <double> )
     {
         return((convertible as IEnumerable <double>).ToArray());
     }
     else if (convertible is IEnumerable <DateTime> )
     {
         return((convertible as IEnumerable <DateTime>).Select(t => t.ToOADate()).ToArray());
     }
     else if ((convertible is IEnumerable <object>) || (convertible is IEnumerable))
     {
         System.Array array = GeneralArray.ToDoubleArray(convertible);
         if (array.Rank == 1)
         {
             return(array as double[]);
         }
         else
         {
             throw new Exception("Array must be one dimensional.");
         }
     }
     else
     {
         throw new Exception("Unknown array type.");
     }
 }
Example #2
0
        public FalseColourImage(IEnumerable <object> underlyingData)
        {
            Array array = GeneralArray.ToDoubleArray(underlyingData);

            this.underlyingData = ((double[, ])array).ArrayEnumerator(EnumerationOrder2D.ColumnMajor);
            width  = array.GetLength(0);
            height = array.GetLength(1);
            Initialize(true);
        }
Example #3
0
        /// <summary>
        /// Create an enumerator suitable for constructing images (i.e. travels along the first dimension,
        /// then the second). If not 2D, first element is null.
        /// </summary>
        /// <param name="enumerable"></param>
        /// <returns></returns>
        public static IEnumerable <double> ToImageEnumerator(IEnumerable <object> enumerable, out int xLength, out int yLength)
        {
            int[] dimensions = GeneralArray.GetDimensions(enumerable);
            xLength = dimensions[0];
            yLength = 0;
            if (dimensions.Length > 2)
            {
                return(null);
            }

            // If this is a list of lists, then create enumerator, otherwise convert to a rectangular array first.
            // This gives better performance if not indexable or if this is a Numpy array.

            Type arrayType = enumerable.GetType();

            if (dimensions.Length == 2 && arrayType.Name != "ndarray" && IsIListOfILists(enumerable))
            {
                List <int> dimensionList = new List <int>();
                IList      parent = enumerable as IList;
                int        minLength = int.MaxValue; int maxLength = int.MinValue;
                int        count = parent.FastCount();

                for (int i = 0; i < count; ++i)
                {
                    IList child = parent[i] as IList;
                    if (child == null)
                    {
                        minLength = 0;
                    }
                    else
                    {
                        minLength = Math.Min(minLength, child.Count);
                        maxLength = Math.Max(maxLength, child.Count);
                    }
                }
                yLength = maxLength;
                if (minLength == 0)
                {
                    return(null);
                }
                else
                {
                    return(Jagged2DImageEnumerable(parent, minLength, maxLength));
                }
            }
            else if (dimensions.Length == 1)
            {
                return(Enumerable1D(enumerable));
            }
            else
            {
                double[,] array = (double[, ])GeneralArray.ToDoubleArray(enumerable);
                yLength         = dimensions[1];
                return(array.ArrayEnumerator(EnumerationOrder2D.ColumnMajor));
            }
        }
Example #4
0
        internal FalseColourImage(Rect bounds, IEnumerable <object> underlyingData, bool newColourBar)
        {
            Array array = GeneralArray.ToDoubleArray(underlyingData);

            this.underlyingData = ((double[, ])array).ArrayEnumerator(EnumerationOrder2D.ColumnMajor);
            width  = array.GetLength(0);
            height = array.GetLength(1);
            Initialize(newColourBar);
            Bounds = bounds;
        }
Example #5
0
        public FalseColourImage AddFalseColourImage(IEnumerable <object> x, IEnumerable <object> y, IEnumerable <object> image)
        {
            int xLength, yLength;
            var xa = GeneralArray.ToImageEnumerator(x, out xLength, out yLength);
            var ya = GeneralArray.ToImageEnumerator(y, out xLength, out yLength);
            FalseColourImage falseColour =
                new FalseColourImage(new Rect(new Point(xa.Min(), ya.Min()), new Point(xa.Max(), ya.Max())), image, true);

            this.Children.Add(falseColour);
            return(falseColour);
        }
Example #6
0
        /// <summary>
        /// Converts object to a
        /// </summary>
        /// <param name="generalArray"></param>
        /// <returns></returns>
        public unsafe static Array ToDoubleArray(object generalArray)
        {
            Type  arrayType    = generalArray.GetType();
            Array managedArray = null;

            #region ILArray
            if (arrayType.Name == "ILArray`1")
            {
                dynamic dynamicArray = generalArray;

                if ((dynamicArray.Dimensions.NumberOfDimensions == 1) ||
                    ((dynamicArray.Dimensions.NumberOfDimensions == 2) && (dynamicArray.Dimensions[1] == 1)))
                {
                    int n = dynamicArray.Dimensions[0];
                    managedArray = new double[n];
                    double[] managedArrayCast = managedArray as double[];
                    for (int i = 0; i < n; ++i)
                    {
                        managedArrayCast[i] = dynamicArray.GetValue(i);
                    }
                    return(managedArray);
                }
                else if (dynamicArray.Dimensions.NumberOfDimensions == 2)
                {
                    int n1 = dynamicArray.Dimensions[0];
                    int n2 = dynamicArray.Dimensions[1];
                    managedArray = new double[n1, n2];
                    double[,] managedArrayCast = managedArray as double[, ];
                    for (int i = 0; i < n1; ++i)
                    {
                        for (int j = 0; j < n2; ++j)
                        {
                            managedArrayCast[i, j] = dynamicArray.GetValue(i, j);
                        }
                    }
                    return(managedArray);
                }
                throw new Exception("Array must be one or two dimensionsal.");
            }
            #endregion
            else if (arrayType.Name == "ndarray")
            {
                managedArray = ManagedArrayFromNumpyArray(generalArray);
            }
            else
            {
                // not Array, ILArray or Numpy array
                int[] dimensions = GeneralArray.GetDimensions(generalArray);
                if (dimensions.Length > 2)
                {
                    throw new Exception("More than two dimensions found.");
                }
                if (dimensions.Length == 1)
                {
                    var      enumerable = generalArray as IEnumerable <object>;
                    double[] tempArray  = new double[enumerable.Count()];
                    int      index      = 0;
                    foreach (object item in enumerable)
                    {
                        tempArray[index] = Convert.ToDouble(item);
                        index++;
                    }
                    managedArray = tempArray;
                }
                else
                {
                    var parent = generalArray as IEnumerable <object>;
                    int minLength = int.MaxValue; int maxLength = int.MinValue;
                    int count = parent.FastCount();
                    foreach (IEnumerable <object> child in parent)
                    {
                        if (child == null)
                        {
                            minLength = 0;
                        }
                        else
                        {
                            minLength = Math.Min(minLength, child.FastCount());
                            maxLength = Math.Max(maxLength, child.FastCount());
                        }
                    }
                    int i = 0;
                    double[,] tempArray = new double[count, maxLength];
                    foreach (IEnumerable <object> child in parent)
                    {
                        int j = 0;
                        foreach (object element in child)
                        {
                            tempArray[i, j] = Convert.ToDouble(element);
                            j++;
                        }
                        while (j < maxLength)
                        {
                            tempArray[i, j] = Double.NaN; ++j;
                        }
                        i++;
                    }
                    managedArray = tempArray;
                }
            }
            return(managedArray);
        }