Helpful utilities for python development
Project description
frills 1.3.0
Helpful utilities for python development
[!NOTE] Requires python >=3.7
datascience
-
interpret_array_str()takes one positional argumentarray_strthat is expected to be a string representation of a list or ndarray. The function also takes one optional parameteruse_typethat can be used to force the resultant type of the interpreted list/array items. -
smart_load_pickle()takes one argumentfilepathwhich is expected to point at a .pkl (pickle) file. The function repeatedly calls pickle.load() on the file until EOF is met. This is useful for recovering all data from a pickle file that was written to multiple times.
Example usage:
import frills.datascience as fds
result = fds.interpret_array_str("[1, 2, 3]", use_type=int)
print(result) # [1, 2, 3]
print(type(result)) # <class 'list'>
print(type(result[0])) # <class 'int'>
result = fds.interpret_array_str("[1.0, 2.2, 3.5]", use_type=int)
print(result) # [1, 2, 3]
print(type(result)) # <class 'list'>
print(type(result[0])) # <class 'int'>
result = fds.interpret_array_str("[1.0, 2.0, 3.0]", use_type=float)
print(result) # [1.0, 2.0, 3.0]
print(type(result)) # <class 'list'>
print(type(result[0])) # <class 'float'>
result = fds.interpret_array_str("[1 2 3]", use_type=int)
print(result) # [1 2 3]
print(type(result)) # <class 'numpy.ndarray'>
print(type(result[0])) # <class 'numpy.int64'>
result = fds.interpret_array_str("[1.0 2.2 3.5]", use_type=float)
print(result) # [1. 2.2 3.5]
print(type(result)) # <class 'numpy.ndarray'>
print(type(result[0])) # <class 'numpy.float64'>
# = = = = = = #
results = smart_load_pickle("path/to/pickle/file.pkl")
print(type(results)) # list of dicts or objects
maths
-
get_factors()takes one argumentnand returns all factors ofn. This is useful for picking appropriate batch sizes in custom data loaders. -
normalize_to_range()takes the following arguments:xthe data (expected to be a numpy array)min_origthe original lower bound of the datamax_origthe original upper bound of the datamin_newthe minimum of the new scalemax_newthe maximum of the new scale
-
euclidean_distance()takesvector_aandvector_bwhich are expected to be typelistornumpy.ndarray. -
cumulative_mean()takesnew_value,current_meanandnwherenis the number of items accumulated bycurrent_meanso far. -
percentage()takesn(numerator),total(denominator) and an optional parameterdp(number of decimal places) and returns a formatted string representing the percentage. -
report_progress()takesn(numerator),total(denominator) and optional parametersinterval(reporting frequency) anddp(number of decimal places). This can be used inside a loop to conveniently track progress.
Example usage:
import frills.maths as fm
# = = = = = = #
# given dataset with 192 samples
factors = fm.get_factors(192)
print(factors) # [1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96]
batch_size = max(factors) # ensures that each batch is the same size, with no empty rows
# = = = = = = #
# arbitrary values on a scale of 1-1000
x = [540, 800, 250]
# convert to color values (0-255)
rgb = []
for value in x:
rgb.append(int(fm.normalise_to_range(value, 1, 1000, 0, 255)))
print(rgb) # [137, 203, 63]
# = = = = = = #
x, y = [3, 6, 9], [1, 2, 7]
dist = fm.euclidean_distance(x, y)
print(dist) # 4.898979485566356
# = = = = = = #
x = [15, 20, 25]
mean = sum(x) / len(x)
del x # no longer have the original values
y = 50
mean = fm.cumulative_mean(y, mean, 3)
print(mean) # 27.5
# = = = = = = #
print(fm.percentage(3, 40, dp=1)) # 7.5%
# = = = = = = #
n_max = 128
for i in range(n_max):
# do_work_here()
fm.report_progress(i, n_max, interval=20, dp=3)
# 0.0%
# 15.625%
# 31.25%
# 46.875%
# 62.5%
# 78.125%
# 93.75%
# = = = = = = #
graphics
All show functions take the arguments message and image for the window title and image to be displayed.
-
showw()waits for user input (any key) before continuing execution. This is useful for checking through a series of images, allowing the user to skip as quickly or slowly as they want. -
showx()shows the specified image, then exits the program entirely once the cv2 window is closed. This is useful for checking the contents of an image dataset are as expected without having to manually halt further execution.
Example usage:
import frills.graphics as fg
import numpy as np
import cv2
# load image
img = cv2.imread("images.png")
img.astype(np.uint8)
# shows until user input
fg.showw("test 1", img)
# shows until window is closed
# then terminates the program
fg.showx("test 2", img)
# never shows
cv2.imshow("test 3", img)
cv2.waitKey(0)
debugging
-
printw()prints a string built from the arguments given and waits for used input (any key) before continuing execution. This is useful for stepping through a program slowly and analysing the contents of variables one at a time. -
printx()prints a string built from the arguments given, then exits the program entirely. This is useful for checking the contents of a variable and breaking there, rather than having to comment the rest of the code or usesys.exit()underneath aprint().
Example usage:
import frills.debugging as fd
# prints and waits for user input before continuing
fd.printw("this")
# prints and terminates the program
fd.printx("that")
# never prints
print("the other")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file frills-1.3.0.tar.gz.
File metadata
- Download URL: frills-1.3.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4c93d656bc9e351614875077b148a0a197aa5c5b9ee6dd9dccd5970236c1304
|
|
| MD5 |
8a1a8757ee7a7c9f356b8ff60863d509
|
|
| BLAKE2b-256 |
f45698b496ab2c7e2bb18604ff88b6ec8cdc8ba3307e7a5e9fd4c9a082b6ca61
|
File details
Details for the file frills-1.3.0-py3-none-any.whl.
File metadata
- Download URL: frills-1.3.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f28927873434ff9b5aaf67d9d96ffe0d2286150a1f764cf139aade45d763477
|
|
| MD5 |
0d7e23989d83acaacbbb98b560b6cb3a
|
|
| BLAKE2b-256 |
2832f7abd09136f59aafccf2faa94bde8b094c7a20459e31da4a7c2bd8e3e6e2
|