Skip to content

Commit

Permalink
WIP on the numpy structured arrays returned
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoledoux committed May 27, 2024
1 parent 14df380 commit 19084a2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
22 changes: 22 additions & 0 deletions demo/temp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import startinpy
import numpy as np
import laspy
import math
import sys
import json


dt = startinpy.DT()
dt.add_attribute_map([("intensity", "f64"), ("visited", "i64")])

dt.insert_one_pt([20.0, 30.0, 1.1], intensity=111.2, visited=4);
dt.insert_one_pt([120.0, 33.0, 12.5], intensity=111.2, visited=5);
dt.insert_one_pt([124.0, 222.0, 7.65], intensity=111.2, visited=6);
dt.insert_one_pt([20.0, 133.0, 21.0], intensity=111.2, visited=7);
dt.insert_one_pt([23.0, 13.0, 11.0], intensity=111.2, visited=8);
dt.insert_one_pt([60.0, 60.0, 33.0], intensity=111.2, visited=9);


print(dt)

print(dt.all_attributes()[1:])
41 changes: 21 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,16 +443,26 @@ impl DT {

// #[pyo3(text_signature = "($self, dtype)")]
#[args()]
pub fn all_attributes<'py>(&self, py: Python<'py>) -> PyResult<()> {
// let am = self.t.list_all_attributes();
// Get the numpy module
pub fn all_attributes<'py>(&self, py: Python<'py>) -> PyResult<PyObject> {
let np = py.import("numpy")?;
// Define the dtype for the structured array
let dtype = np.call_method1("dtype", (vec![("intensity", "f8"), ("visited", "i8")],))?;
// let dmap = self.t.list_all_attributes();
let mut vmap: Vec<(String, String)> = Vec::new();
for (key, dtype) in &self.t.list_all_attributes() {
match dtype.as_ref() {
"f64" => vmap.push((String::from(key), "f8".to_string())),
"i64" => vmap.push((String::from(key), "i8".to_string())),
"u64" => vmap.push((String::from(key), "u8".to_string())),
"bool" => vmap.push((String::from(key), "b1".to_string())),
"String" => vmap.push((String::from(key), "U10".to_string())),
&_ => continue,
}
}
println!("{:?}", vmap);
let dtype = np.call_method1("dtype", (vmap,))?;
// let dtype = np.call_method1("dtype", (vec![("intensity", "f8"), ("visited", "i8")],))?;

let allt = self.t.all_attributes().unwrap();
// Create an empty array with the defined dtype
let arraydtype = np.call_method1("empty", (allt.len(), dtype))?;

for (i, each) in allt.iter().enumerate() {
let item = arraydtype.get_item(i)?;
let o = each.as_object().unwrap();
Expand All @@ -462,22 +472,13 @@ impl DT {
}
match o.get("visited") {
Some(x) => item.set_item("visited", x.as_i64())?,
None => item.set_item("visited", std::f64::NAN)?,
None => item.set_item("visited", std::i64::MIN)?,
}
}
// println!("{:?}", arraydtype);

// // Fill the structured array with data
// for (i, name) in names.iter().enumerate() {
// let item = arraydtype.get_item(i)?;
// item.set_item("name", name)?;
// item.set_item("age", ages[i])?;
// item.set_item("height", heights[i])?;
// }

println!("{:?}", arraydtype);

Ok(())
// Ok(arraydtype.into())
// Ok(())
Ok(arraydtype.into())
}

/// Get all the values for a given extra attribute stored for the vertices.
Expand Down

0 comments on commit 19084a2

Please sign in to comment.