Skip to content

Commit

Permalink
feat(size_2_square): add function to calculate the square needed for …
Browse files Browse the repository at this point in the history
…the size
  • Loading branch information
Axel Fahy committed Jan 8, 2020
1 parent aa89450 commit df5a3ff
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ As of *v0.2*, plots are not yet tested in the travis build.
* 0.2.5
* ADD: Function ``log_df`` to print function results during method chaining.
* ADD: Function ``avg_dicts`` to make the average of multiple similar dictionaries.
* ADD: Function ``size_2_square`` to calculate the square needed for the given size (e.g. in subplots).
* ADD: Option ``with_identity`` to plot an identity line in the ``plot_true_vs_pred`` function.
* ADD: Option ``with_determination`` to plot the coefficient of determination in the ``plot_true_vs_pred`` function.
* ADD: Function ``size_to_square`` to calculate automatically the size of the side of a square needed to store all the elements.
Expand Down
2 changes: 2 additions & 0 deletions bff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
normalization_pd,
parse_date,
read_sql_by_chunks,
size_2_square,
sliding_window,
value_2_list,
)
Expand All @@ -38,6 +39,7 @@
'parse_date',
'plot',
'read_sql_by_chunks',
'size_2_square',
'sliding_window',
'FancyConfig',
'value_2_list',
Expand Down
22 changes: 22 additions & 0 deletions bff/fancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,28 @@ def read_sql_by_chunks(sql: str, cnxn, params: Union[List, Dict, None] = None,
return res


def size_2_square(n: int) -> Tuple[int, int]:
"""
Return the size of the side to create a square able to contain n elements.
This is mainly used to have the correct sizes of the sides when creating squared subplots.
Parameters
----------
n: int
Number of elements that need to fit inside the square.
Returns
-------
Tuple of int
Tuple of int with the size of each part of the square.
Both element of the tuple are similar.
"""
size_float = math.sqrt(n)
size_int = int(size_float) if size_float.is_integer() else math.ceil(size_float)
return (size_int, size_int)


def sliding_window(sequence: Sequence, window_size: int, step: int):
"""
Apply a sliding window over the sequence.
Expand Down
12 changes: 11 additions & 1 deletion tests/test_fancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from bff.fancy import (avg_dicts, cast_to_category_pd, concat_with_categories, get_peaks,
idict, kwargs_2_list, log_df, mem_usage_pd, normalization_pd,
parse_date, sliding_window, value_2_list)
parse_date, size_2_square, sliding_window, value_2_list)


class TestFancy(unittest.TestCase):
Expand Down Expand Up @@ -298,6 +298,16 @@ def dummy_function_custom(**kwargs):
self.assertEqual(dummy_function(date='wrong format')['date'],
'wrong format')

def test_size_2_square(self):
"""
Test of the `size_2_square` function.
"""
# Test when result is a square.
self.assertEqual(size_2_square(9), (3, 3))

# Test when not a square.
self.assertEqual(size_2_square(10), (4, 4))

def test_sliding_window(self):
"""
Test of the `sliding_window` function.
Expand Down

0 comments on commit df5a3ff

Please sign in to comment.