-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
353 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent | ||
|
||
__app_name__ = "kajihs_utils" | ||
__version__ = "0.2.0" | ||
__version__ = "0.3.0" | ||
__authors__ = ["Kajih"] | ||
__author_emails__ = ["[email protected]"] | ||
__repo_url__ = "https://github.com/Kajiih/kajihs_utils" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
Utils for arithmetic. | ||
close_factors and almost_factors taken from: | ||
https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide | ||
""" | ||
|
||
|
||
def closest_factors(n: int, /) -> tuple[int, int]: | ||
""" | ||
Find the closest pair of factors. | ||
Args: | ||
n: The number to find factors for. | ||
Returns: | ||
A tuple containing the two closest factors of n, the larger first. | ||
Example: | ||
>>> close_factors(99) | ||
(11, 9) | ||
""" | ||
factor1 = 0 | ||
factor2 = n | ||
while factor1 + 1 <= factor2: | ||
factor1 += 1 | ||
if n % factor1 == 0: | ||
factor2 = n // factor1 | ||
|
||
return factor1, factor2 | ||
|
||
|
||
def almost_factors(n: int, /, ratio: float = 0.5) -> tuple[int, int]: | ||
""" | ||
Find a pair of factors that are close enough. | ||
Args: | ||
n: The number to almost-factorize. | ||
ratio: The threshold ratio between both factors. | ||
Returns: | ||
A tuple containing the first two numbers factoring to n or more such | ||
that factor 1 is at most 1/ratio times larger than factor 2. | ||
Example: | ||
>>> almost_factors(10, ratio=0.5) | ||
(4, 3) | ||
""" | ||
while True: | ||
factor1, factor2 = closest_factors(n) | ||
if ratio * factor1 <= factor2: | ||
break | ||
n += 1 | ||
return factor1, factor2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Utils for matplotlib.pyplot.""" | ||
|
||
from typing import Any | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.figure import Figure | ||
from numpy import ndarray | ||
|
||
from .arithmetic import ( | ||
almost_factors, | ||
) | ||
|
||
|
||
def auto_subplot( | ||
size: int, /, ratio: float = 9 / 16, **subplot_params: Any | ||
) -> tuple[Figure, ndarray[tuple[int], Any]]: | ||
""" | ||
Automatically creates a subplot grid with an adequate number of rows and columns. | ||
Args: | ||
size: The total number of subplots. | ||
ratio: The threshold aspect ratio between rows and columns. | ||
**subplot_params: Additional keyword parameters for subplot. | ||
Returns: | ||
Tuple containing the figure and the flatten axes. | ||
""" | ||
rows, cols = almost_factors(size, ratio) | ||
|
||
fig, axes = plt.subplots(rows, cols, **subplot_params) | ||
|
||
# if isinstance(axes, np.ndarray): | ||
# axes = axes.flatten() | ||
axes = axes.flatten() | ||
|
||
return fig, axes |
Oops, something went wrong.