def get_space_connected_to_position(layer, position): empty_mask = (layer != Tiles.wall) labeled_components, component_count = label_connected_components( empty_mask) label = labeled_components[tuple(position)] connected_space = (labeled_components == label) return connected_space
def get_random_positions_for_lock(layer, start_position): connected_space = MissionGenerator.get_space_connected_to_position( layer, start_position) connected_walls_and_corridors = MissionGenerator.get_walls_and_corridors_connected_to_space( layer, connected_space) connected_walls_and_corridors_components, component_count = label_connected_components( connected_walls_and_corridors) random_positions = MissionGenerator.get_random_positions_per_component( connected_walls_and_corridors_components, component_count, 1) return random_positions
def get_space_component_masks(layer): masks = [] empty_space = (layer == Tiles.empty) connected_components, component_count = label_connected_components( empty_space) for component_label in range(1, component_count + 1): component_mask = ( connected_components == component_label).astype(int) masks.append(component_mask) return masks
def remove_hazard_component(layer, hazard_position, hazard_tile): hazard_mask = (layer == hazard_tile) labeled_components, component_count = label_connected_components( hazard_mask) hazard_label = labeled_components[hazard_position] layer[labeled_components == hazard_label] = Tiles.empty
sub = sitk.ReadImage('./segmentation-0.nii') # Get Numpy data and compress to int8. reference_volume = sitk.GetArrayFromImage(ref)#.astype(np.int8) lreference_volume = reference_volume.view((1,75,512,512)) submission_volume = sitk.GetArrayFromImage(sub).astype(np.int8) # Ensure that the shapes of the masks match. if submission_volume.shape!=reference_volume.shape: raise AttributeError("Shapes do not match! Prediction mask {}, " "ground truth mask {}" "".format(submission_volume.shape, reference_volume.shape)) # Create masks with labeled connected components. # (Assuming there is always exactly one liver - one connected comp.) pred_mask_liver = submission_volume;pred_mask_liver[submission_volume>=1]=1 pred_mask_lesion, num_predicted = label_connected_components(submission_volume==2, output=np.int16)#default structuring is 2-D array, so the input demension is? true_mask_liver = reference_volume;true_mask_liver[reference_volume>=1]=1 true_mask_lesion, num_reference = label_connected_components(reference_volume==2, output=np.int16) liver_prediction_exists = np.any(submission_volume==1) # Compute per-case (per patient volume) dice. if not np.any(pred_mask_lesion) and not np.any(true_mask_lesion): tumor_dice_per_case = 1. else: tumor_dice_per_case = metric.dc(pred_mask_lesion, true_mask_lesion) if liver_prediction_exists: liver_dice_per_case = metric.dc(pred_mask_liver, true_mask_liver) else: liver_dice_per_case = 0 print(tumor_dice_per_case)
# Get Numpy data and compress to int8. reference_volume = (reference_volume.get_data()).astype(np.int8) submission_volume = (submission_volume.get_data()).astype(np.int8) # Ensure that the shapes of the masks match. if submission_volume.shape != reference_volume.shape: raise AttributeError("Shapes do not match! Prediction mask {}, " "ground truth mask {}" "".format(submission_volume.shape, reference_volume.shape)) print("Done loading files ({:.2f} seconds)".format(t())) # Create lesion and liver masks with labeled connected components. # (Assuming there is always exactly one liver - one connected comp.) pred_mask_lesion, num_predicted = label_connected_components( \ submission_volume==2, output=np.int16) true_mask_lesion, num_reference = label_connected_components( \ reference_volume==2, output=np.int16) pred_mask_liver = submission_volume >= 1 true_mask_liver = reference_volume >= 1 liver_prediction_exists = np.any(submission_volume == 1) print("Done finding connected components ({:.2f} seconds)".format(t())) # Identify detected lesions. # Retain detected_mask_lesion for overlap > 0.5 for overlap in [0, 0.5]: detected_mask_lesion, mod_ref_mask, num_detected = detect_lesions( \ prediction_mask=pred_mask_lesion, reference_mask=true_mask_lesion, min_overlap=overlap)
def fill_unused_space(level): start_position = np.argwhere(level.upper_layer == Tiles.player)[0] non_wall_mask = (level.upper_layer != Tiles.wall).astype(int) connected_components, component_count = label_connected_components(non_wall_mask) used_space_component = connected_components[tuple(start_position)] level.upper_layer[connected_components != used_space_component] = Tiles.wall
def get_potential_lock_components(layer): potential_lock_mask = MissionGenerator.get_wall_corridor_mask(layer) labeled_components, component_count = label_connected_components( potential_lock_mask) return labeled_components, component_count
def get_rooms_components(layer): empty_mask = (layer == Tiles.empty) labeled_components, component_count = label_connected_components( empty_mask) return labeled_components, component_count