def exercise(file_name=None, pdb_file_name = None, map_file_name = None ,
    split_pdb_file_name = None,
    out = sys.stdout):

  # Set up source data

  if not os.path.isfile(file_name):
    raise Sorry("Missing the file: %s" %(file_name)+"\n")

  print ("Reading from %s" %(file_name))
  from iotbx.map_manager import map_manager
  m = map_manager(file_name)

  print ("Header information from %s:" %(file_name))
  m.show_summary(out = out)

  map_data = m.map_data().deep_copy()
  crystal_symmetry = m.crystal_symmetry()
  unit_cell_parameters = m.crystal_symmetry().unit_cell().parameters()

  print ("\nMap origin: %s Extent %s"  %( map_data.origin(), map_data.all()))
  print ("Original unit cell, not just unit cell of part in this file): %s" %(
     str(unit_cell_parameters)))

  grid_point = (1, 2, 3)
  if map_data.origin() !=  (0, 0, 0): # make sure it is inside
    from scitbx.matrix import col
    grid_point = tuple (col(grid_point)+col(map_data.origin()))
  print ("\nValue of map_data at grid point %s: %.3f" %(str(grid_point),
    map_data[grid_point]))
  print ("Map data is %s" %(type(map_data)))

  random_position = (10, 5, 7.9)
  point_frac = crystal_symmetry.unit_cell().fractionalize(random_position)
  value_at_point_frac = map_data.eight_point_interpolation(point_frac)
  print ("Value of map_data at coordinates %s: %.3f" %(
      str(random_position), value_at_point_frac))

  map_data_as_float = map_data.as_float()
  print ("Map data as float is %s" %(type(map_data_as_float)))


  # make a little model
  sites_cart = flex.vec3_double( ((8, 10, 12), (14, 15, 16)))
  model = model_manager.from_sites_cart(
         atom_name = ' CA ',
         resname = 'ALA',
         chain_id = 'A',
         b_iso = 30.,
         occ = 1.,
         scatterer = 'C',
         sites_cart = sites_cart,
         crystal_symmetry = crystal_symmetry)


  # Move map and a model to place origin at (0, 0, 0)
  # map data is new copy but model is shifted in place.

  from iotbx.map_model_manager import map_model_manager
  mam = map_model_manager(
          map_manager =  m,
          model     = model.deep_copy(),
    )

  # Read in map and model and split up
  dm = DataManager()
  aa = dm.get_map_model_manager(model_file=pdb_file_name,
    map_files=map_file_name)
  bb = dm.get_map_model_manager(model_file=split_pdb_file_name,
    map_files=map_file_name)

  for selection_method in ['by_chain', 'by_segment','supplied_selections',
      'boxes']:
    if selection_method == 'boxes':
      choices = [True, False]
    else:
      choices = [True]
    if selection_method == 'by_chain':
      mask_choices = [True,False]
    else:
      mask_choices = [False]
    for select_final_boxes_based_on_model in choices:
      for skip_empty_boxes in choices:
        for mask_choice in mask_choices:
          if mask_choice: # use split model
            a=bb.deep_copy()
          else: # usual
            a=aa.deep_copy()
          print ("\nRunning split_up_map_and_model with \n"+
            "select_final_boxes_based_on_model="+
           "%s   skip_empty_boxes=%s selection_method=%s" %(
            select_final_boxes_based_on_model,skip_empty_boxes,selection_method))

          if selection_method == 'by_chain':
            print ("Mask around unused atoms: %s" %(mask_choice))
            box_info = a.split_up_map_and_model_by_chain(
              mask_around_unselected_atoms=mask_choice)
          elif selection_method == 'by_segment':
            box_info = a.split_up_map_and_model_by_segment()
          elif selection_method == 'supplied_selections':
            selection = a.model().selection('all')
            box_info = a.split_up_map_and_model_by_supplied_selections(
              selection_list = [selection])
          elif selection_method == 'boxes':
            box_info = a.split_up_map_and_model_by_boxes(
              skip_empty_boxes = skip_empty_boxes,
              select_final_boxes_based_on_model =
                select_final_boxes_based_on_model)
          print (selection_method,skip_empty_boxes,
              len(box_info.selection_list),
              box_info.selection_list[0].count(True))
          assert (selection_method,skip_empty_boxes,
              len(box_info.selection_list),
              box_info.selection_list[0].count(True)) in [
                ('by_chain',True,3,19),
                ("by_chain",True,1,86,),
                ("by_segment",True,1,86,),
                ("supplied_selections",True,1,86,),
                ("boxes",True,13,1,),
                ("boxes",False,36,0,),
                ("boxes",True,13,1,),
                ("boxes",False,36,0,),
                ]




          # Change the coordinates in one box
          small_model = box_info.mmm_list[0].model()
          small_sites_cart = small_model.get_sites_cart()
          from scitbx.matrix import col
          small_sites_cart += col((1,0,0))
          small_model.set_crystal_symmetry_and_sites_cart(
            sites_cart = small_sites_cart,
            crystal_symmetry = small_model.crystal_symmetry())
          # Put everything back together
          a.merge_split_maps_and_models(box_info = box_info)


  mam.box_all_maps_around_model_and_shift_origin()

  shifted_crystal_symmetry = mam.model().crystal_symmetry()
  shifted_model = mam.model()
  shifted_map_data = mam.map_data()

  print ("\nOriginal map origin (grid units):", map_data.origin())
  print ("Original model:\n", model.model_as_pdb())

  print ("Shifted map origin:", shifted_map_data.origin())
  print ("Shifted model:\n", shifted_model.model_as_pdb())


  # Save the map_model manager
  mam_dc=mam.deep_copy()
  print ("dc",mam)
  print ("dc mam_dc",mam_dc)

  # Mask map around atoms
  mam=mam_dc.deep_copy()
  print ("dc mam_dc dc",mam_dc)
  print (mam)
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3,
     set_outside_to_mean_inside=True, soft_mask=False)
  print ("Mean before masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      -0.0585683621466)
  print ("Max before masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      -0.0585683621466)

  # Mask map around atoms, with soft mask
  mam=mam_dc.deep_copy()
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3, soft_mask = True,
    soft_mask_radius = 5, set_outside_to_mean_inside=True)
  print ("Mean after first masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      -0.00177661714805)
  print ("Max after first masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
       0.236853733659)

  # Mask map around atoms again
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3,
     set_outside_to_mean_inside = True, soft_mask=False)
  print ("Mean after second masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
     -0.0585683621466)
  print ("Max after second masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      -0.0585683621466)

  # Mask around edges
  mam=mam_dc.deep_copy()
  mam.mask_all_maps_around_edges( soft_mask_radius = 3)
  print ("Mean after masking edges", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      0.0155055604192)
  print ("Max after masking edges", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      0.249827131629)


  print ("\nWriting map_data and model in shifted position (origin at 0, 0, 0)")

  output_file_name = 'shifted_map.ccp4'
  print ("Writing to %s" %(output_file_name))
  mrcfile.write_ccp4_map(
      file_name = output_file_name,
      crystal_symmetry = shifted_crystal_symmetry,
      map_data = shifted_map_data, )

  output_file_name = 'shifted_model.pdb'
  f = open(output_file_name, 'w')
  print (shifted_model.model_as_pdb(), file=f)
  f.close()


  print ("\nWriting map_data and model in original position (origin at %s)" %(
      str(mam.map_manager().origin_shift_grid_units)))

  output_file_name = 'new_map_original_position.ccp4'
  print ("Writing to %s" %(output_file_name))
  mrcfile.write_ccp4_map(
      file_name = output_file_name,
      crystal_symmetry = shifted_crystal_symmetry,
      map_data = shifted_map_data,
      origin_shift_grid_units = mam.map_manager().origin_shift_grid_units)
  print (shifted_model.model_as_pdb())
  output_pdb_file_name = 'new_model_original_position.pdb'
  f = open(output_pdb_file_name, 'w')
  print (shifted_model.model_as_pdb(), file=f)
  f.close()

  # Write as mmcif
  output_cif_file_name = 'new_model_original_position.cif'
  f = open(output_cif_file_name, 'w')
  print (shifted_model.model_as_mmcif(),file = f)
  f.close()


  # Read the new map and model
  import iotbx.pdb
  new_model =  model_manager(
     model_input = iotbx.pdb.input(
         source_info = None,
         lines = flex.split_lines(open(output_pdb_file_name).read())),
         crystal_symmetry = crystal_symmetry)
  assert new_model.model_as_pdb() == model.model_as_pdb()

  new_model_from_cif =  model_manager(
     model_input = iotbx.pdb.input(
         source_info = None,
         lines = flex.split_lines(open(output_cif_file_name).read())),
         crystal_symmetry = crystal_symmetry)
  assert new_model_from_cif.model_as_pdb() == model.model_as_pdb()

  # Read and box the original file again in case we modified m in any
  #   previous tests
  m = map_manager(file_name)
  mam=map_model_manager(model=model.deep_copy(),map_manager=m)
  mam.box_all_maps_around_model_and_shift_origin()

  file_name = output_file_name
  print ("Reading from %s" %(file_name))
  new_map = iotbx.mrcfile.map_reader(file_name = file_name, verbose = False)
  new_map.data = new_map.data.shift_origin()
  print ("Header information from %s:" %(file_name))
  new_map.show_summary(out = out)
  assert new_map.map_data().origin() == mam.map_manager().map_data().origin()
  assert new_map.crystal_symmetry().is_similar_symmetry(mam.map_manager().crystal_symmetry())

  # make a map_model_manager with lots of maps and model and ncs
  from mmtbx.ncs.ncs import ncs
  ncs_object=ncs()
  ncs_object.set_unit_ncs()
  mam = map_model_manager(
          map_manager =  m,
          ncs_object =  ncs_object,
          map_manager_1 =  m.deep_copy(),
          map_manager_2 =  m.deep_copy(),
          extra_model_list =  [model.deep_copy(),model.deep_copy()],
          extra_model_id_list = ["model_1","model_2"],
          extra_map_manager_list =  [m.deep_copy(),m.deep_copy()],
          extra_map_manager_id_list = ["extra_1","extra_2"],
          model     = model.deep_copy(),
    )


  # make a map_model_manager with lots of maps and model and ncs and run
  # with wrapping and ignore_symmetry_conflicts on
  from mmtbx.ncs.ncs import ncs
  ncs_object=ncs()
  ncs_object.set_unit_ncs()
  m.set_ncs_object(ncs_object.deep_copy())
  mam2 = map_model_manager(
          map_manager =  m.deep_copy(),
          ncs_object =  ncs_object.deep_copy(),
          map_manager_1 =  m.deep_copy(),
          map_manager_2 =  m.deep_copy(),
          extra_model_list =  [model.deep_copy(),model.deep_copy()],
          extra_model_id_list = ["model_1","model_2"],
          extra_map_manager_list =  [m.deep_copy(),m.deep_copy()],
          extra_map_manager_id_list = ["extra_1","extra_2"],
          model     = model.deep_copy(),
          ignore_symmetry_conflicts = True,
          wrapping = m.wrapping(),
    )
  assert mam.map_manager().is_similar(mam2.map_manager())
  assert mam.map_manager().is_similar(mam2.map_manager_1())
  for m in mam2.map_managers():
    assert mam.map_manager().is_similar(m)
  assert mam.model().shift_cart() == mam2.model().shift_cart()
  assert mam.model().shift_cart() == mam2.get_model_by_id('model_2').shift_cart()



  print ("OK")
def exercise(file_name, out=sys.stdout):

    # Set up source data
    if not os.path.isfile(file_name):
        raise Sorry("Missing the file: %s" % (file_name) + "\n")

    print("Reading from %s" % (file_name))
    from iotbx.map_manager import map_manager

    m = map_manager(file_name)

    # make a little model
    sites_cart = flex.vec3_double(((8, 10, 12), (14, 15, 16)))
    model = model_manager.from_sites_cart(
        atom_name=' CA ',
        resname='ALA',
        chain_id='A',
        b_iso=30.,
        occ=1.,
        scatterer='C',
        sites_cart=sites_cart,
        crystal_symmetry=m.crystal_symmetry())

    # make a map_model_manager with lots of maps and model and ncs
    from iotbx.map_model_manager import map_model_manager

    from mmtbx.ncs.ncs import ncs
    ncs_object = ncs()
    ncs_object.set_unit_ncs()
    mask_mm = m.deep_copy()
    mask_mm.set_is_mask(True)
    mam = map_model_manager(
        map_manager=m,
        ncs_object=ncs_object,
        map_manager_1=m.deep_copy(),
        map_manager_2=m.deep_copy(),
        extra_map_manager_list=[m.deep_copy(),
                                m.deep_copy(),
                                m.deep_copy()],
        extra_map_manager_id_list=["extra_1", "extra_2", "map_manager_mask"],
        model=model.deep_copy(),
    )
    print(mam.map_manager())
    print(mam.model())
    print(mam.map_manager_1())
    print(mam.map_manager_2())
    print(mam.map_manager_mask())
    print(mam.map_manager().ncs_object())
    all_map_names = mam.map_id_list()
    for id in all_map_names:
        print("Map_manager %s: %s " % (id, mam.get_map_manager_by_id(id)))

    dm = DataManager(['model', 'miller_array', 'real_map', 'phil', 'ncs_spec'])
    dm.set_overwrite(True)

    # Create a model with ncs
    from iotbx.regression.ncs.tst_ncs import pdb_str_5
    file_name = 'tst_mam.pdb'
    f = open(file_name, 'w')
    print(pdb_str_5, file=f)
    f.close()

    # Generate map data from this model (it has ncs)
    mmm = map_model_manager()
    mmm.generate_map(box_cushion=0, file_name=file_name, n_residues=500)
    ncs_mam = mmm.deep_copy()
    ncs_mam_copy = mmm.deep_copy()

    # Make sure this model has 126 sites (42 sites times 3-fold ncs)
    assert ncs_mam.model().get_sites_cart().size() == 126
    assert approx_equal(ncs_mam.model().get_sites_cart()[0],
                        (23.560999999999996, 8.159, 10.660000000000002))

    # Get just unique part (42 sites)
    unique_mam = ncs_mam.extract_all_maps_around_model(
        select_unique_by_ncs=True)
    assert unique_mam.model().get_sites_cart().size() == 42
    assert approx_equal(unique_mam.model().get_sites_cart()[0],
                        (18.740916666666664, 13.1794, 16.10544))

    # Make sure that the extraction did not change the original but does change
    #   the extracted part
    assert (unique_mam.model().get_sites_cart()[0] !=
            ncs_mam.model().get_sites_cart()[0]
            )  # it was a deep copy so original stays

    # Shift back the extracted part and make sure it matches the original now
    shifted_back_unique_model = mmm.get_model_from_other(
        unique_mam.deep_copy())
    assert approx_equal(shifted_back_unique_model.get_sites_cart()[0],
                        (23.560999999999996, 8.158999999999997, 10.66))

    # Change the extracted model
    sites_cart = unique_mam.model().get_sites_cart()
    sites_cart[0] = (1, 1, 1)
    unique_mam.model().get_hierarchy().atoms().set_xyz(sites_cart)
    # Note; setting xyz in hierarchy does not set xrs by itself. do that now:
    unique_mam.model().set_sites_cart_from_hierarchy(multiply_ncs=False)

    # Make sure we really changed it
    assert approx_equal(unique_mam.model().get_sites_cart()[0], (1, 1, 1))

    # Now propagate all the changes in this unique part to entire original model
    #   using NCS
    ncs_mam.propagate_model_from_other(other=unique_mam,
                                       model_id='model',
                                       other_model_id='model')
    # ...and check that copy 1 and copy 2 both change
    assert approx_equal(
        ncs_mam.model().get_sites_cart()[0],
        (5.820083333333333, -4.020400000000001, -4.445440000000001))
    assert approx_equal(
        ncs_mam.model().get_sites_cart()[42],
        (38.41904613024224, 17.233251085893276, 2.5547442135142524))

    # Find ncs from map or model
    nn = ncs_mam_copy
    nn.write_map('ncs.ccp4')
    nn.write_model('ncs.pdb')
    ncs_object = nn.get_ncs_from_model()
    dm.write_ncs_spec_file(ncs_object, 'ncs.ncs_spec')
    print("NCS from map", ncs_object)
    nn.set_ncs_object(ncs_object)
    print("NCS now: ", nn.ncs_object())
    nn.get_ncs_from_map(ncs_object=ncs_object)
    print("ncs cc:", nn.ncs_cc())
    assert approx_equal(nn.ncs_cc(), 0.961915979834, eps=0.01)

    # Make a deep_copy
    dc = mam.deep_copy()
    new_mam = mam.deep_copy()
    assert mam.map_manager().map_data()[0] == new_mam.map_manager().map_data(
    )[0]

    # Make a customized_copy
    new_mam = mam.customized_copy(model_dict={'model': mam.model()})
    assert new_mam.model() is mam.model()
    assert not new_mam.map_dict() is mam.map_dict()

    new_mam = mam.customized_copy(model_dict={'model': mam.model()},
                                  map_dict=mam.map_dict())
    assert new_mam.model() is mam.model()
    assert new_mam.map_dict() is mam.map_dict()
    print(mam)

    # Add a map
    mam = dc.deep_copy()
    print(mam.map_id_list())
    assert len(mam.map_id_list()) == 6
    mam.add_map_manager_by_id(mam.map_manager().deep_copy(), 'new_map_manager')
    print(mam.map_id_list())
    assert len(mam.map_id_list()) == 7

    # duplicate a map
    mam = dc.deep_copy()
    print(mam.map_id_list())
    assert len(mam.map_id_list()) == 6
    mam.duplicate_map_manager('map_manager', 'new_map_manager')
    print(mam.map_id_list())
    assert len(mam.map_id_list()) == 7

    # resolution_filter a map
    mam = dc.deep_copy()
    print(mam.map_id_list())
    mam.duplicate_map_manager('map_manager', 'new_map_manager')
    mam.resolution_filter(map_id='new_map_manager', d_min=3.5, d_max=6)

    # Add a model
    mam = dc.deep_copy()
    print(mam.model_id_list())
    assert len(mam.model_id_list()) == 1
    mam.add_model_by_id(mam.model().deep_copy(), 'new_model')
    print(mam.model_id_list())
    assert len(mam.model_id_list()) == 2

    # Initialize a map
    mam1 = new_mam.deep_copy()
    mam1.initialize_maps(map_value=6)
    assert mam1.map_manager().map_data()[225] == 6

    # Create mask around density and apply to all maps
    mam1 = new_mam.deep_copy()
    mam1.mask_all_maps_around_density(
        solvent_content=0.5,
        soft_mask=True,
    )
    s = (mam1.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1024, 2048))

    # Create mask around edges and apply to all maps
    mam1 = new_mam.deep_copy()
    mam1.mask_all_maps_around_edges()
    s = (mam1.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1176, 2048))

    # Create a soft mask around model and apply to all maps
    new_mam.mask_all_maps_around_atoms(mask_atoms_atom_radius=8,
                                       soft_mask=True)
    s = (new_mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1944, 2048))

    # Create a soft mask around model and do not do anything with it
    new_mam.create_mask_around_atoms(mask_atoms_atom_radius=8, soft_mask=True)
    s = (new_mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1944, 2048))

    # Create a soft mask around model and do not do anything with it, wrapping =true
    dummy_mam = new_mam.deep_copy()
    dummy_mam.map_manager().set_wrapping(True)
    dummy_mam.create_mask_around_atoms(mask_atoms_atom_radius=8,
                                       soft_mask=True)
    s = (dummy_mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1944, 2048))

    # Create a sharp mask around model and do not do anything with it
    new_mam.create_mask_around_atoms(soft_mask=False, mask_atoms_atom_radius=8)
    s = (new_mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (138, 2048))

    # Mask around edges and do not do anything with it
    mam = dc.deep_copy()
    mam.create_mask_around_edges()
    s = (mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1176, 2048))

    # Mask around density and to not do anything with it
    mam = dc.deep_copy()
    mam.create_mask_around_density(soft_mask=False)
    s = (mam.get_map_manager_by_id('mask').map_data() > 0.5)
    assert approx_equal((s.count(True), s.size()), (1000, 2048))

    # Apply the current mask to one map
    mam.apply_mask_to_map('map_manager')
    s = (mam.map_manager().map_data() > 0.)
    assert approx_equal((s.count(True), s.size()), (640, 2048))
    s = (mam.map_manager().map_data() != 0.)
    assert approx_equal((s.count(True), s.size()), (1000, 2048))
    assert approx_equal((mam.map_manager().map_data()[225]), -0.0418027862906)

    # Apply any mask to one map
    mam.apply_mask_to_map('map_manager', mask_id='mask')
    s = (mam.map_manager().map_data() > 0.)
    assert approx_equal((s.count(True), s.size()), (640, 2048))
    s = (mam.map_manager().map_data() != 0.)
    assert approx_equal((s.count(True), s.size()), (1000, 2048))
    assert approx_equal((mam.map_manager().map_data()[225]), -0.0418027862906)

    # Apply the mask to all maps
    mam.apply_mask_to_maps()
    s = (mam.map_manager().map_data() > 0.)
    assert approx_equal((s.count(True), s.size()), (640, 2048))
    s = (mam.map_manager().map_data() != 0.)
    assert approx_equal((s.count(True), s.size()), (1000, 2048))
    assert approx_equal((mam.map_manager().map_data()[225]), -0.0418027862906)

    # Apply the mask to all maps, setting outside value to mean inside
    mam.apply_mask_to_maps(set_outside_to_mean_inside=True)
    s = (mam.map_manager().map_data() > 0.)
    assert approx_equal((s.count(True), s.size()), (1688, 2048))
    s = (mam.map_manager().map_data() != 0.)
    assert approx_equal((s.count(True), s.size()), (2048, 2048))
    assert approx_equal((mam.map_manager().map_data()[2047]), -0.0759598612785)
    s = (mam.get_map_manager_by_id('mask').map_data() > 0).as_1d()
    inside = mam.map_manager().map_data().as_1d().select(s)
    outside = mam.map_manager().map_data().as_1d().select(~s)
    assert approx_equal(
        (inside.min_max_mean().max, outside.min_max_mean().max),
        (0.335603952408, 0.0239064293122))

    # Make a new map and model, get mam and box with selection
    mmm = map_model_manager()
    mmm.generate_map(box_cushion=0, wrapping=True)
    mam = mmm
    mam_dc = mam.deep_copy()

    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((18, 25, 20), (18, 25, 20)))

    # Get local fsc or randomized map
    dc = mam_dc.deep_copy()
    dc.map_manager().set_wrapping(False)
    map_coeffs = dc.map_manager().map_as_fourier_coefficients(d_min=3)
    from cctbx.development.create_models_or_maps import generate_map
    new_mm_1 = generate_map(map_coeffs=map_coeffs,
                            d_min=3,
                            low_resolution_real_space_noise_fraction=1,
                            high_resolution_real_space_noise_fraction=50,
                            map_manager=dc.map_manager(),
                            random_seed=124321)
    new_mm_2 = generate_map(map_coeffs=map_coeffs,
                            d_min=3,
                            low_resolution_real_space_noise_fraction=1,
                            high_resolution_real_space_noise_fraction=50,
                            map_manager=dc.map_manager(),
                            random_seed=734119)
    dc.add_map_manager_by_id(new_mm_1, 'map_manager_1')
    dc.add_map_manager_by_id(new_mm_2, 'map_manager_2')
    cc = dc.map_map_cc()
    fsc_curve = dc.map_map_fsc()
    dc.set_log(sys.stdout)
    dc.local_fsc(n_boxes=1)

    # Get map-map FSC
    dc = mam_dc.deep_copy()
    dc.duplicate_map_manager(map_id='map_manager', new_map_id='filtered')
    dc.resolution_filter(d_min=3.5, d_max=10, map_id='filtered')
    dc.create_mask_around_atoms()
    fsc_curve = dc.map_map_fsc(map_id_1='map_manager',
                               map_id_2='filtered',
                               mask_id='mask',
                               resolution=3.5,
                               fsc_cutoff=0.97)
    assert approx_equal(fsc_curve.d_min, 3.91175024213, eps=0.01)
    assert approx_equal(fsc_curve.fsc.fsc[-1], 0.695137718033)

    # Get map-map CC
    dc = mam_dc.deep_copy()
    dc.duplicate_map_manager(map_id='map_manager', new_map_id='filtered')
    dc.resolution_filter(d_min=3.5, d_max=6, map_id='filtered')
    cc = dc.map_map_cc('map_manager', 'filtered')
    assert approx_equal(cc, 0.706499206126)

    # Get map-map CC with mask
    dc = mam_dc.deep_copy()
    dc.duplicate_map_manager(map_id='map_manager', new_map_id='filtered')
    dc.create_mask_around_density(mask_id='filtered')
    cc = dc.map_map_cc('map_manager', 'filtered', mask_id='mask')
    assert approx_equal(cc, 0.411247493741)

    # box around model
    mam = mam_dc.deep_copy()
    mam.box_all_maps_around_model_and_shift_origin(
        selection_string="resseq 221:221")
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((18, 25, 20), (24, 20, 20)))

    # extract_around_model (get new mam)
    new_mam_dc = mam_dc.extract_all_maps_around_model(
        selection_string="resseq 221:221")
    new_mm_1a = new_mam_dc.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1a.map_data().all()),
                        ((18, 25, 20), (24, 20, 20)))
    assert approx_equal(new_mm_1.map_data(), new_mm_1a.map_data())

    # box around_density
    mam2 = mam_dc.deep_copy()
    mam2.box_all_maps_around_density_and_shift_origin(box_cushion=0)
    new_mm_2 = mam2.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_2.map_data().all()),
                        ((18, 25, 20), (16, 23, 18)))

    # extract_around_density (get new mam)
    mam2 = mam_dc.deep_copy()
    mam2_b = mam2.extract_all_maps_around_density(box_cushion=0)
    new_mm_2 = mam2_b.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_2.map_data().all()),
                        ((18, 25, 20), (16, 23, 18)))

    # Repeat as map_model_manager:
    mmm = mam_dc.as_map_model_manager().deep_copy()
    mmm.box_all_maps_around_model_and_shift_origin(
        selection_string="resseq 221:221")
    new_mm_1a = mmm.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1a.map_data().all()),
                        ((24, 20, 20), (24, 20, 20)))
    assert approx_equal(new_mm_1.map_data(), new_mm_1a.map_data())

    # box around density
    mam.box_all_maps_around_density_and_shift_origin(box_cushion=0)
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (22, 18, 18)))

    # extract around density (get new mam)
    mam1 = mam_dc.deep_copy()
    mam1.extract_all_maps_around_density(box_cushion=0)
    new_mm_1 = mam1.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (18, 25, 20)))

    # create mask around density, then box around mask (i.e., box around density)
    mam.create_mask_around_density(soft_mask=False)
    mam.box_all_maps_around_mask_and_shift_origin(box_cushion=3)
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (22, 18, 18)))

    # box with bounds
    mam.box_all_maps_with_bounds_and_shift_origin(lower_bounds=(10, 10, 10),
                                                  upper_bounds=(15, 15, 15))
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (6, 6, 6)))

    # extract with bounds
    mam = mam_dc.deep_copy()
    mam_1 = mam.extract_all_maps_with_bounds(lower_bounds=(10, 10, 10),
                                             upper_bounds=(15, 15, 15))
    new_mm_1 = mam_1.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (6, 6, 6)))

    # box with unique
    mam = mam_dc.deep_copy()
    mam.box_all_maps_around_unique_and_shift_origin(molecular_mass=2500,
                                                    resolution=3)
    new_mm_1 = mam.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (18, 25, 20)))

    # extract with unique
    mam = mam_dc.deep_copy()
    mam_1 = mam.extract_all_maps_around_unique(molecular_mass=2500,
                                               resolution=3)
    new_mm_1 = mam_1.map_manager()
    assert approx_equal((mmm.map_data().all(), new_mm_1.map_data().all()),
                        ((24, 20, 20), (18, 25, 20)))

    # extract a box and then restore model into same reference as current mam
    mam = mam_dc.deep_copy()
    mam.box_all_maps_with_bounds_and_shift_origin(lower_bounds=(2, 2, 2),
                                                  upper_bounds=(17, 17, 17))
    print("mam:",
          mam.model().get_sites_cart()[0],
          mam.map_manager().origin_is_zero())
    # extract a box
    box_mam = mam.extract_all_maps_with_bounds(lower_bounds=(10, 10, 10),
                                               upper_bounds=(15, 15, 15))
    box_model = box_mam.model()
    matched_box_model = mam.get_model_from_other(box_mam)
    assert approx_equal(matched_box_model.get_sites_cart()[0],
                        mam.model().get_sites_cart()[0])

    # Convert a map to fourier coefficients
    mam = mam_dc.deep_copy()
    ma = mam.map_as_fourier_coefficients(d_min=3)
    assert approx_equal(ma.d_min(), 3.01655042414)

    mam.add_map_from_fourier_coefficients(ma, map_id='new_map_manager')
    cc = flex.linear_correlation(
        mam.get_map_manager_by_id('map_manager').map_data().as_1d(),
        mam.get_map_manager_by_id(
            'new_map_manager').map_data().as_1d()).coefficient()
    assert (cc >= 0.99)

    # Get map-model CC
    dc = mam_dc.extract_all_maps_around_model(
        selection_string="(name ca or name cb or name c or name o) " +
        "and resseq 221:221",
        box_cushion=0)
    cc = dc.map_model_cc(resolution=3)
    assert approx_equal(cc, 0.450025539936)

    # Remove model outside map
    dc.remove_model_outside_map(boundary=0)
    assert (mam_dc.model().get_sites_cart().size(),
            dc.model().get_sites_cart().size()) == (86, 4)

    # shift a model to match the map
    dc = mam_dc.extract_all_maps_around_model(
        selection_string="(name ca or name cb or name c or name o) " +
        "and resseq 221:221",
        box_cushion=0)
    actual_model = dc.model().deep_copy()
    working_model = dc.model().deep_copy()
    working_model.set_shift_cart((0, 0, 0))
    working_model.set_sites_cart(working_model.get_sites_cart() -
                                 actual_model.shift_cart())
    dc.shift_any_model_to_match(working_model)
    assert approx_equal(actual_model.get_sites_cart()[0],
                        working_model.get_sites_cart()[0])