Skip to content

Commit 5b8e4e4

Browse files
authored
Merge pull request #207 from Rust-Data-Science/wip-rand-float-32
Wip rand float 32
2 parents 48e32a8 + 87117bf commit 5b8e4e4

File tree

7 files changed

+81
-29
lines changed

7 files changed

+81
-29
lines changed

tests/test_constructors.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,6 @@
2323
(ul.arange, (5,), {"step": 2}, [0, 2, 4],),
2424
(ul.arange, (0,), {"stop": 5, "step": 2}, [0, 2, 4],),
2525
26-
(ul.repeat, (0, 3), {}, [0, 0, 0],),
27-
(ul.repeat, (1.0, 3), {}, [1.0, 1.0, 1.0],),
28-
(ul.repeat, (False, 3), {}, [False, False, False],),
29-
(ul.repeat, ('foo', 3), {}, ['foo', 'foo', 'foo'],),
30-
31-
(ul.from_seq, (range(3), "float"), {}, [0.0, 1.0, 2.0],),
32-
(ul.from_seq, (range(3), "int"), {}, [0, 1, 2],),
33-
(ul.from_seq, (range(3), "int32"), {}, [0, 1, 2],),
34-
(ul.from_seq, (range(3), "int64"), {}, [0, 1, 2],),
35-
36-
(ul.from_seq, ([False, True], "bool"), {}, [False, True],),
37-
(ul.from_seq, ([0.0, 1.0, 2.0], "float"), {}, [0.0, 1.0, 2.0],),
38-
(ul.from_seq, ([0, 1, 2], "int"), {}, [0, 1, 2],),
39-
(ul.from_seq, (['foo', 'bar'], "string"), {}, ['foo', 'bar'],),
40-
41-
(ul.from_seq, ((False, True), "bool"), {}, [False, True],),
42-
(ul.from_seq, ((0.0, 1.0, 2.0), "float"), {}, [0.0, 1.0, 2.0],),
43-
(ul.from_seq, ((0, 1, 2), "int"), {}, [0, 1, 2],),
44-
(ul.from_seq, (('foo', 'bar'), "string"), {}, ['foo', 'bar'],),
45-
(ul.from_seq, (('foo', 'bar', None), "string"),
46-
{}, ['foo', 'bar', None],),
47-
4826
(ul.cycle, (range(3), 1, 'float'), {}, [0.0],),
4927
(ul.cycle, (range(3), 1, 'int32'), {}, [0],),
5028
(ul.cycle, (range(3), 1, 'int64'), {}, [0],),
@@ -64,6 +42,29 @@
6442
(ul.cycle, ((0.0, 1.0), 3, 'float'), {}, [0.0, 1.0, 0.0],),
6543
(ul.cycle, ((0, 1), 3, 'int'), {}, [0, 1, 0],),
6644
(ul.cycle, (('foo', 'bar'), 3, 'string'), {}, ['foo', 'bar', 'foo'],),
45+
46+
47+
(ul.from_seq, (range(3), "float"), {}, [0.0, 1.0, 2.0],),
48+
(ul.from_seq, (range(3), "int"), {}, [0, 1, 2],),
49+
(ul.from_seq, (range(3), "int32"), {}, [0, 1, 2],),
50+
(ul.from_seq, (range(3), "int64"), {}, [0, 1, 2],),
51+
52+
(ul.from_seq, ([False, True], "bool"), {}, [False, True],),
53+
(ul.from_seq, ([0.0, 1.0, 2.0], "float"), {}, [0.0, 1.0, 2.0],),
54+
(ul.from_seq, ([0, 1, 2], "int"), {}, [0, 1, 2],),
55+
(ul.from_seq, (['foo', 'bar'], "string"), {}, ['foo', 'bar'],),
56+
57+
(ul.from_seq, ((False, True), "bool"), {}, [False, True],),
58+
(ul.from_seq, ((0.0, 1.0, 2.0), "float"), {}, [0.0, 1.0, 2.0],),
59+
(ul.from_seq, ((0, 1, 2), "int"), {}, [0, 1, 2],),
60+
(ul.from_seq, (('foo', 'bar'), "string"), {}, ['foo', 'bar'],),
61+
(ul.from_seq, (('foo', 'bar', None), "string"),
62+
{}, ['foo', 'bar', None],),
63+
64+
(ul.repeat, (0, 3), {}, [0, 0, 0],),
65+
(ul.repeat, (1.0, 3), {}, [1.0, 1.0, 1.0],),
66+
(ul.repeat, (False, 3), {}, [False, False, False],),
67+
(ul.repeat, ('foo', 3), {}, ['foo', 'foo', 'foo'],),
6768
],
6869
)
6970
def test_constructors(
@@ -84,3 +85,21 @@ def test_constructors(
8485
else:
8586
raise TypeError(f"Unexpected type {type(expected_value[0])}!")
8687
check_test_result(dtype, test_method, result, expected_value)
88+
89+
90+
@pytest.mark.parametrize(
91+
"test_method, args, kwargs",
92+
[
93+
(ul.random, (), {"size": 3, "dtype": "float32"},),
94+
],
95+
)
96+
def test_rand(
97+
test_method: Callable,
98+
args: tuple,
99+
kwargs: dict,
100+
) -> None:
101+
arr1 = test_method(*args, **kwargs)
102+
arr2 = test_method(*args, **kwargs)
103+
assert arr1.not_equal(arr2).all()
104+
assert len(arr1) == len(arr2)
105+
assert len(arr1) == kwargs["size"]

ulist/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ crate-type = ["cdylib"]
1515

1616
[dependencies]
1717
csv = "1.1"
18+
rand = "0.8.5"
1819

1920
[dependencies.pyo3]
2021
version = "0.16.4"

ulist/python/ulist/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .constructor import arange, cycle, from_seq, repeat # noqa:F401
1+
from .constructor import arange, cycle, from_seq, random, repeat # noqa:F401
22
from .control_flow import select # noqa:F401
33
from .core import UltraFastList # noqa:F401
44
from .io import read_csv # noqa:F401

ulist/python/ulist/constructor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,27 @@ def from_seq(obj: Sequence, dtype: str) -> UltraFastList:
192192
return result
193193

194194

195+
def random(size: int, dtype: str) -> UltraFastList:
196+
"""Return a new ulist of random number in [0.0, 1.0).
197+
198+
Args:
199+
size (int):
200+
size (int): Size of the new ulist.
201+
dtype (str):
202+
The type of the output ulist. 'float', 'float32' or 'float64'.
203+
"""
204+
if dtype == "float" or dtype == "float64":
205+
raise NotImplementedError("Not implemented for float64!")
206+
# result = UltraFastList(FloatList64.random(size))
207+
elif dtype == "float32":
208+
result = UltraFastList(FloatList32.random(size))
209+
else:
210+
raise ValueError(
211+
"Parameter dtype should be 'float', 'float32' or 'float64'!"
212+
)
213+
return result
214+
215+
195216
def repeat(elem: ELEM, size: int) -> UltraFastList:
196217
"""Return a new ulist of given size, filled with elem.
197218

ulist/python/ulist/ulist.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class FloatList32:
8989
def pop(self) -> None: ...
9090
def pow_scala(self, elem: NUM) -> FloatList32: ...
9191
@staticmethod
92+
def random(size: int) -> FloatList32: ...
93+
@staticmethod
9294
def repeat(elem: float, size: int) -> FloatList32: ...
9395
def replace(self, old: ELEM_OPT, new: ELEM_OPT) -> FloatList32: ...
9496
def set(self, index: int, elem: ELEM_OPT) -> None: ...

ulist/src/floatings/float32.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::types::AsIntegerList32;
1212
use crate::types::AsIntegerList64;
1313
use crate::types::AsStringList;
1414
use pyo3::prelude::*;
15+
use rand::distributions::Uniform;
16+
use rand::Rng;
1517
use std::cell::Ref;
1618
use std::cell::RefCell;
1719
use std::cell::RefMut;
@@ -135,8 +137,8 @@ impl FloatList32 {
135137
pub fn greater_than_scala(&self, elem: f32) -> BooleanList {
136138
NumericalList::greater_than_scala(self, elem)
137139
}
138-
139-
pub fn has_zero(&self) -> bool{
140+
141+
pub fn has_zero(&self) -> bool {
140142
NumericalList::has_zero(self)
141143
}
142144

@@ -188,6 +190,13 @@ impl FloatList32 {
188190
NumericalList::pow_scala(self, elem)
189191
}
190192

193+
#[staticmethod]
194+
fn random(size: usize) -> Self {
195+
let range: Uniform<f32> = Uniform::from(0.0..1.0);
196+
let v: Vec<f32> = rand::thread_rng().sample_iter(&range).take(size).collect();
197+
FloatList32::new(v, HashSet::new())
198+
}
199+
191200
#[staticmethod]
192201
pub fn repeat(elem: f32, size: usize) -> Self {
193202
List::repeat(elem, size)

ulist/src/floatings/float64.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl List<f64> for FloatList64 {
297297
impl NumericalList<f64, i32, f64> for FloatList64 {
298298
fn argmax(&self) -> PyResult<usize> {
299299
self._check_empty()?;
300-
self._check_all_na()?;
300+
self._check_all_na()?;
301301
let vec = self.values();
302302
let hset = self.na_indexes();
303303
let val_0 = &f64::NEG_INFINITY;
@@ -311,7 +311,7 @@ self._check_all_na()?;
311311

312312
fn argmin(&self) -> PyResult<usize> {
313313
self._check_empty()?;
314-
self._check_all_na()?;
314+
self._check_all_na()?;
315315
let vec = self.values();
316316
let hset = self.na_indexes();
317317
let val_0 = &f64::INFINITY;
@@ -339,7 +339,7 @@ self._check_all_na()?;
339339

340340
fn max(&self) -> PyResult<f64> {
341341
self._check_empty()?;
342-
self._check_all_na()?;
342+
self._check_all_na()?;
343343
let hset = self.na_indexes();
344344
Ok(*self
345345
.values()
@@ -353,7 +353,7 @@ self._check_all_na()?;
353353

354354
fn min(&self) -> PyResult<f64> {
355355
self._check_empty()?;
356-
self._check_all_na()?;
356+
self._check_all_na()?;
357357
let hset = self.na_indexes();
358358
Ok(*self
359359
.values()

0 commit comments

Comments
 (0)