def load(filename): ''' Initate TrackEdit/ZStackEdit object and load object to database. Send specific attributes of the object to the .js file. ''' start = timeit.default_timer() current_app.logger.info('Loading track at %s', filename) folders = re.split('__', filename) filename = folders[len(folders) - 1] subfolders = folders[2:len(folders) - 1] subfolders = '/'.join(subfolders) full_path = os.path.join(subfolders, filename) input_bucket = folders[0] output_bucket = folders[1] # arg is 'false' which gets parsed to True if casting to bool rgb = request.args.get('rgb', default='false', type=str) rgb = bool(distutils.util.strtobool(rgb)) if not is_trk_file(filename) and not is_npz_file(filename): error = { 'error': 'invalid file extension: {}'.format( os.path.splitext(filename)[-1]) } return jsonify(error), 400 # Initate Edit object and entry in database caliban_file = CalibanFile(filename, input_bucket, full_path) edit = get_edit(caliban_file, output_bucket, rgb) project = Project.create_project(filename, edit, subfolders) if is_trk_file(filename): current_app.logger.debug('Loaded trk file "%s" in %s s.', filename, timeit.default_timer() - start) # Send attributes to .js file return jsonify({ 'max_frames': caliban_file.max_frames, 'tracks': caliban_file.readable_tracks, 'dimensions': (caliban_file.width, caliban_file.height), 'project_id': project.id, 'screen_scale': edit.scale_factor }) if is_npz_file(filename): current_app.logger.debug('Loaded npz file "%s" in %s s.', filename, timeit.default_timer() - start) # Send attributes to .js file return jsonify({ 'max_frames': caliban_file.max_frames, 'channel_max': caliban_file.channel_max, 'feature_max': caliban_file.feature_max, 'tracks': caliban_file.readable_tracks, 'dimensions': (caliban_file.width, caliban_file.height), 'project_id': project.id })
def upload_file(project_id): '''Upload .trk/.npz data file to AWS S3 bucket.''' start = timeit.default_timer() # Use id to grab appropriate TrackEdit/ZStackEdit object from database project = Project.get_project_by_id(project_id) if not project: return jsonify({'error': 'project_id not found'}), 404 state = load_project_state(project) filename = state.file.filename # Call function in caliban.py to save data file and send to S3 bucket if is_trk_file(filename): state.action_save_track() elif is_npz_file(filename): state.action_save_zstack() # add "finished" timestamp and null out state longblob Project.finish_project(project) current_app.logger.debug('Uploaded file "%s" for project "%s" in %s s.', filename, project_id, timeit.default_timer() - start) return redirect('/')
def test_is_npz_file(): assert helpers.is_npz_file('test.npz') assert helpers.is_npz_file('test.NpZ') assert not helpers.is_npz_file('test.pdf') assert not helpers.is_npz_file('test.trk') assert not helpers.is_npz_file('this is just a string') assert not helpers.is_npz_file(1234) assert not helpers.is_npz_file(None) assert not helpers.is_npz_file(dict())
def load(filename): ''' Initate TrackReview/ZStackReview object and load object to database. Send specific attributes of the object to the .js file. ''' conn = create_connection("caliban.db") print(f"Loading track at {filename}", file=sys.stderr) folders = re.split('__', filename) filename = folders[len(folders) - 1] subfolders = folders[2:len(folders)] subfolders = '/'.join(subfolders) input_bucket = folders[0] output_bucket = folders[1] if is_trk_file(filename): # Initate TrackReview object and entry in database track_review = TrackReview(filename, input_bucket, output_bucket, subfolders) project_id = create_project(conn, filename, track_review) conn.commit() conn.close() # Send attributes to .js file return jsonify({ "max_frames": track_review.max_frames, "tracks": track_review.readable_tracks, "dimensions": track_review.dimensions, "project_id": project_id, "screen_scale": track_review.scale_factor }) if is_npz_file(filename): # Initate ZStackReview object and entry in database zstack_review = ZStackReview(filename, input_bucket, output_bucket, subfolders) project_id = create_project(conn, filename, zstack_review) conn.commit() conn.close() # Send attributes to .js file return jsonify({ "max_frames": zstack_review.max_frames, "channel_max": zstack_review.channel_max, "feature_max": zstack_review.feature_max, "tracks": zstack_review.readable_tracks, "dimensions": zstack_review.dimensions, "project_id": project_id, "screen_scale": zstack_review.scale_factor }) conn.close() error = { 'error': 'invalid file extension: {}'.format(os.path.splitext(filename)[-1]) } return jsonify(error), 400
def get_edit(file_, output_bucket, rgb): """Factory for Edit objects""" if is_npz_file(file_.filename): return ZStackEdit(file_, output_bucket, rgb) elif is_trk_file(file_.filename): # don't use RGB mode with track files return TrackEdit(file_, output_bucket) return BaseEdit(file_, output_bucket)
def tool(): ''' Request HTML caliban tool page to be rendered after user inputs filename in the landing page. ''' if 'filename' not in request.form: return redirect('/') filename = request.form['filename'] current_app.logger.info('%s is filename', filename) # TODO: better name template? new_filename = 'caliban-input__caliban-output__test__{}'.format(filename) # if no options passed (how this route will be for now), # still want to pass in default settings rgb = request.args.get('rgb', default='false', type=str) pixel_only = request.args.get('pixel_only', default='false', type=str) label_only = request.args.get('label_only', default='false', type=str) # Using distutils to cast string arguments to bools settings = { 'rgb': bool(distutils.util.strtobool(rgb)), 'pixel_only': bool(distutils.util.strtobool(pixel_only)), 'label_only': bool(distutils.util.strtobool(label_only)) } if is_trk_file(new_filename): filetype = 'track' title = 'Tracking Tool' elif is_npz_file(new_filename): filetype = 'zstack' title = 'Z-Stack Tool' else: # TODO: render an error template instead of JSON. error = { 'error': 'invalid file extension: {}'.format( os.path.splitext(filename)[-1]) } return jsonify(error), 400 return render_template('tool.html', filetype=filetype, title=title, filename=new_filename, settings=settings)
def shortcut(filename): ''' Request HTML caliban tool page to be rendered if user makes a URL request to access a specific data file that has been preloaded to the input S3 bucket (ex. http://127.0.0.1:5000/test.npz). ''' if is_trk_file(filename): return render_template('index_track.html', filename=filename) if is_npz_file(filename): return render_template('index_zstack.html', filename=filename) error = { 'error': 'invalid file extension: {}'.format(os.path.splitext(filename)[-1]) } return jsonify(error), 400
def tool(): ''' Request HTML caliban tool page to be rendered after user inputs filename in the landing page. ''' filename = request.form['filename'] print(f"{filename} is filename", file=sys.stderr) new_filename = 'caliban-input__caliban-output__test__{}'.format( str(filename)) if is_trk_file(new_filename): return render_template('index_track.html', filename=new_filename) if is_npz_file(new_filename): return render_template('index_zstack.html', filename=new_filename) error = { 'error': 'invalid file extension: {}'.format(os.path.splitext(filename)[-1]) } return jsonify(error), 400
def shortcut(filename): ''' Request HTML caliban tool page to be rendered if user makes a URL request to access a specific data file that has been preloaded to the input S3 bucket (ex. http://127.0.0.1:5000/test.npz). ''' rgb = request.args.get('rgb', default='false', type=str) pixel_only = request.args.get('pixel_only', default='false', type=str) label_only = request.args.get('label_only', default='false', type=str) settings = { 'rgb': bool(distutils.util.strtobool(rgb)), 'pixel_only': bool(distutils.util.strtobool(pixel_only)), 'label_only': bool(distutils.util.strtobool(label_only)) } if is_trk_file(filename): filetype = 'track' title = 'Tracking Tool' elif is_npz_file(filename): filetype = 'zstack' title = 'Z-Stack Tool' else: # TODO: render an error template instead of JSON. error = { 'error': 'invalid file extension: {}'.format( os.path.splitext(filename)[-1]) } return jsonify(error), 400 return render_template('tool.html', filetype=filetype, title=title, filename=filename, settings=settings)
def upload_file(project_id): ''' Upload .trk/.npz data file to AWS S3 bucket. ''' conn = create_connection("caliban.db") # Use id to grab appropriate TrackReview/ZStackReview object from database id_exists = get_project(conn, project_id) if id_exists is None: conn.close() return jsonify({'error': 'project_id not found'}), 404 state = pickle.loads(id_exists[2]) # Call function in caliban.py to save data file and send to S3 bucket if is_trk_file(id_exists[1]): state.action_save_track() elif is_npz_file(id_exists[1]): state.action_save_zstack() # Delete id and object from database delete_project(conn, project_id) conn.close() return redirect("/")