def get_studies(val=None): x, y, z, radius = get_params(val) points = Peak.closestPeaks(radius, x, y, z) # Track number of peaks and study details for each found study, # keeping only peaks that haven't been previously seen for current # study/x/y/z combination. seen = {} study_counts = defaultdict(list) for p in points: key = hash((p.pmid, round(p.x, 2), round(p.y, 2), round(p.z, 2))) if key in seen: next study_counts[p.pmid].append(p) seen[key] = 1 if 'dt' in request.args: data = [] for pmid, peaks in study_counts.items(): s = peaks[0].study link = '<a href={0}>{1}</a>'.format(url_for('studies.show', val=pmid), s.title) data.append([link, s.authors, s.journal, len(peaks)]) else: data = [{'pmid': pmid, 'peaks': len(peaks)} for pmid, peaks in study_counts.items()] return jsonify(data=data)
def location_api(val): args = [int(i) for i in val.split('_')] if len(args) == 3: args.append(10) x, y, z, radius = args ### PEAKS ### # Limit search to 20 mm to keep things fast if radius > 20: radius = 20 points = Peak.closestPeaks(radius,x,y,z) points = points.group_by(Peak.pmid) #prevents duplicate studies points = points.add_columns(sqlalchemy.func.count(Peak.id)) #counts duplicate peaks ### IMAGES ### location = Location.query.filter_by(x=x, y=y, z=z).first() images = [] if location is None else location.images images = [{'label': i.label, 'id': i.id} for i in images if i.display] if 'draw' in request.args: data = [] for p in points: s = p[0].study link = '<a href={0}>{1}</a>'.format(url_for('studies.show',val=s.pmid),s.title) data.append([link, s.authors, s.journal,p[1]]) data = jsonify(data=data) else: data = { 'studies': [{'pmid':p[0].study.pmid,'peaks':p[1] } for p in points], 'images': images } data = jsonify(data=data) return data
def get_location(): """ Retrieve location data --- tags: - locations responses: 200: description: Location data default: description: No locations found parameters: - in: query name: x description: x-coordinate required: true type: integer - in: query name: y description: y-coordinate required: true type: integer - in: query name: z description: z-coordinate required: true type: integer - in: query name: r description: Radius of sphere within which to search for study activations, in mm (default = 6, max = 20). required: false type: integer """ x = int(request.args['x']) y = int(request.args['y']) z = int(request.args['z']) # Radius: 6 mm by default, max 2 cm r = min(int(request.args.get('r', 6)), 20) # Check validity of coordinates and redirect if necessary check_xyz(x, y, z) loc = Location.query.filter_by(x=x, y=y, z=z).first() if loc is None: from nsweb.controllers.locations import make_location loc = make_location(x, y, z) peaks = Peak.closestPeaks(r, x, y, z) peaks = peaks.group_by(Peak.pmid) peaks = peaks.add_columns(func.count(Peak.id)) loc.studies = [p[0].study for p in peaks] schema = LocationSchema() return jsonify(data=schema.dump(loc).data)
def get_studies(val=None): x, y, z, radius = get_params(val) points = Peak.closestPeaks(radius, x, y, z) # prevents duplicate studies points = points.group_by(Peak.pmid) # counts duplicate peaks points = points.add_columns(sqlalchemy.func.count(Peak.id)) if 'dt' in request.args: data = [] for p in points: s = p[0].study link = '<a href={0}>{1}</a>'.format(url_for('studies.show', val=s.pmid), s.title) data.append([link, s.authors, s.journal, p[1]]) data = jsonify(data=data) else: data = [{'pmid': p[0].study.pmid, 'peaks':p[1]} for p in points] data = jsonify(data=data) return data