Skip to content

Commit

Permalink
configured flake8 for linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi-Zha0 committed Feb 10, 2021
1 parent de027f1 commit dda4ceb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
ignore = E722, E226, W504
max-line-length = 120
exclude = tests/*, .git. __pycache__, env, examples
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ before-install:
install:
- pip install -r requirements.txt
script:
- pytest --cov=./
- pytest --cov=./ --flake8
after-success:
- bash <(curl -s https://codecov.io/bash)
5 changes: 3 additions & 2 deletions code/shot_stats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np


class shot_stats:
'''
Takes in a DataFrame of shot data generated by target_reader.py and
Expand Down Expand Up @@ -116,7 +117,7 @@ def cont_to_hist(self, series, bin_min=0, bin_max=None, num_bins=11):
list (int): histogram of series as a list with indices as bins
list (float): heatmap color values corresponding to histogram
'''
if bin_max == None:
if bin_max is None:
bin_max = self.trg_size
bins = np.linspace(bin_min, bin_max, num_bins, endpoint=False)[1:]
bin_vals = np.searchsorted(bins, series)
Expand Down Expand Up @@ -188,4 +189,4 @@ def get_reco_list(self, x_vals, y_vals, actual_score, op_score):
reco_list.extend([hori_reco, vert_reco])
else:
reco_list.extend([vert_reco, hori_reco])
return reco_list
return reco_list
27 changes: 13 additions & 14 deletions code/target_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class variable 'df' and image steps in 'stage_images.'
self.df = None

# Loads image from file if it exists
if type(file_obj) == type(None):
if file_obj is None:
image = cv2.imread(filename, 1)

# Loads image from werkzeug file object
Expand All @@ -79,7 +79,7 @@ class variable 'df' and image steps in 'stage_images.'
image = None

# Convert image to proper color channels
if type(image) == type(None):
if image is None:
return 'Could not read image file'
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
self.orig_image = image
Expand Down Expand Up @@ -136,7 +136,7 @@ def remove_skew(self):
# Finds the longest contour that can be approximated by four coordinates
# Returns error status if no such contour exists
_, contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

paper = None
max_perim = 0
for c in contours:
Expand All @@ -146,7 +146,7 @@ def remove_skew(self):
if len(approx) == 4:
paper = approx
max_perim = perim
if type(paper) == type(None):
if paper is None:
return True

# Reorders corner points to Top Left, Top Right, Bottom Right,
Expand All @@ -173,7 +173,7 @@ def remove_skew(self):
[new_w, 0],
[new_w, new_h],
[0, new_h]
], dtype = 'float32')
], dtype='float32')
M = cv2.getPerspectiveTransform(bounds, new_bounds)
img = cv2.warpPerspective(img, M, (new_w, new_h))

Expand Down Expand Up @@ -319,12 +319,12 @@ def balance_contrast(self):
layers = 0
for mask in circle_masks:
# Creates histogram and extracts most frequent value bin
hist = cv2.calcHist([gray], [0], mask, [32], [0,256])
hist = cv2.calcHist([gray], [0], mask, [32], [0, 256])
mode_idx = np.argmax(hist)
limit = hist[mode_idx] * pct_max

# Calculates upper and lower value bounds
limit_idx = np.argwhere(hist < limit)[:,0]
limit_idx = np.argwhere(hist < limit)[:, 0]
try:
low_val = max(limit_idx[limit_idx < mode_idx]) * 8
except:
Expand Down Expand Up @@ -411,7 +411,7 @@ def find_shots(self):
img = cv2.drawKeypoints(self.image,
self.keypoints,
np.array([]),
(0,255,0),
(0, 255, 0),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Saves image to class variables and returns status
Expand Down Expand Up @@ -441,12 +441,12 @@ def get_shot_data(self):
arrow_x.append(k.pt[0])
arrow_y.append(k.pt[1])
arrow_radii.append(k.size / 2)
df = pd.DataFrame({'x':arrow_x, 'y':arrow_y, 'radius':arrow_radii})
df = pd.DataFrame({'x': arrow_x, 'y': arrow_y, 'radius': arrow_radii})

# Calculates how many shots created a hole based on the mean radius of
# the smallest holes
num_smallest = np.ceil(len(arrow_radii) * pct_smallest).astype(int)
single_size = (np.mean(np.sort(arrow_radii)[:num_smallest]) *
single_size = (np.mean(np.sort(arrow_radii)[: num_smallest]) *
overlap_penalty)
df['count'] = np.clip(df['radius'] // single_size, 0, max_overlapped)
df['count'] = df['count'].replace(0, 1).astype(int)
Expand All @@ -466,8 +466,7 @@ def get_shot_data(self):
clus_df['radius'] /= 2
clus_df['vec_x'] = center - clus_df['x']
clus_df['vec_y'] = center - clus_df['y']
clus_df['mag'] = np.sqrt(np.square(clus_df['vec_x']) +
np.square(clus_df['vec_y']))
clus_df['mag'] = np.sqrt(np.square(clus_df['vec_x']) + np.square(clus_df['vec_y']))
clus_df['vec_x'] = (clus_df['vec_x'] / clus_df['mag'] *
clus_df['radius'])
clus_df['vec_y'] = (clus_df['vec_y'] / clus_df['mag'] *
Expand Down Expand Up @@ -517,7 +516,7 @@ def read_image(self, filename=None, file_obj=None):
self.df = None

# Loads image from file if it exists
if type(file_obj) == type(None):
if file_obj is None:
image = cv2.imread(filename, 1)

# Loads image from werkzeug file object
Expand All @@ -529,7 +528,7 @@ def read_image(self, filename=None, file_obj=None):
image = None

# Convert image to proper color channels
if type(image) == type(None):
if image is None:
return 'Could not read image file'
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
self.orig_image = image
Expand Down
29 changes: 17 additions & 12 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
attrs==20.2.0
certifi==2020.6.20
chardet==3.0.4
Click==7.0
codecov==2.1.10
coverage==5.3
flake8==3.8.4
Flask==1.1.1
gunicorn==19.9.0
idna==2.10
importlib-metadata==2.0.0
iniconfig==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
mccabe==0.6.1
numpy==1.16.4
opencv-python==3.4.2.17
packaging==20.4
pandas==0.24.2
pkg-resources==0.0.0
pluggy==0.13.1
py==1.9.0
pycodestyle==2.6.0
pyflakes==2.2.0
pyparsing==2.4.7
pytest==6.1.1
pytest-cov==2.10.1
python-dateutil==2.8.0
pytz==2019.1
requests==2.24.0
six==1.15.0
toml==0.10.1
urllib3==1.25.11
zipp==3.4.0
Click==7.0
Flask==1.1.1
gunicorn==19.9.0
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
numpy==1.16.4
opencv-python==3.4.2.17
pandas==0.24.2
python-dateutil==2.8.0
pytz==2019.1
Werkzeug==0.15.5
zipp==3.4.0

0 comments on commit dda4ceb

Please sign in to comment.