def __init__(self, user, project_id, stack_mirror_ids, x_min, x_max, y_min, y_max, z_min, z_max, rotation_cw, zoom_level, single_channel=False, output_path=None): self.user = user self.project_id = int(project_id) self.project = get_object_or_404(Project, pk=project_id) # Allow a single ID and a list if isinstance(stack_mirror_ids, int): self.stack_mirror_ids= [stack_mirror_ids] else: self.stack_mirror_ids = stack_mirror_ids self.stack_mirrors = [] self.stack_tile_sources = {} for sid in self.stack_mirror_ids: stack_mirror = get_object_or_404(StackMirror, pk=sid) self.stack_mirrors.append(stack_mirror) tile_source = get_tile_source(stack_mirror.tile_source_type) self.stack_tile_sources[stack_mirror.stack.id] = tile_source # The reference stack is used to obtain e.g. resolution information self.ref_stack = self.stack_mirrors[0].stack self.x_min = float(x_min) self.x_max = float(x_max) self.y_min = float(y_min) self.y_max = float(y_max) self.z_min = float(z_min) self.z_max = float(z_max) self.zoom_level = int(zoom_level) # Save a normalized version of the rotation angle self.rotation_cw = rotation_cw % 360 # Create an output path if not already present if output_path is None: file_name = file_prefix + id_generator() + "." + file_extension output_path = os.path.join(crop_output_path, file_name) self.single_channel = single_channel self.output_path = output_path
def __init__(self, user, project_id, stack_mirror_ids, x_min, x_max, y_min, y_max, z_min, z_max, rotation_cw, zoom_level, single_channel=False, output_path=None): self.user = user self.project_id = int(project_id) self.project = get_object_or_404(Project, pk=project_id) # Allow a single ID and a list if isinstance(stack_mirror_ids, int): self.stack_mirror_ids= [stack_mirror_ids] else: self.stack_mirror_ids = stack_mirror_ids self.stack_mirrors:List = [] self.stack_tile_sources:Dict = {} for sid in self.stack_mirror_ids: stack_mirror = get_object_or_404(StackMirror, pk=sid) self.stack_mirrors.append(stack_mirror) tile_source = get_tile_source(stack_mirror.tile_source_type) self.stack_tile_sources[stack_mirror.stack.id] = tile_source # The reference stack is used to obtain e.g. resolution information self.ref_stack = self.stack_mirrors[0].stack self.x_min = float(x_min) self.x_max = float(x_max) self.y_min = float(y_min) self.y_max = float(y_max) self.z_min = float(z_min) self.z_max = float(z_max) self.zoom_level = int(zoom_level) # Save a normalized version of the rotation angle self.rotation_cw = rotation_cw % 360 # Create an output path if not already present if output_path is None: file_name = file_prefix + id_generator() + "." + file_extension output_path = os.path.join(crop_output_path, file_name) self.single_channel = single_channel self.output_path = output_path
def crop(request: HttpRequest, project_id=None) -> JsonResponse: """ Crops out the specified region of the stack. The region is expected to be given in terms of real world units (e.g. nm). """ stack_ids = get_request_list(request.POST, "stack_ids", [], int) x_min = float(request.POST['min_x']) y_min = float(request.POST['min_y']) z_min = float(request.POST['min_z']) x_max = float(request.POST['max_x']) y_max = float(request.POST['max_y']) z_max = float(request.POST['max_z']) zoom_level = float(request.POST['zoom_level']) single_channel = get_request_bool(request.POST, 'single_channel', False) rotation_cw = float(request.GET.get('rotationcw', 0.0)) # Make sure tmp dir exists and is writable if not os.path.exists(crop_output_path) or not os.access( crop_output_path, os.W_OK): if request.user.is_superuser: err_message = "Please make sure your output folder (%s) exists " \ "is writable." % crop_output_path else: err_message = "Sorry, the output path for the cropping tool " \ "isn't set up correctly. Please contact an administrator." return json_error_response(err_message) # Use first reachable stack mirrors stack_mirror_ids = [] for sid in stack_ids: stack_mirrors = StackMirror.objects.select_related('stack').filter( stack_id=sid) for sm in stack_mirrors: # If mirror is reachable use it right away tile_source = get_tile_source(sm.tile_source_type) try: req = requests.head(tile_source.get_canary_url(sm), allow_redirects=True, verify=verify_ssl) reachable = req.status_code == 200 except Exception as e: logger.error(e) reachable = False if reachable: stack_mirror_ids.append(sm.id) break if not reachable: raise ValueError( "Can't find reachable stack mirror for stack {}".format(sid)) # Crate a new cropping job job = CropJob(request.user, project_id, stack_mirror_ids, x_min, x_max, y_min, y_max, z_min, z_max, rotation_cw, zoom_level, single_channel) # Parameter check errors = sanity_check(job) if len(errors) > 0: err_message = "Some problems with the cropping parameters were found: " for n, errtxt in enumerate(errors): if n == 0: err_message += str(n + 1) + ". " + errtxt else: err_message += ", " + str(n + 1) + ". " + errtxt err_response = json_error_response(err_message) return err_response result = start_asynch_process(job) return result
def crop(request, project_id=None): """ Crops out the specified region of the stack. The region is expected to be given in terms of real world units (e.g. nm). """ stack_ids = get_request_list(request.POST, "stack_ids", [], int) x_min = float(request.POST['min_x']) y_min = float(request.POST['min_y']) z_min = float(request.POST['min_z']) x_max = float(request.POST['max_x']) y_max = float(request.POST['max_y']) z_max = float(request.POST['max_z']) zoom_level = float(request.POST['zoom_level']) single_channel = request.POST['single_channel'] == 'true' rotation_cw = float(request.GET.get('rotationcw', 0.0)) # Make sure tmp dir exists and is writable if not os.path.exists( crop_output_path ) or not os.access( crop_output_path, os.W_OK ): if request.user.is_superuser: err_message = "Please make sure your output folder (%s) exists " \ "is writable." % crop_output_path else: err_message = "Sorry, the output path for the cropping tool " \ "isn't set up correctly. Please contact an administrator." return json_error_response(err_message) # Use first reachable stack mirrors stack_mirror_ids = [] for sid in stack_ids: stack_mirrors = StackMirror.objects.select_related('stack').filter(stack_id=sid); for sm in stack_mirrors: # If mirror is reachable use it right away tile_source = get_tile_source(sm.tile_source_type) try: req = requests.head(tile_source.get_canaray_url(sm)) reachable = req.status_code == 200 except : reachable = False if reachable: stack_mirror_ids.append(sm.id) break if not reachable: raise ValueError("Can't find reachable stack mirror for stack {}".format(sid)) # Crate a new cropping job job = CropJob(request.user, project_id, stack_mirror_ids, x_min, x_max, y_min, y_max, z_min, z_max, rotation_cw, zoom_level, single_channel) # Parameter check errors = sanity_check( job ) if len(errors) > 0: err_message = "Some problems with the cropping parameters were found: " for n, e in enumerate( errors ): if n == 0: err_message += str( n+1 ) + ". " + e else: err_message += ", " + str( n+1 ) + ". " + e err_response = json_error_response( err_message ) return err_response result = start_asynch_process( job ) return result