diff --git a/Views.py b/Views.py index 0b2c6e0..08bcc64 100644 --- a/Views.py +++ b/Views.py @@ -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') @@ -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) @@ -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.') @@ -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) @@ -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}) @@ -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.' @@ -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) @@ -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 @@ -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}) diff --git a/arc-models/Get_distance_rings.py b/arc-models/Get_distance_rings.py index 52d52c3..a5c392b 100644 --- a/arc-models/Get_distance_rings.py +++ b/arc-models/Get_distance_rings.py @@ -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]); @@ -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; diff --git a/arc-models/motion_model.py b/arc-models/motion_model.py index f757703..b18582c 100644 --- a/arc-models/motion_model.py +++ b/arc-models/motion_model.py @@ -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(): diff --git a/data_analysis/hours_plot-1.py b/data_analysis/hours_plot-1.py index a0b1f17..16127fb 100644 --- a/data_analysis/hours_plot-1.py +++ b/data_analysis/hours_plot-1.py @@ -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): diff --git a/data_analysis/hours_plot.py b/data_analysis/hours_plot.py index bbd9884..f4d60b9 100644 --- a/data_analysis/hours_plot.py +++ b/data_analysis/hours_plot.py @@ -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 @@ -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)) @@ -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)) diff --git a/data_analysis/hours_plot_cat2.py b/data_analysis/hours_plot_cat2.py index cab63e3..852f1c2 100644 --- a/data_analysis/hours_plot_cat2.py +++ b/data_analysis/hours_plot_cat2.py @@ -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 != '': @@ -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): @@ -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, @@ -141,15 +140,15 @@ 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) @@ -157,7 +156,7 @@ def main(): 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()): @@ -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)) @@ -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) @@ -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)') diff --git a/data_analysis/resample.py b/data_analysis/resample.py index 50f4c92..8dd9286 100644 --- a/data_analysis/resample.py +++ b/data_analysis/resample.py @@ -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] diff --git a/data_analysis/weather.py b/data_analysis/weather.py index 5e6beeb..f8dfcc2 100644 --- a/data_analysis/weather.py +++ b/data_analysis/weather.py @@ -59,8 +59,7 @@ def request_data(endpoint, safe=':,', **parameters): urllib.parse.urlencode(parameters, safe=safe)) request = urllib.request.Request(url, headers={'token': API_TOKEN}) response = urllib.request.urlopen(request) - data = json.loads(response.read().decode('utf-8')) - return data + return json.loads(response.read().decode('utf-8')) def get_bounds(coordinates, d): @@ -111,8 +110,7 @@ def get_stations(date, coordinates, datasetid='GHCND', d=20): enddate=date_as_str, extent=extent ) - for station in data['results']: - yield station + yield from data['results'] def get_conditions(date, coordinates):