def add_raw(token): """Add new channel to the project.""" project = Project.get(token) if not project: return abort(404, description=f'project {token} not found') # Load channel from first array in attached file try: npz = np.load(request.files.get('file')) except BadRequestKeyError: # could not get file from request.files return abort( 400, description= 'Attach a new channel file in a form under the file field.') except TypeError: return abort( 400, description='Could not load the attached file. Attach an .npz file.' ) channel = npz[npz.files[0]] # Check channel is the right shape expected_shape = (project.num_frames, project.width, project.height, 1) if channel.shape != expected_shape: raise ValueError(f'New channel must have shape {expected_shape}') # Add channel to project project.add_channel(channel) return {'numChannels': project.num_channels}
def create_project_from_dropped_file(): """ Create a new Project from drag & dropped file. """ start = timeit.default_timer() loader = loaders.FileLoader(request) project = Project.create(loader) current_app.logger.info('Created project from %s in %s s.', loader.path, timeit.default_timer() - start) return jsonify({'projectId': project.token})
def semantic_labels(project_id, feature): project = Project.get(project_id) if not project: return jsonify({'error': f'project {project_id} not found'}), 404 cell_info = project.labels.cell_info[feature] cell_info = add_frame_div_parent(cell_info) cell_info = reformat_cell_info(cell_info) response = make_response(cell_info) response.add_etag() return response.make_conditional(request)
def create_project_from_url(): """ Create a new Project from URL. """ start = timeit.default_timer() url_form = request.form loader = loaders.URLLoader(url_form) project = Project.create(loader) current_app.logger.info('Created project from %s in %s s.', loader.path, timeit.default_timer() - start) return jsonify({'projectId': project.token})
def get_project(token): """ Retrieve data from a project already in the Project table. """ start = timeit.default_timer() project = Project.get(token) if not project: return abort(404, description=f'project {token} not found') payload = project.make_first_payload() current_app.logger.debug('Loaded project %s in %s s.', project.token, timeit.default_timer() - start) return jsonify(payload)
def raw(token, channel, frame): project = Project.get(token) if not project: return abort(404, description=f'project {token} not found') raw_array = project.get_raw_array(channel, frame) content = gzip.compress(json.dumps(raw_array.tolist()).encode('utf8'), 5) response = make_response(content) response.headers['Content-length'] = len(content) response.headers['Content-Encoding'] = 'gzip' # gzip includes a timestamp that changes the md5 hash # TODO: in Python >= 3.8, add mtime=0 to create stable md5 and use add_etag instead etag = hashlib.md5(raw_array).hexdigest() response.set_etag(etag) return response.make_conditional(request)
def redo(token): start = timeit.default_timer() project = Project.get(token) if not project: return abort(404, description=f'project {token} not found') payload = project.redo() current_app.logger.debug( 'Redid action for project %s finished in %s s.', token, timeit.default_timer() - start, ) return jsonify(payload)
def download_project(): """ Download a DeepCell Label project as a .npz file """ id = request.args.get('id') project = Project.get(id) if not project: return abort(404, description=f'project {id} not found') format = request.args.get('format') exporter = exporters.Exporter(project, format) filestream = exporter.export() return send_file(filestream, as_attachment=True, attachment_filename=exporter.path)
def labeled(token, feature, frame): """ """ project = Project.get(token) if not project: return jsonify({'error': f'project {token} not found'}), 404 labeled_array = project.get_labeled_array(feature, frame).astype(np.int32) content = gzip.compress( json.dumps(labeled_array.tolist()).encode('utf8'), 5) response = make_response(content) response.headers['Content-length'] = len(content) response.headers['Content-Encoding'] = 'gzip' # gzip includes a timestamp that changes the md5 hash # TODO: in Python >= 3.8, add mtime=0 to create stable md5 and use add_etag instead etag = hashlib.md5(np.ascontiguousarray(labeled_array)).hexdigest() response.set_etag(etag) return response.make_conditional(request)
def edit(token, action_type): """ Edit the labeling of the project and update the project in the database. """ start = timeit.default_timer() # obtain 'info' parameter data sent by .js script info = {k: json.loads(v) for k, v in request.values.to_dict().items()} project = Project.get(token) if not project: return abort(404, description=f'project {token} not found') # TODO: remove frame/feature/channel columns from db schema? or keep them around # to load projects on the last edited frame project.frame = info['frame'] project.feature = info['feature'] project.channel = info['channel'] del info['frame'] del info['feature'] del info['channel'] edit = Edit(project) edit.dispatch_action(action_type, info) project.create_memento(action_type) project.update() changed_frames = [frame.frame_id for frame in project.action.frames] payload = { 'feature': project.feature, 'frames': changed_frames, 'labels': edit.labels_changed, } current_app.logger.debug( 'Finished action %s for project %s in %s s.', action_type, token, timeit.default_timer() - start, ) return jsonify(payload)
def upload_project_to_s3(): """Upload .trk/.npz data file to AWS S3 bucket.""" start = timeit.default_timer() id = request.form['id'] format = request.form['format'] bucket = request.form['bucket'] project = Project.get(id) if not project: return abort(404, description=f'project {id} not found') # Save data file and send to S3 bucket exporter = exporters.S3Exporter(project, format) exporter.export(bucket) # add "finished" timestamp and null out PickleType columns # project.finish() current_app.logger.debug( 'Uploaded %s to S3 bucket %s from project %s in %s s.', exporter.path, bucket, id, timeit.default_timer() - start, ) return {}