Skip to content

Commit 9cd8ce7

Browse files
committed
renaming modules
1 parent b921943 commit 9cd8ce7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2262
-1742
lines changed

examples/.DS_Store

6 KB
Binary file not shown.

examples/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "/usr/local/bin/python3"
3+
}

examples/feature_extraction/ellipses.ipynb

Lines changed: 237 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# %%
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
from matplotlib.patches import Ellipse
5+
from multiprocessing import Pool
6+
from pybalu.feature_extraction.geometric_utils import bbox as _bbox
7+
from pybalu.feature_extraction import basic_geo_features
8+
from pybalu.io import imread
9+
from skimage.measure import label
10+
11+
# %%
12+
import matplotlib
13+
matplotlib.rcParams["figure.figsize"] = (7, 7)
14+
matplotlib.rcParams["axes.titlesize"] = 20
15+
matplotlib.rcParams["axes.titlepad"] = 15
16+
matplotlib.rcParams["figure.figsize"] = (7, 7)
17+
del matplotlib
18+
19+
# %%
20+
im = imread("feature_extraction/rice.png")
21+
plt.title("Original Image", fontdict={"fontsize": 20}, pad=20)
22+
plt.imshow(im, cmap="gray")
23+
plt.show()
24+
25+
# %%
26+
im_bin = (im > 140).astype(int)
27+
labeled, n = label(im_bin, return_num=True)
28+
labeled_T = labeled.T
29+
30+
31+
def calc_ellipse(idx):
32+
region = (labeled_T == idx).astype(int)
33+
box = _bbox(region)
34+
feats = basic_geo_features(region[box])
35+
# feats[0]: center of grav i [px]
36+
# feats[1]: center of grav j [px]
37+
# feats[10]: MajorAxisLength [px]
38+
# feats[11]: MinorAxisLength [px]
39+
# feats[12]: Orientation [deg]
40+
return np.array([box[0].start + feats[0], box[1].start + feats[1], feats[10], feats[11], feats[12]])
41+
42+
43+
with Pool() as pool:
44+
ellipses = np.vstack(pool.map(calc_ellipse, range(1, n)))
45+
46+
# %%
47+
ax = plt.axes()
48+
plt.title("Segmented Image")
49+
plt.imshow(im, cmap="gray")
50+
51+
52+
def draw_ellipse(x, y, height, width, angle, axes):
53+
ell = Ellipse(xy=(x, y), height=height, width=width,
54+
angle=angle, edgecolor="red", facecolor="none")
55+
axes.add_artist(ell)
56+
ell.set_clip_box(axes.bbox)
57+
return ell
58+
59+
60+
for ell in ellipses:
61+
draw_ellipse(*ell, axes=ax)
62+
63+
plt.show()
64+
65+
# %%
66+
plt.title("Mean sized rice grains (major axis)")
67+
68+
major_25 = np.percentile(ellipses[:, 3], 25)
69+
major_75 = np.percentile(ellipses[:, 3], 75)
70+
71+
valid_labels = 1 + \
72+
np.where((ellipses[:, 3] > major_25) & (ellipses[:, 3] < major_75))[0]
73+
im_mean = np.array(im)
74+
im_mean[np.where(~np.isin(labeled, valid_labels))] //= 7
75+
plt.imshow(im_mean, cmap="gray")
76+
plt.show()
77+
78+
# %%
79+
80+
81+
def draw_at_angle(theta):
82+
valid_labels = 1 + \
83+
np.where((ellipses[:, 4] > theta - 10) &
84+
(ellipses[:, 4] < theta + 10))[0]
85+
plt.title(f"Orientation at {theta} deg")
86+
im_rotated = np.array(im)
87+
im_rotated[np.where(~np.isin(labeled, valid_labels))] //= 7
88+
plt.imshow(im_rotated, cmap="gray")
89+
plt.show()
90+
91+
92+
draw_at_angle(45)

examples/feature_extraction/rice.png

313 KB
Loading
1.97 MB
Binary file not shown.

examples/feature_selection/sfs.ipynb

Lines changed: 161 additions & 0 deletions
Large diffs are not rendered by default.

examples/feature_selection/sfs.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# %%
2+
from scipy.io import loadmat
3+
4+
# load a dataset with 810 samples and 294 features
5+
data = loadmat("feature_selection/realdata")
6+
features = data["features"]
7+
classes = data["classes"].squeeze()
8+
9+
# %%
10+
from pybalu.data_selection import stratify
11+
from pybalu.feature_transformation import normalize
12+
13+
# Training and Testing data (90% training, 10% testing)
14+
idx_train, idx_test = stratify(classes, .90)
15+
f_train = features[idx_train]
16+
c_train = classes[idx_train]
17+
f_test = features[idx_test]
18+
c_test = classes[idx_test]
19+
20+
f_train_norm, a, b = normalize(f_train)
21+
f_test_norm = f_test * a + b
22+
23+
# %%
24+
from pybalu.feature_selection import sfs
25+
26+
N_FEATURES = 15
27+
28+
selected_feats = sfs(f_train_norm, c_train, n_features=N_FEATURES,
29+
method="fisher", show=True)
30+
31+
32+
# %%
33+
from pybalu.classification import structure
34+
from pybalu.performance_eval import performance
35+
from sklearn.neighbors import KNeighborsClassifier
36+
import matplotlib.pyplot as plt
37+
38+
def performance_for_features(feat_idxs):
39+
# train classifier
40+
knn = KNeighborsClassifier(n_neighbors=3)
41+
knn.fit(f_train_norm[:, feat_idxs], c_train)
42+
43+
# predict and evaluate performance
44+
prediction = knn.predict(f_test_norm[:, feat_idxs])
45+
return performance(prediction, c_test)
46+
47+
48+
values = [performance_for_features(selected_feats[:i]) * 100
49+
for i in range(1, N_FEATURES + 1)]
50+
51+
plt.plot(values)
52+
plt.title("Performance vs. number of features")
53+
plt.xlabel('selected features')
54+
plt.ylabel('accuracy [%]')
55+
plt.show()

pybalu/__init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from . import classification
2-
from . import data_selection
3-
from . import feature_analysis
4-
from . import feature_extraction
5-
from . import feature_selection
6-
from . import feature_transformation
7-
from . import img_processing
8-
from . import io
9-
from . import misc
10-
from . import performance_eval
1+
# modules
112

12-
__version__ = '0.2.1'
3+
__all__ = ["classification",
4+
"data_selection",
5+
"feature_analysis",
6+
"feature_extraction",
7+
"feature_selection",
8+
"feature_transformation",
9+
"img_processing",
10+
"io",
11+
"misc",
12+
"performance_eval"]
13+
14+
__version__ = '0.2.2'

pybalu/classification/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
from ._structure import *
2-
# from ._construct import *
1+
from .structure import structure
2+
3+
__all__ = ["structure"]

0 commit comments

Comments
 (0)