How to make a np array of arrays - arrays

Hey all i imagine this could have been answered however I can not find what i'm looking for exactly. Here is the code bellow:
positiveData = np.array([])
negativeData = []
with AedatFile('someFile') as f:
# loop through the "frames" stream
for e in f['events'].numpy():
for event in e:
time, x, y, polarity, _, _ = event
if polarity == 1:
data = np.array([time, x, y, polarity])
print(data)
positiveData = np.append(positiveData,data)
print(positiveData)
else:
data = [time, x, y, polarity]
negativeData.append(data)
I am expecting the code to look like this:
[[1,2,3,4],
[1,2,3,4],
....]
I plan on using this to make a 3d plot so i want an array so i can easily plot3d(array[0][:],array[1][:],array[2][:])
cheers all.
Here is the a sample set of data that was asked for below. I cant paste more as it says my comment is mostly code and wont allow me to post more without adding more text which incredibly stupid.
[(1612584805989190, 254, 304, 1, 0, 0)
(1612584805989190, 254, 283, 1, 0, 0)
(1612584805989190, 254, 286, 1, 0, 0) ...
(1612584805999148, 596, 20, 1, 0, 0)
(1612584805999162, 549, 60, 1, 0, 0)
(1612584805999189, 461, 225, 0, 0, 0)]
[(1612584806009235, 512, 31, 1, 0, 0)
(1612584806009263, 419, 274, 1, 0, 0)
(1612584806009287, 338, 188, 0, 0, 0) ...
(1612584806019188, 214, 241, 0, 0, 0)
(1612584806019188, 214, 237, 0, 0, 0)
(1612584806019189, 211, 234, 0, 0, 0)]

Try modifying to
positiveData = np.append( positiveData, [data] )
or
data = np.array( [[ time, x, y, polarity ]] )
The append function will append (embed) the array given to inside the array target
https://numpy.org/doc/stable/reference/generated/numpy.append.html

Related

macro word "VBA error too many line continuations"

I have a long variant array. I was trying to break it with (, _) but I still got the same error.
for example:
arr as variant
arr = array(15,54,10,15,0,0,0,51,12,36,23,15,52,115,132,16,13,18,0,0,0,0,0,0,0,2,0,51,13, _,
,1,25,15,31,81,35,64,31,,0,0,0,0,2,0,5,1,4,3,150,1,91,156,151,51,150,1,0,0,0, _ , ...
maybe the array length is 30 lines.
any help on how to solve this error?
Can't you take your data another way? or do you need to generate random numbers? I think it will take more explanation on what you plan to do to help yourself.
So far the only thing I have found is that your array is filled with a function. I don't know if that will satisfy you as an answer.
Sub main()
Dim arr() As Variant
arr = Array(15, 54)
Call AddArray(arr, Array(15, 54, 10, 15, 0, 0, 0, 51, 12, 36, 23, 15, 52, 115, 132, 16, 13, 18, 0, 0, 0, 0, 0, 0, 0, 2, 0, 51, 13))
Call AddArray(arr, Array(1, 25, 15, 31, 81, 35, 64, 31, , 0, 0, 0, 0, 2, 0, 5, 1, 4, 3, 150, 1, 91, 156, 151, 51, 150, 1, 0, 0, 0))
End Sub
Function AddArray(ByRef arr As Variant, ByRef arrAdd As Variant) As Variant
' "UBound(arr) - LBound(arr) + 1" : Is the calcul for get length of array
LengthArr = UBound(arr) - LBound(arr) + 1
LengthArrAdd = UBound(arrAdd) - LBound(arrAdd) + 1
'Calcul the length of futur array
LengthNewArr = LengthArr + LengthArrAdd - 1
'Redimension the arr
ReDim Preserve arr(LBound(arr) To LengthNewArr)
'Fill the arr with i who start to new espace of array at end
For i = LengthArr To LengthNewArr
arr(i) = arrAdd(i - LengthArr)
Next
'AddArray = arr
End Function
result : arr(0) = 15, arr(1) = 54, ..., ..., arr(59) = 0, arr(60) = 0

Why do the bytes of a PNG image downloaded with reqwest differ from those downloaded with Python?

I'm trying to use reqwest library to download a PNG file, but when I download it I see a strange behaviour respect other programming languages like: Python.
For instance:
let content = reqwest::get("https://www.google.es/images/searchbox/desktop_searchbox_sprites302_hr.png").await?;
If I print the result as a bytes array (println!("{:?}", content.text().await?.as_bytes());
[ 191, 189, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 40, 0, 0, 0, 82, 8, 3, 0, 0, 0, 17, 191, 189, 102, 191, 189, 0, 0, 0, 108, 80, 76, 84, 69, 0, 0, 0, 191, 189, 191, 189, 191, 189,...]
However, the result using Python requests is:
[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 40, 0, 0, 0, 82, 8, 3, 0, 0, 0, 17, 153, 102, 248, ...]
In the Rust version, I see a lot of 191, 189. This sequence repeats a lot throughout the array, but in Python doesn't appear at all.
What am I doing wrong in Rust?
I see a lot of 191, 189
It's better seen as EF, BF, BD, which is the Unicode replacement character encoded as UTF-8. Binary data is not text data. You should not use text for binary data, instead use bytes.
const URL: &str = "https://www.google.es/images/searchbox/desktop_searchbox_sprites302_hr.png";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let content = reqwest::get(URL).await?;
let bytes = content.bytes().await?;
println!("{:x?}", &bytes[..]);
Ok(())
}
[89, 50, 4e, 47, d, a, 1a, a, 0, 0, 0, d, 49, 48, 44, 52, 0, 0, 0, 28, 0, 0, 0, 52, 8, 3, 0, 0, 0, 11, 99, 66, f8, 0, 0, 0, 6c, 50, 4c, 54, 45, 0, 0, 0, 9f, ...

Byte data Parsing in flutter

hi there i need to parse the byte data of a response from api the data is
[0, 2, 0, 44, 0, 6, 58, 1, 0, 1, 109, 85, 0, 0, 0, 1, 0, 1, 111, 97, 0, 115, 224, 4, 0, 0, 0, 0, 0, 0, 1, 114, 0, 1, 115, 169, 0, 1, 116, 18, 0, 1, 108, 121, 0, 1, 113, 241, 0, 44, 0, 13, 128, 1, 0, 0, 55, 200, 0, 0, 0, 10, 0, 0, 55, 227, 5, 172, 149, 3, 0, 0, 84, 154, 0, 0, 0, 0, 0, 0, 56, 79, 0, 0, 57, 28, 0, 0, 54, 226, 0, 0, 56, 89]
there are certain rules to parse the data according to api provider as below
A The first two bytes ([0 - 2] -- SHORT or int16) represent the number of packets in the message.
B The next two bytes ([2 - 4] -- SHORT or int16) represent the length (number of bytes) of the first packet.
C The next series of bytes ([4 - 4+B]) is the quote packet.
D The next two bytes ([4+B - 4+B+2] -- SHORT or int16) represent the length (number of bytes) of the second packet.
C The next series of bytes ([4+B+2 - 4+B+2+D]) is the next quote packet.
please someone help me how to parse this data according to these rules in dart. i am stuck thanks for help
First, you need to convert the list of integers into a list of bytes:
final byteList = Uint8List.fromList(responseList);
Then you need to create a ByteData from that byte list:
final byteData = ByteData.view(byteList.buffer);
Then you can do your parsing of various bytes, shorts, ints, or longs that you want at various byte offsets. For example:
final packetNum = byteData.getUint16(0);
final firstPacketLength = byteData.getUint16(2);
final firstPacketView = ByteData.sublistView(byteData, 4, 4 + firstPacketLength);
// Do whatever you need for that packet
final secondPacketPos = 4 + firstPacketLength;
final secondPacketLength = byteData.getUint16(secondPacketPos),
final secondPacketView = ByteData.sublistView(byteData, secondPacketPos + 2, 4 + secondPacketLength);
// Do whatever you need for the saecond packet

collapsing a cube by subtracting dimensions

I have a cube that contains the probability density based on 3 variables of the shape [t1,t2,gamma] with 80 values in each direction. I need to plot the distribution of T, gamma. Where T is t2-t1.
Is there some clever way to collapse this cube into the desired result? I've been breaking my head on it and I can't find one.
You can pad with zeros and then shear the array by incrementing or decrementing a dimension by one:
# make small diagnostic example
nt1, nt2, ngm = 4, 5, 2
data = sum(np.ogrid[1:nt1+1,-1:-nt2-1:-1,100:100*ngm+100:100])
# by construction values are equal if coordinates (T,gamma) are equal, no matter how T = t2-t1 decomposes.
# Fixing gamma, for example at 1, we can see that T is constant along the diagonals
data[..., 1]
# array([[200, 199, 198, 197, 196],
# [201, 200, 199, 198, 197],
# [202, 201, 200, 199, 198],
# [203, 202, 201, 200, 199]])
# now let's transform the example, first recover dimensions
nt1, nt2, ngm = data.shape
# next, zero pad
aux = np.zeros((nt1+2, nt1+nt2-2, ngm), data.dtype)
aux[1:-1, :nt2] = data
# and shear, in this case by incrementing dimension 1
sheared = aux.reshape(-1, ngm)[nt2-1:3-nt1-nt2].reshape(nt1, nt1+nt2-1, ngm)
# check result, for example at gamma = 1
sheared[..., 1]
# array([[ 0, 0, 0, 200, 199, 198, 197, 196],
# [ 0, 0, 201, 200, 199, 198, 197, 0],
# [ 0, 202, 201, 200, 199, 198, 0, 0],
# [203, 202, 201, 200, 199, 0, 0, 0]])
# corresponding values of T are now aligned and ready for further processing.

Gzip sequence of tuples and then unzip again into sequence of tuples - issue when unzipping the sequence

I have a sequence of Tuples that I need to gzip for storage. Afterwards I want to be able to extract the compressed content, decompress it and then get the Sequence of tuples back.
I use the following code for de/compressing:
def unzip(x: Array[Byte]) : String = {
val inputStream = new GZIPInputStream(new ByteArrayInputStream(x))
val output = scala.io.Source.fromInputStream(inputStream).mkString
return output
}
def gzip(input: Array[Byte]): Array[Byte] = {
val bos = new ByteArrayOutputStream(input.length)
val gzip = new GZIPOutputStream(bos)
gzip.write(input)
gzip.close()
val compressed = bos.toByteArray
bos.close()
compressed
}
As taken from this source https://gist.github.com/owainlewis/1e7d1e68a6818ee4d50e .
Then my routine more or less is the following:
val arr = Seq(("a",1),("b",2))
val arr_bytes = arr.toString.getBytes
val comp = compress(arr_bytes)
val x = unzip(comp)
The output is the following:
arr: Seq[(String, Int)] = List((a,1), (b,2))
arr_bytes: Array[Byte] = Array(76, 105, 115, 116, 40, 40, 97, 44, 49, 41, 44, 32, 40, 98, 44, 50, 41, 41)
comp: Array[Byte] = Array(31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, -55, 44, 46, -47, -48, 72, -44, 49, -44, -44, 81, -48, 72, -46, 49, -46, -44, 4, 0, 35, 120, 118, -118, 18, 0, 0, 0)
x: String = List((a,1), (b,2))
The problem is x is now a String that has the format from above (with the word List contained as well).
For example:
x.toList
res174: List[Char] = List(L, i, s, t, (, (, a, ,, 1, ), ,, , (, b, ,, 2, ), ))
My question is, how do I decompress my exact sequence back, or how do I make x into my previous sequence again?
Solved it using the play api json library for storing the content in json objects:
val arr = Json.toJson(Array(Json.obj("name"-> "Bran", "age" -> 13),Json.obj("name"-> "Jon", "age" -> 18)))
val arr_bytes = arr.toString().getBytes
val comp = compress(arr_bytes)
val x= unzip(comp)
val json = Json.parse(x)

Resources