|
| 1 | +use polars::datatypes::PlHashSet; |
| 2 | +use polars::export::arrow::array::PrimitiveArray; |
| 3 | +use polars::export::num::Float; |
| 4 | +use polars::prelude::*; |
| 5 | +use pyo3_polars::export::polars_core::utils::arrow::types::NativeType; |
| 6 | +use pyo3_polars::export::polars_core::with_match_physical_integer_type; |
| 7 | +use std::hash::Hash; |
| 8 | + |
| 9 | +#[allow(clippy::all)] |
| 10 | +pub(super) fn naive_hamming_dist(a: &str, b: &str) -> u32 { |
| 11 | + let x = a.as_bytes(); |
| 12 | + let y = b.as_bytes(); |
| 13 | + x.iter() |
| 14 | + .zip(y) |
| 15 | + .fold(0, |a, (b, c)| a + (*b ^ *c).count_ones() as u32) |
| 16 | +} |
| 17 | + |
| 18 | +fn jacc_helper<T: NativeType + Hash + Eq>(a: &PrimitiveArray<T>, b: &PrimitiveArray<T>) -> f64 { |
| 19 | + // convert to hashsets over Option<T> |
| 20 | + let s1 = a.into_iter().collect::<PlHashSet<_>>(); |
| 21 | + let s2 = b.into_iter().collect::<PlHashSet<_>>(); |
| 22 | + |
| 23 | + // count the number of intersections |
| 24 | + let s3_len = s1.intersection(&s2).count(); |
| 25 | + // return similarity |
| 26 | + s3_len as f64 / (s1.len() + s2.len() - s3_len) as f64 |
| 27 | +} |
| 28 | + |
| 29 | +pub(super) fn naive_jaccard_sim(a: &ListChunked, b: &ListChunked) -> PolarsResult<Float64Chunked> { |
| 30 | + polars_ensure!( |
| 31 | + a.inner_dtype() == b.inner_dtype(), |
| 32 | + ComputeError: "inner data types don't match" |
| 33 | + ); |
| 34 | + polars_ensure!( |
| 35 | + a.inner_dtype().is_integer(), |
| 36 | + ComputeError: "inner data types must be integer" |
| 37 | + ); |
| 38 | + Ok(with_match_physical_integer_type!(a.inner_dtype(), |$T| { |
| 39 | + polars::prelude::arity::binary_elementwise(a, b, |a, b| { |
| 40 | + match (a, b) { |
| 41 | + (Some(a), Some(b)) => { |
| 42 | + let a = a.as_any().downcast_ref::<PrimitiveArray<$T>>().unwrap(); |
| 43 | + let b = b.as_any().downcast_ref::<PrimitiveArray<$T>>().unwrap(); |
| 44 | + Some(jacc_helper(a, b)) |
| 45 | + }, |
| 46 | + _ => None |
| 47 | + } |
| 48 | + }) |
| 49 | + })) |
| 50 | +} |
| 51 | + |
| 52 | +fn haversine_elementwise<T: Float>(start_lat: T, start_long: T, end_lat: T, end_long: T) -> T { |
| 53 | + let r_in_km = T::from(6371.0).unwrap(); |
| 54 | + let two = T::from(2.0).unwrap(); |
| 55 | + let one = T::one(); |
| 56 | + |
| 57 | + let d_lat = (end_lat - start_lat).to_radians(); |
| 58 | + let d_lon = (end_long - start_long).to_radians(); |
| 59 | + let lat1 = (start_lat).to_radians(); |
| 60 | + let lat2 = (end_lat).to_radians(); |
| 61 | + |
| 62 | + let a = ((d_lat / two).sin()) * ((d_lat / two).sin()) |
| 63 | + + ((d_lon / two).sin()) * ((d_lon / two).sin()) * (lat1.cos()) * (lat2.cos()); |
| 64 | + let c = two * ((a.sqrt()).atan2((one - a).sqrt())); |
| 65 | + r_in_km * c |
| 66 | +} |
| 67 | + |
| 68 | +pub(super) fn naive_haversine<T>( |
| 69 | + start_lat: &ChunkedArray<T>, |
| 70 | + start_long: &ChunkedArray<T>, |
| 71 | + end_lat: &ChunkedArray<T>, |
| 72 | + end_long: &ChunkedArray<T>, |
| 73 | +) -> PolarsResult<ChunkedArray<T>> |
| 74 | +where |
| 75 | + T: PolarsFloatType, |
| 76 | + T::Native: Float, |
| 77 | +{ |
| 78 | + let out: ChunkedArray<T> = start_lat |
| 79 | + .into_iter() |
| 80 | + .zip(start_long.into_iter()) |
| 81 | + .zip(end_lat.into_iter()) |
| 82 | + .zip(end_long.into_iter()) |
| 83 | + .map(|(((start_lat, start_long), end_lat), end_long)| { |
| 84 | + let start_lat = start_lat?; |
| 85 | + let start_long = start_long?; |
| 86 | + let end_lat = end_lat?; |
| 87 | + let end_long = end_long?; |
| 88 | + Some(haversine_elementwise( |
| 89 | + start_lat, start_long, end_lat, end_long, |
| 90 | + )) |
| 91 | + }) |
| 92 | + .collect(); |
| 93 | + |
| 94 | + Ok(out.with_name(start_lat.name())) |
| 95 | +} |
0 commit comments