def tst_find_best_cell():
    uc_array = [
        uctbx.unit_cell('40, 50, 60, 90, 90, 90'),
        uctbx.unit_cell('40, 60, 50, 90, 90, 90'),
        uctbx.unit_cell('50, 40, 60, 90, 90, 90'),
        uctbx.unit_cell('50, 60, 40, 90, 90, 90'),
        uctbx.unit_cell('60, 40, 50, 90, 90, 90'),
        uctbx.unit_cell('60, 50, 40, 90, 90, 90')
    ]

    uc_correct = [
        uctbx.unit_cell('40, 50, 60, 90, 90, 90'),
        uctbx.unit_cell('40, 60, 50, 90, 90, 90'),
        uctbx.unit_cell('40, 50, 60, 90, 90, 90'),
        uctbx.unit_cell('50, 60, 40, 90, 90, 90'),
        uctbx.unit_cell('40, 60, 50, 90, 90, 90'),
        uctbx.unit_cell('50, 60, 40, 90, 90, 90')
    ]

    sg_info = sgtbx.space_group_info('P 21 21 2')
    sg_info_2 = sgtbx.space_group_info('I 21 21 21')

    sg = sg_info.group()
    sg_2 = sg_info_2.group()

    for uc, correct in zip(uc_array, uc_correct):
        best_cell_finder = fbc(uc, sg)
        assert approx_equal(correct.parameters(),
                            best_cell_finder.return_best_cell().parameters())

        cb_op = best_cell_finder.return_change_of_basis_op_to_best_cell()
        xs = crystal.symmetry(uc, space_group=sg)
        assert approx_equal(correct.parameters(),
                            xs.change_basis(cb_op).unit_cell().parameters())

        best_cell_finder = fbc(uc, sg_2)
        assert approx_equal(uc_array[0].parameters(),
                            best_cell_finder.return_best_cell().parameters())

        xs = crystal.symmetry(uc, space_group=sg_2)
        cb_op = best_cell_finder.return_change_of_basis_op_to_best_cell()
        assert approx_equal(uc_array[0].parameters(),
                            xs.change_basis(cb_op).unit_cell().parameters())

    # test with incomming sg not in reference setting
    uc = uctbx.unit_cell('60, 40, 30, 90, 90, 90')
    sg_info_3 = sgtbx.space_group_info('P 1 1 21')
    sg_3 = sg_info_3.group()
    best_cell_finder = fbc(uc, sg_3)
    xs_best = best_cell_finder.return_best_xs()

    uc_correct = uctbx.unit_cell('40, 30, 60, 90, 90, 90')
    sg_correct = sgtbx.space_group_info('P 1 21 1').group()
    assert approx_equal(xs_best.unit_cell().parameters(),
                        uc_correct.parameters())
    assert sg_correct == xs_best.space_group()
def tst_find_best_cell():
  uc_array=[ uctbx.unit_cell( '40, 50, 60, 90, 90, 90' ),
             uctbx.unit_cell( '40, 60, 50, 90, 90, 90' ),
             uctbx.unit_cell( '50, 40, 60, 90, 90, 90' ),
             uctbx.unit_cell( '50, 60, 40, 90, 90, 90' ),
             uctbx.unit_cell( '60, 40, 50, 90, 90, 90' ),
             uctbx.unit_cell( '60, 50, 40, 90, 90, 90' ) ]

  uc_correct = [ uctbx.unit_cell( '40, 50, 60, 90, 90, 90' ),
                 uctbx.unit_cell( '40, 60, 50, 90, 90, 90' ),
                 uctbx.unit_cell( '40, 50, 60, 90, 90, 90' ),
                 uctbx.unit_cell( '50, 60, 40, 90, 90, 90' ),
                 uctbx.unit_cell( '40, 60, 50, 90, 90, 90' ),
                 uctbx.unit_cell( '50, 60, 40, 90, 90, 90' ) ]

  sg_info =  sgtbx.space_group_info( 'P 21 21 2' )
  sg_info_2 = sgtbx.space_group_info( 'I 21 21 21' )

  sg = sg_info.group()
  sg_2 = sg_info_2.group()

  for uc, correct in zip(uc_array,uc_correct):
    best_cell_finder = fbc( uc, sg )
    assert approx_equal( correct.parameters(),
                         best_cell_finder.return_best_cell().parameters() )

    cb_op = best_cell_finder.return_change_of_basis_op_to_best_cell()
    xs = crystal.symmetry( uc, space_group=sg)
    assert approx_equal( correct.parameters(),
                         xs.change_basis(cb_op).unit_cell().parameters() )


    best_cell_finder = fbc( uc, sg_2 )
    assert approx_equal( uc_array[0].parameters(),
                         best_cell_finder.return_best_cell().parameters() )

    xs = crystal.symmetry( uc, space_group=sg_2)
    cb_op = best_cell_finder.return_change_of_basis_op_to_best_cell()
    assert approx_equal( uc_array[0].parameters(),
                         xs.change_basis(cb_op).unit_cell().parameters() )

  # test with incomming sg not in reference setting
  uc = uctbx.unit_cell( '60, 40, 30, 90, 90, 90' )
  sg_info_3 = sgtbx.space_group_info( 'P 1 1 21' )
  sg_3 = sg_info_3.group()
  best_cell_finder =  fbc( uc, sg_3 )
  xs_best =  best_cell_finder.return_best_xs()

  uc_correct = uctbx.unit_cell( '40, 30, 60, 90, 90, 90' )
  sg_correct = sgtbx.space_group_info( 'P 1 21 1' ).group()
  assert approx_equal(  xs_best.unit_cell().parameters(),
                        uc_correct.parameters() )
  assert sg_correct == xs_best.space_group()
def test_reference_setting_choices():
  buffer = StringIO()

  for space_group_info in sgtbx.reference_space_group_infos():
    space_group = space_group_info.group()

    uc = space_group_info.any_compatible_unit_cell(volume=57*57*76)
    xs = crystal.symmetry(uc, space_group=space_group)

    cobs = pt.reference_setting_choices(space_group)

    if len(cobs)>1:
      tmp_array = []
      for cob in cobs:
        xs_new =crystal.symmetry(xs.change_basis( cob ).unit_cell(),
                                 space_group=xs.space_group() )
        best_cell_finder = fbc(xs_new.unit_cell(),
                               xs_new.space_group() )

        xs_new = best_cell_finder.return_best_xs()
        tmp_array.append(xs_new)
      count = 0
      for tmp_xs1 in xrange(len(tmp_array)):
        for tmp_xs2 in xrange(len(tmp_array)):

          if (tmp_xs1 != tmp_xs2):
            assert (tmp_array[tmp_xs1].space_group()
                    == tmp_array[tmp_xs2].space_group())
            assert not approx_equal(
              tmp_array[tmp_xs1].unit_cell().parameters(),
              tmp_array[tmp_xs2].unit_cell().parameters(),
              eps=0.001,
              out=buffer)
def test_reference_setting_choices():
    buffer = StringIO()

    for space_group_info in sgtbx.reference_space_group_infos():
        space_group = space_group_info.group()

        uc = space_group_info.any_compatible_unit_cell(volume=57 * 57 * 76)
        xs = crystal.symmetry(uc, space_group=space_group)

        cobs = pt.reference_setting_choices(space_group)

        if len(cobs) > 1:
            tmp_array = []
            for cob in cobs:
                xs_new = crystal.symmetry(xs.change_basis(cob).unit_cell(),
                                          space_group=xs.space_group())
                best_cell_finder = fbc(xs_new.unit_cell(),
                                       xs_new.space_group())

                xs_new = best_cell_finder.return_best_xs()
                tmp_array.append(xs_new)
            count = 0
            for tmp_xs1 in xrange(len(tmp_array)):
                for tmp_xs2 in xrange(len(tmp_array)):

                    if (tmp_xs1 != tmp_xs2):
                        assert (tmp_array[tmp_xs1].space_group() ==
                                tmp_array[tmp_xs2].space_group())
                        assert not approx_equal(
                            tmp_array[tmp_xs1].unit_cell().parameters(),
                            tmp_array[tmp_xs2].unit_cell().parameters(),
                            eps=0.001,
                            out=buffer)
def test_extensively( this_chunk ):
  n_chunks = 1
  i_chunk = int(this_chunk)

  assert i_chunk <= n_chunks
  for i_sg, sg in enumerate(sgtbx.space_group_symbol_iterator()):
    if i_sg % n_chunks != i_chunk:
      continue

    buffer = StringIO()

    group = sgtbx.space_group(sg.hall())

    #if group != sgtbx.space_group_info( symbol = 'I 21 21 21' ).group() :
    #  continue

    unit_cell =sgtbx.space_group_info(group=group).any_compatible_unit_cell(
      volume=57*57*76)

    xs_in = crystal.symmetry(unit_cell=unit_cell, space_group=group)
    xs_in = xs_in.best_cell()

    # Just make sure we have the 'best cell' to start out with

    xs_minimum = xs_in.change_basis(xs_in.change_of_basis_op_to_niggli_cell())

    sg_min = xs_minimum.space_group()
    sg_min_info = sgtbx.space_group_info(group = sg_min)
    sg_min_to_ref = sg_min_info.change_of_basis_op_to_reference_setting()
    sg_in_to_min = xs_in.change_of_basis_op_to_niggli_cell()

    xs_ref = xs_minimum.change_basis(sg_min_to_ref)
    best_cell_finder = fbc(xs_ref.unit_cell(),
                           xs_ref.space_group())
    xs_ref = best_cell_finder.return_best_xs()

    xs_lattice_pg = sgtbx.lattice_symmetry.group(
      xs_minimum.unit_cell(),
      max_delta=5.0)
    sg_in_ref_setting = sg_min.change_basis(sg_min_to_ref)

    # -----------
    queue = []
    if sg_in_ref_setting.is_chiral():
      print "Testing : ",\
            sgtbx.space_group_info(group=group),\
            "( or ", sgtbx.space_group_info(group=sg_in_ref_setting),\
            " in reference setting)"
      for s in sg_min:
        new_sg = sgtbx.space_group()
        new_sg.expand_smx(s)
        if new_sg in queue:
          continue
        else:
          queue.append(new_sg)
        xs_cheat = crystal.symmetry(xs_minimum.unit_cell(),
                                    space_group=new_sg)
        #print
        #print "Using symop", s

        xs_cheat_min = xs_cheat.change_basis(
          xs_cheat.change_of_basis_op_to_niggli_cell() )

        #print "This results in space group: ", xs_cheat.space_group_info()

        sg_clues = pt.space_group_graph_from_cell_and_sg(
          xs_cheat_min.unit_cell(),
          xs_cheat_min.space_group() )

        #sg_clues.show()

        found_it = False
        #print
        #print " --- Spacegroups consistent with input parameters ---"
        for sg_and_uc in sg_clues.return_likely_sg_and_cell():
          check_sg = False
          check_uc = False
          xs = crystal.symmetry(sg_and_uc[1], space_group=sg_and_uc[0])
          if approx_equal(
            sg_and_uc[1].parameters(),
            xs_ref.unit_cell().parameters(),
            eps=0.001,
            out=buffer):
            if sg_and_uc[0] == sg_in_ref_setting:
              found_it = True

          #print sgtbx.space_group_info( group=sg_and_uc[0] ), \
          #      sg_and_uc[1].parameters()
        if not found_it:
          print "FAILURE: ", sg.hall()
          assert found_it
def test_extensively(this_chunk):
    n_chunks = 1
    i_chunk = int(this_chunk)

    assert i_chunk <= n_chunks
    for i_sg, sg in enumerate(sgtbx.space_group_symbol_iterator()):
        if i_sg % n_chunks != i_chunk:
            continue

        buffer = StringIO()

        group = sgtbx.space_group(sg.hall())

        #if group != sgtbx.space_group_info( symbol = 'I 21 21 21' ).group():
        #  continue

        unit_cell = sgtbx.space_group_info(
            group=group).any_compatible_unit_cell(volume=57 * 57 * 76)

        xs_in = crystal.symmetry(unit_cell=unit_cell, space_group=group)
        xs_in = xs_in.best_cell()

        # Just make sure we have the 'best cell' to start out with

        xs_minimum = xs_in.change_basis(
            xs_in.change_of_basis_op_to_niggli_cell())

        sg_min = xs_minimum.space_group()
        sg_min_info = sgtbx.space_group_info(group=sg_min)
        sg_min_to_ref = sg_min_info.change_of_basis_op_to_reference_setting()
        sg_in_to_min = xs_in.change_of_basis_op_to_niggli_cell()

        xs_ref = xs_minimum.change_basis(sg_min_to_ref)
        best_cell_finder = fbc(xs_ref.unit_cell(), xs_ref.space_group())
        xs_ref = best_cell_finder.return_best_xs()

        xs_lattice_pg = sgtbx.lattice_symmetry.group(xs_minimum.unit_cell(),
                                                     max_delta=5.0)
        sg_in_ref_setting = sg_min.change_basis(sg_min_to_ref)

        # -----------
        queue = []
        if sg_in_ref_setting.is_chiral():
            print "Testing : ",\
                  sgtbx.space_group_info(group=group),\
                  "( or ", sgtbx.space_group_info(group=sg_in_ref_setting),\
                  " in reference setting)"
            for s in sg_min:
                new_sg = sgtbx.space_group()
                new_sg.expand_smx(s)
                if new_sg in queue:
                    continue
                else:
                    queue.append(new_sg)
                xs_cheat = crystal.symmetry(xs_minimum.unit_cell(),
                                            space_group=new_sg)
                #print
                #print "Using symop", s

                xs_cheat_min = xs_cheat.change_basis(
                    xs_cheat.change_of_basis_op_to_niggli_cell())

                #print "This results in space group: ", xs_cheat.space_group_info()

                sg_clues = pt.space_group_graph_from_cell_and_sg(
                    xs_cheat_min.unit_cell(), xs_cheat_min.space_group())

                #sg_clues.show()

                found_it = False
                #print
                #print " --- Spacegroups consistent with input parameters ---"
                for sg_and_uc in sg_clues.return_likely_sg_and_cell():
                    check_sg = False
                    check_uc = False
                    xs = crystal.symmetry(sg_and_uc[1],
                                          space_group=sg_and_uc[0])
                    if approx_equal(sg_and_uc[1].parameters(),
                                    xs_ref.unit_cell().parameters(),
                                    eps=0.001,
                                    out=buffer):
                        if sg_and_uc[0] == sg_in_ref_setting:
                            found_it = True

                    #print sgtbx.space_group_info( group=sg_and_uc[0] ), \
                    #      sg_and_uc[1].parameters()
                if not found_it:
                    print "FAILURE: ", sg.hall()
                    assert found_it