|
| 1 | +namespace Plotly.NET |
| 2 | + |
| 3 | +open DynamicObj |
| 4 | +open System |
| 5 | + |
| 6 | +/// Data type tag for encoded typed arrays accepted by plotly.js (>= 2.28.0). |
| 7 | +/// Serializes to the shorthand strings documented in the plotly.js release notes |
| 8 | +/// (e.g. "f8" for Float64, "u1c" for UInt8Clamped). |
| 9 | +[<RequireQualifiedAccess>] |
| 10 | +type TypedArrayDType = |
| 11 | + | Float64 |
| 12 | + | Float32 |
| 13 | + | Int32 |
| 14 | + | UInt32 |
| 15 | + | Int16 |
| 16 | + | UInt16 |
| 17 | + | Int8 |
| 18 | + | UInt8 |
| 19 | + | UInt8Clamped |
| 20 | + |
| 21 | + static member toString = |
| 22 | + function |
| 23 | + | Float64 -> "f8" |
| 24 | + | Float32 -> "f4" |
| 25 | + | Int32 -> "i4" |
| 26 | + | UInt32 -> "u4" |
| 27 | + | Int16 -> "i2" |
| 28 | + | UInt16 -> "u2" |
| 29 | + | Int8 -> "i1" |
| 30 | + | UInt8 -> "u1" |
| 31 | + | UInt8Clamped -> "u1c" |
| 32 | + |
| 33 | + static member convert = TypedArrayDType.toString >> box |
| 34 | + override this.ToString() = this |> TypedArrayDType.toString |
| 35 | + member this.Convert() = this |> TypedArrayDType.convert |
| 36 | + |
| 37 | + |
| 38 | +/// <summary> |
| 39 | +/// Base64-encoded typed array representation accepted by plotly.js (>= 2.28.0) for trace data_array fields. |
| 40 | +/// |
| 41 | +/// Carries a base64-encoded byte payload (<c>bdata</c>), the underlying value data type (<c>dtype</c>), |
| 42 | +/// and an optional <c>shape</c> for multi-dimensional arrays. See the plotly.js v2.28.0 release notes. |
| 43 | +/// </summary> |
| 44 | +type EncodedTypedArray() = |
| 45 | + inherit DynamicObj() |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Returns a new EncodedTypedArray with the given base64 payload, dtype tag, and optional shape. |
| 49 | + /// Use this overload when you already own the base64 encoding (e.g. from numpy-style pipelines). |
| 50 | + /// </summary> |
| 51 | + /// <param name="bdata">The base64-encoded contents of the typed array.</param> |
| 52 | + /// <param name="dtype">The data type of individual values in the payload.</param> |
| 53 | + /// <param name="shape">Optional array shape. Required for multi-dimensional arrays, omittable for 1-D. Serialized as a comma-separated string.</param> |
| 54 | + static member init |
| 55 | + ( |
| 56 | + bdata: string, |
| 57 | + dtype: TypedArrayDType, |
| 58 | + ?shape: seq<int> |
| 59 | + ) = |
| 60 | + EncodedTypedArray() |
| 61 | + |> EncodedTypedArray.style ( |
| 62 | + bdata = bdata, |
| 63 | + dtype = dtype, |
| 64 | + ?shape = shape |
| 65 | + ) |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Returns a function that applies the given bdata/dtype/shape to an existing EncodedTypedArray. |
| 69 | + /// </summary> |
| 70 | + static member style |
| 71 | + ( |
| 72 | + bdata: string, |
| 73 | + dtype: TypedArrayDType, |
| 74 | + ?shape: seq<int> |
| 75 | + ) = |
| 76 | + fun (eta: EncodedTypedArray) -> |
| 77 | + eta |
| 78 | + |> DynObj.withProperty "bdata" bdata |
| 79 | + |> DynObj.withProperty "dtype" (TypedArrayDType.convert dtype) |
| 80 | + |> DynObj.withOptionalPropertyBy "shape" shape (fun s -> String.Join(",", s |> Seq.map string)) |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Creates an EncodedTypedArray from a 1-D Float64 array, base64-encoding the underlying bytes. |
| 84 | + /// Pass <paramref name="shape"/> to declare a multi-dimensional layout over the flat data. |
| 85 | + /// </summary> |
| 86 | + static member ofFloat64Array (data: float[], ?shape: seq<int>) = |
| 87 | + let bytes = Array.zeroCreate<byte> (data.Length * sizeof<float>) |
| 88 | + Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length) |
| 89 | + EncodedTypedArray.init (Convert.ToBase64String bytes, TypedArrayDType.Float64, ?shape = shape) |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Creates an EncodedTypedArray from a 1-D Float32 array. |
| 93 | + /// </summary> |
| 94 | + static member ofFloat32Array (data: single[], ?shape: seq<int>) = |
| 95 | + let bytes = Array.zeroCreate<byte> (data.Length * sizeof<single>) |
| 96 | + Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length) |
| 97 | + EncodedTypedArray.init (Convert.ToBase64String bytes, TypedArrayDType.Float32, ?shape = shape) |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Creates an EncodedTypedArray from a 1-D Int32 array. |
| 101 | + /// </summary> |
| 102 | + static member ofInt32Array (data: int32[], ?shape: seq<int>) = |
| 103 | + let bytes = Array.zeroCreate<byte> (data.Length * sizeof<int32>) |
| 104 | + Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length) |
| 105 | + EncodedTypedArray.init (Convert.ToBase64String bytes, TypedArrayDType.Int32, ?shape = shape) |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Creates an EncodedTypedArray from a 1-D UInt32 array. |
| 109 | + /// </summary> |
| 110 | + static member ofUInt32Array (data: uint32[], ?shape: seq<int>) = |
| 111 | + let bytes = Array.zeroCreate<byte> (data.Length * sizeof<uint32>) |
| 112 | + Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length) |
| 113 | + EncodedTypedArray.init (Convert.ToBase64String bytes, TypedArrayDType.UInt32, ?shape = shape) |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Creates an EncodedTypedArray from a 1-D Int16 array. |
| 117 | + /// </summary> |
| 118 | + static member ofInt16Array (data: int16[], ?shape: seq<int>) = |
| 119 | + let bytes = Array.zeroCreate<byte> (data.Length * sizeof<int16>) |
| 120 | + Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length) |
| 121 | + EncodedTypedArray.init (Convert.ToBase64String bytes, TypedArrayDType.Int16, ?shape = shape) |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Creates an EncodedTypedArray from a 1-D UInt16 array. |
| 125 | + /// </summary> |
| 126 | + static member ofUInt16Array (data: uint16[], ?shape: seq<int>) = |
| 127 | + let bytes = Array.zeroCreate<byte> (data.Length * sizeof<uint16>) |
| 128 | + Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length) |
| 129 | + EncodedTypedArray.init (Convert.ToBase64String bytes, TypedArrayDType.UInt16, ?shape = shape) |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Creates an EncodedTypedArray from a 1-D Int8 (signed byte) array. |
| 133 | + /// </summary> |
| 134 | + static member ofInt8Array (data: sbyte[], ?shape: seq<int>) = |
| 135 | + let bytes = Array.zeroCreate<byte> data.Length |
| 136 | + Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length) |
| 137 | + EncodedTypedArray.init (Convert.ToBase64String bytes, TypedArrayDType.Int8, ?shape = shape) |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Creates an EncodedTypedArray from a 1-D UInt8 (byte) array. |
| 141 | + /// </summary> |
| 142 | + static member ofUInt8Array (data: byte[], ?shape: seq<int>) = |
| 143 | + EncodedTypedArray.init (Convert.ToBase64String data, TypedArrayDType.UInt8, ?shape = shape) |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Creates an EncodedTypedArray from a byte array, tagged as UInt8Clamped (maps to JS Uint8ClampedArray). |
| 147 | + /// </summary> |
| 148 | + static member ofUInt8ClampedArray (data: byte[], ?shape: seq<int>) = |
| 149 | + EncodedTypedArray.init (Convert.ToBase64String data, TypedArrayDType.UInt8Clamped, ?shape = shape) |
0 commit comments