Skip to content

Commit

Permalink
Refactored by Sourcery (#145)
Browse files Browse the repository at this point in the history
Co-authored-by: Sourcery AI <[email protected]>
  • Loading branch information
sourcery-ai[bot] and sourcery-ai-bot committed Oct 13, 2021
1 parent 9f2d153 commit 737bbbc
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 77 deletions.
69 changes: 45 additions & 24 deletions Views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ def check_account_fields(fields, new_user=True):
'password2' : r'^.+$',
'website' : r'.*$'
}
good_fields = dict()
for field in regexes:
good_fields = {}
for field, value in regexes.items():
field_input = fields.get(field)
if field_input and re.match(regexes[field], field_input) or field == 'website':
if field_input and re.match(value, field_input) or field == 'website':
good_fields[field] = field_input
uname, password1, password2 = \
fields.get('username'), fields.get('password1'), fields.get('password2')
Expand Down Expand Up @@ -177,7 +177,7 @@ def show_find_pt(URL2):
def case_to_dict(case):
''' Prepare case attributes to be passed to the template. '''

input_dict = dict()
input_dict = {}
for attr in dir(case):
try:
input_dict[attr] = case.__getattribute__(attr)
Expand Down Expand Up @@ -274,7 +274,7 @@ def log_in(request):
Otherwise, return an error page. '''

username, password = request.POST.get('username'), request.POST.get('password')
if not username or not password:
if not (username and password):
return incorrect_login(request)
elif username in TerminatedAccounts.objects.values_list('username', flat=True):
return error('This account was deleted.')
Expand Down Expand Up @@ -346,7 +346,7 @@ def create_account(request):

input_dict = dict(csrf(request))
if 'HTTP_REFERER' in request.META and '/reg_conditions/' in request.META.get('HTTP_REFERER'):
request.session['good_fields'] = dict()
request.session['good_fields'] = {}
input_dict['first_time'] = True
input_dict.update(request.session['good_fields'])
return render_to_response('account_create.html', input_dict)
Expand Down Expand Up @@ -842,14 +842,35 @@ def testcase_admin(request):
caselist = []

for i in Case.objects.all():
inputlist = []
inputlist.append(i.id)
inputlist.append(i.case_name)
inputlist.append(i.Age)
inputlist.append(i.Sex)
inputlist.append('(' + str(i.upright_lat ) + ',' + str(i.upright_lon ) + ');'+'(' + str(i.downright_lat) + ',' + str(i.downright_lon ) + ');'+'(' + str(i.upleft_lat) + ',' + str(i.upleft_lon ) + ');'+'(' + str(i.downleft_lat ) + ',' + str(i.downleft_lon) + ')')
inputlist.append('(' + str(i.findx) + ',' + str(i.findy ) + ')')
inputlist.append(str(i.UploadedLayers))
inputlist = [
i.id,
i.case_name,
i.Age,
i.Sex,
'('
+ str(i.upright_lat)
+ ','
+ str(i.upright_lon)
+ ');'
+ '('
+ str(i.downright_lat)
+ ','
+ str(i.downright_lon)
+ ');'
+ '('
+ str(i.upleft_lat)
+ ','
+ str(i.upleft_lon)
+ ');'
+ '('
+ str(i.downleft_lat)
+ ','
+ str(i.downleft_lon)
+ ')',
'(' + str(i.findx) + ',' + str(i.findy) + ')',
str(i.UploadedLayers),
]

caselist.append(inputlist)

return render_to_response('TestCaseMenu.html',{'case_list':caselist})
Expand Down Expand Up @@ -947,8 +968,8 @@ def evaluate(request):
@login_required
def completed_test(request):
test_name = str(request.GET.get('name')).strip()
completed_lst = list(test.test_name for test in
request.session['active_model'].tests.all() if not test.active)
completed_lst = [test.test_name for test in
request.session['active_model'].tests.all() if not test.active]

if test_name not in completed_lst:
request.session['error'] = 'The test that you requested does not exist.'
Expand Down Expand Up @@ -1002,7 +1023,10 @@ def leaderboard(request):
model.save()

input_dict['case_names'] = Case.objects.values_list('case_name', flat=True)
input_dict['case_categories'] = set(case.subject_category for case in Case.objects.all())
input_dict['case_categories'] = {
case.subject_category for case in Case.objects.all()
}

input_dict['model_data'] = model_data
input_dict['prev_filter'] = filter_choice
return render_to_response('leaderboard.html', input_dict)
Expand Down Expand Up @@ -1323,10 +1347,7 @@ def Account_Profile(request):
# get model descriptions
modellst = []
for i in active_account.account_models.all():
templst = []
templst.append(i.name_id)
templst.append(i.description)
templst.append(account_in)
templst = [i.name_id, i.description, account_in]
modellst.append(templst)

inputdic['modellst'] = modellst
Expand Down Expand Up @@ -1444,13 +1465,13 @@ def upload_casefile(request):
except Case.DoesNotExist:
find_case = None
# Case does exist:
if find_case != None:
row.append("Ignored, name exists")
else:
if find_case is None:
new_case.initialize()
new_case.save()
row.append("Success")

else:
row.append("Ignored, name exists")
return render_to_response('bulkcasereg_complete.html', {'result': data})


Expand Down
35 changes: 19 additions & 16 deletions arc-models/Get_distance_rings.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def getpng(Subject_Category,EcoReg,Terrain):
cursor = arcpy.da.SearchCursor(fctable, ["Name","State",
"Subject_Ca","EcoRegion","Terrain","lat","long_"])
for row in cursor:

namelist.append(row[0]);
stateabbrv.append(row[1]);
subject_category.append(row[2]);
Expand All @@ -280,33 +280,36 @@ def getpng(Subject_Category,EcoReg,Terrain):
for val in nameval:
if (val >='0' and val <= '9'):
keystr = keystr + val

keyval = keystr;
totname = stateabbrv[value] + keyval
key.append(totname);
keystr = ''
#transform to integers
for i in range(len(cat_list)):
raster_name = "C:/Users/Eric Cawi/Documents/SAR/tifs/" + cat_list[i] + ".tif"
transform_name = "C:/Users/Eric Cawi/Documents/SAR/tifs/transform" + cat_list[i] + ".tif"
conversion_name = "C:/Users/Eric Cawi/Documents/SAR/16bittifs/" + cat_list[i] + ".tif"
png_name = "C:/Users/Eric Cawi/Documents/SAR/pngs"
transform_raster = 5*Int(Log2(raster_name))+255

transform_raster.save(transform_name)
arcpy.CopyRaster_management(transform_name, conversion_name,"DEFAULTS","","","","","16_BIT_UNSIGNED","NONE")
arcpy.RasterToOtherFormat_conversion(conversion_name,png_name, "PNG")
png_name = "C:/Users/Eric Cawi/Documents/SAR/pngs"
#transform to integers
for cat in cat_list:
raster_name = "C:/Users/Eric Cawi/Documents/SAR/tifs/" + cat + ".tif"
transform_name = (
"C:/Users/Eric Cawi/Documents/SAR/tifs/transform" + cat + ".tif"
)

conversion_name = "C:/Users/Eric Cawi/Documents/SAR/16bittifs/" + cat + ".tif"
transform_raster = 5*Int(Log2(raster_name))+255

transform_raster.save(transform_name)
arcpy.CopyRaster_management(transform_name, conversion_name,"DEFAULTS","","","","","16_BIT_UNSIGNED","NONE")
arcpy.RasterToOtherFormat_conversion(conversion_name,png_name, "PNG")

for i in range(len(key)):

sc = subject_category[i]
ecoreg = ecoregion_domain[i]
terr = terrain[i]
fname = getpng(sc,ecoreg,terr)#gets the appropriate distance tif for each point in the pointlist
#transform to integers

point_name = "C:/Users/Eric Cawi/Documents/SAR/test_cases/Distance_" + key[i]+".png" # the proper format for the thingy

shutil.copy(fname,point_name)#this should work

exit;
3 changes: 1 addition & 2 deletions arc-models/motion_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ def avg_speed(current_cell, new_theta,dr, walking_speeds, res):
newx, newy = oldx+dx, oldy+dy
first_speed = walking_speeds[oldy][oldx]
last_speed = walking_speeds[newy][newx]
speed = (first_speed+last_speed)/2
return speed
return (first_speed+last_speed)/2

def main():

Expand Down
5 changes: 2 additions & 3 deletions data_analysis/hours_plot-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ def bootstrap_resample(X, n=None):
From http://nbviewer.ipython.org/gist/aflaxman/6871948
"""
if n == None:
if n is None:
n = len(X)

resample_i = np.floor(np.random.rand(n) * len(X)).astype(int)
X_resample = X[resample_i]
return X_resample
return X[resample_i]


def percent_survival(lst):
Expand Down
11 changes: 5 additions & 6 deletions data_analysis/hours_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ def bootstrap_resample(X, n=None):
From http://nbviewer.ipython.org/gist/aflaxman/6871948
"""
if n == None:
if n is None:
n = len(X)

resample_i = np.floor(np.random.rand(n) * len(X)).astype(int)
X_resample = X[resample_i]
return X_resample
return X[resample_i]


mapping = dict() # Key: hours, value: list of subject statuses
Expand Down Expand Up @@ -106,7 +105,7 @@ def is_doa(x):
return 0

x = list(sorted(mapping.keys()))
y = list(percent_survival(mapping[hours]) for hours in x)
y = [percent_survival(mapping[hours]) for hours in x]
N = [len(mapping[hours]) for hours in x]
for hours, survival_rate, n in zip(x, y, N):
print('Survival rate at {} hours = {:.1f}% (N={})'.format(hours, survival_rate, N))
Expand Down Expand Up @@ -137,8 +136,8 @@ def is_doa(x):
elif '-B' in sys.argv:
data, positions, widths = list(), list(), list()
for (lowerbound, upperbound), increment in bin_scheme.items():
bin_x = list(hours for hours in x if lowerbound < hours <= upperbound)
bin_y = list(y[x.index(hours)] for hours in bin_x)
bin_x = [hours for hours in x if lowerbound < hours <= upperbound]
bin_y = [y[x.index(hours)] for hours in bin_x]
data.append(bin_y)
positions.append(np.average(bin_x))
widths.append(min(20, (upperbound - lowerbound) // 1.5))
Expand Down
37 changes: 18 additions & 19 deletions data_analysis/hours_plot_cat2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def read_cases(filename, sheet_index=0):


def group_by_category(hours_slice, status_slice, category_slice):
category_map = dict()
category_map = {}
cases = zip(hours_slice, status_slice, category_slice)
for hours, status, category in cases:
if hours != '' and category != '':
Expand Down Expand Up @@ -105,12 +105,11 @@ def bootstrap_resample(X, n=None):
From http://nbviewer.ipython.org/gist/aflaxman/6871948
"""
if n == None:
if n is None:
n = len(X)

resample_i = np.floor(np.random.rand(n) * len(X)).astype(int)
X_resample = X[resample_i]
return X_resample
return X[resample_i]


def add_trendline(x, y, N, color):
Expand All @@ -125,14 +124,14 @@ def main():
exit(CMD_LINE_HELP.strip().format(sys.argv[0]))
else:
filename = sys.argv[1]

hours_slice, status_slice, category_slice = read_cases(filename)
print('Number of cases: {}'.format(len(hours_slice)))

# Key: category, value: list of (hours, status) points
category_map = group_by_category(hours_slice, status_slice, category_slice)
category_groups = list(get_category_groups(category_map))

# Key: (lowerbound inclusive, upperbound), value: increment size
bin_scheme = {
(0, 12): 1,
Expand All @@ -141,23 +140,23 @@ def main():
(48, 96): 12,
(96, float('inf')): 48
}

colors = 'rgbcyk'
legend_patches, legend_labels = list(), list()
for index, category_group in enumerate(category_groups):
print('Group {}: {}'.format(index + 1, ', '.join(category_group)))

# Key: binned hours, value: list of integers where
# "1" means the subject(s) was/were DOA, "0" means otherwise
outcomes = dict()
outcomes = {}
for category in category_group:
for (hours, status) in category_map[category]:
binned_hours = binify(hours, bin_scheme)
try:
outcomes[binned_hours].append(is_doa(status))
except KeyError:
outcomes[binned_hours] = [is_doa(status)]

# Lists for each number of total hours
_x, _y, _N = list(), list(), list()
for hours in sorted(outcomes.keys()):
Expand All @@ -167,14 +166,14 @@ def main():
_N.append(len(DOA_list))
print('Survival rate at {} hours = {:.1f}% (N={})'.format(
_x[-1], _y[-1], _N[-1]))

# Lists for each (lowerbound, upperbound)
x, y, N = list(), list(), list()
boxplot_data = list()
boxplot_data = []
for lowerbound, upperbound in bin_scheme:
selected_hours = list(hours for hours in _x
if lowerbound <= hours < upperbound)
survival_rates = list(_y[_x.index(hours)] for hours in selected_hours)
selected_hours = [hours for hours in _x
if lowerbound <= hours < upperbound]
survival_rates = [_y[_x.index(hours)] for hours in selected_hours]
x.append(np.average(selected_hours))
y.append(np.average(survival_rates))
N.append(sum(_N[_x.index(hours)] for hours in selected_hours))
Expand All @@ -184,7 +183,7 @@ def main():
ecolor=colors[index], alpha=0.2)
elif '-B' in sys.argv:
boxplot_data.append(survival_rates)

# The points and variations ploted are for the
# data within each (lowerbound, upperbound).
plt.scatter(x, y, c=colors[index], s=N, alpha=0.5)
Expand All @@ -196,13 +195,13 @@ def main():
legend_labels.append(label)
if '-T' in sys.argv[2:]:
add_trendline(_x, _y, _N, colors[index])

if '-B' in sys.argv[2:] and '-E' not in sys.argv[2:]:
# To set the whiskers to the 95% range,
# use the optional argument whis=[2.5, 97.5]
# Only works in matplotlib version > 1.4
plt.boxplot(boxplot_data, positions=x, widths=[10] * len(x))

plt.title('Total Hours vs. Survival Rate')
plt.xlabel('Total Hours (hours)')
plt.ylabel('Survival Rate (percent)')
Expand Down
5 changes: 2 additions & 3 deletions data_analysis/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ def bootstrap_resample(X, n=None):
From http://nbviewer.ipython.org/gist/aflaxman/6871948
"""
if n == None:
if n is None:
n = len(X)

resample_i = np.floor(np.random.rand(n) * len(X)).astype(int)
X_resample = X[resample_i]
return X_resample
return X[resample_i]
Loading

0 comments on commit 737bbbc

Please sign in to comment.