Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int generator and test #293

Merged
25 changes: 25 additions & 0 deletions synthetic_data/distinct_generators/int_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import numpy as np
from ..base_generator import BaseGenerator
from numpy.random import Generator

JGSweets marked this conversation as resolved.
Show resolved Hide resolved

def random_integers(rng: Generator,
min_value: int = -1e6,
max_value: int = 1e6,
num_rows: int = 1
) -> np.array:
"""
Randomly generates an array of integers between a min and max value

:param rng: the np rng object used to generate random values
:type rng: numpy Generator
:param min_value: the minimum integer that can be returned
:type min_value: int, optional
:param max_value: the maximum integer that can be returned
:type max_value: int, optional
:param num_rows: the number of rows in np array generated
:type num_rows: int, optional

:return: np array of integers
"""
return rng.integers(min_value, max_value, (num_rows,))
34 changes: 34 additions & 0 deletions tests/test_int_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import unittest
import pandas as pd
import numpy as np

from synthetic_data.distinct_generators.int_generator import random_integers

JGSweets marked this conversation as resolved.
Show resolved Hide resolved

class TestIntGenerator(unittest.TestCase):
def setUp(self):
self.rng = np.random.default_rng(12345)

def test_return_type(self):
result = random_integers(self.rng)
self.assertIsInstance(result, np.ndarray)
for num in result:
self.assertIsInstance(num, np.int64)

def test_size(self):
num_rows = [5,20,100]
for nr in num_rows:
result = random_integers(self.rng, num_rows=nr)
self.assertEqual(result.shape[0], nr)
result = random_integers(self.rng)
self.assertEqual(result.shape[0], 1)

def test_values_range(self):
ranges = [(-1,1), (-10,10), (-100, 100)]
for range in ranges:
result = random_integers(self.rng, range[0], range[1])
for x in result:
self.assertGreaterEqual(x, range[0])
self.assertLessEqual(x, range[1])

drahc1R marked this conversation as resolved.
Show resolved Hide resolved