code: def construct_weights_and_mask(img):
      seg_boundaries = find_boundaries(img, mode="inner")
  
      bin_img = img > 0
      # take segmentations, ignore boundaries
      binary_with_borders = np.bitwise_xor(bin_img, seg_boundaries)
  
      foreground_weight = (
          1 - binary_with_borders.sum() / binary_with_borders.size
      )
      background_weight = 1 - foreground_weight
  
      # build euclidean distances maps for each cell:
      cell_ids = [x for x in np.unique(img) if x > 0]
      distances = np.zeros((img.shape[0], img.shape[1], len(cell_ids)))
  
      for i, cell_id in enumerate(cell_ids):
          distances[..., i] = distance_transform_edt(img != cell_id)
  
      # we need to look at the two smallest distances
      distances.sort(axis=-1)
  
      weight_map = W_0 * np.exp(
          -(1 / (2 * SIGMA**2))
          * ((distances[..., 0] + distances[..., 1]) ** 2)
      )
      weight_map[binary_with_borders] = foreground_weight
      weight_map[~binary_with_borders] += background_weight
  
      return weight_map, binary_with_borders
  
file_path: NiftyNet/demos/unet/make_cell_weights.py
imports: import os
  import numpy
  import matplotlib.pyplot
  from scipy.ndimage.morphology import distance_transform_edt
  from skimage.io import imsave, imread
  from skimage.segmentation import find_boundaries
  from demos.unet.file_sorter import get_user_args
label_desc: Use image node_id 1 for calling a global function with example usage: construct_weights_and_mask(img) and returns: weight_map,  binary_with_borders