<commit_before>from __future__ import absolute_import, division, print_function

import glob
import os

import procrunner
import pytest
import scitbx

def test_thing_1(tmpdir, dials_regression):
  '''Would you like to know more about what this test is supposed to do?
     I would love to. Always remember to use descriptive names.'''

  data_dir = os.path.join(dials_regression, "indexing_test_data", "trypsin")
  pickle_path1 = os.path.join(data_dir, "strong_P1_X6_1_0-1.pickle")
  pickle_path2 = os.path.join(data_dir, "strong_P1_X6_2_0-1.pickle")
  datablock_path1 = os.path.join(data_dir, "datablock_P1_X6_1.json")
  datablock_path2 = os.path.join(data_dir, "datablock_P1_X6_2.json")

  tmpdir.chdir()

  args = ["dials.search_beam_position",
          datablock_path1,
          datablock_path2,
          pickle_path1,
          pickle_path2]

  print(args)
  result = procrunner.run_process(args)
  assert result['stderr'] == '' and result['exitcode'] == 0
  assert os.path.exists('optimized_datablock.json')

  from dxtbx.serialize import load
  datablocks = load.datablock(datablock_path1, check_format=False)
  original_imageset = datablocks[0].extract_imagesets()[0]
  optimized_datablock = load.datablock('optimized_datablock.json',
                                       check_format=False)
  detector_1 = original_imageset.get_detector()
  detector_2 = optimized_datablock[0].unique_detectors()[0]
  shift = (scitbx.matrix.col(detector_1[0].get_origin()) -
           scitbx.matrix.col(detector_2[0].get_origin()))
  assert shift.elems == pytest.approx((0.037, 0.061, 0.0), abs=1e-1)

def test_thing_2(tmpdir, xia2_regression_build):
  '''Would you like to know more about what this test is supposed to do?
     I would love to. Always remember to use descriptive names.'''

  data_dir = os.path.join(xia2_regression_build, "test_data", "i04_bag_training")

  g = glob.glob(os.path.join(data_dir, "*.cbf*"))
  if not g:
    pytest.skip('xia2_regression files not downloaded. Run xia2_regression.fetch_test_data first')

  tmpdir.chdir()
  # beam centre from image headers: 205.28,210.76 mm
  args = ["dials.import", "mosflm_beam_centre=207,212"] + g
  print(args)
  result = procrunner.run_process(args)
  assert result['stderr'] == '' and result['exitcode'] == 0
  assert os.path.exists('datablock.json')

  # spot-finding, just need a subset of the data
  args = ["dials.find_spots", "datablock.json",
          "scan_range=1,10", "scan_range=531,540"]
  print(args)
  result = procrunner.run_process(args)
  assert result['stderr'] == '' and result['exitcode'] == 0
  assert os.path.exists('strong.pickle')

  # actually run the beam centre search
  args = ["dials.search_beam_position", "datablock.json",
          "strong.pickle"]
  print(args)
  result = procrunner.run_process(args)
  assert result['stderr'] == '' and result['exitcode'] == 0
  assert os.path.exists('optimized_datablock.json')

  # look at the results
  from dxtbx.serialize import load
  datablocks = load.datablock("datablock.json", check_format=False)
  original_imageset = datablocks[0].extract_imagesets()[0]
  optimized_datablock = load.datablock('optimized_datablock.json',
                                       check_format=False)
  detector_1 = original_imageset.get_detector()
  detector_2 = optimized_datablock[0].unique_detectors()[0]
  shift = (scitbx.matrix.col(detector_1[0].get_origin()) -<commit_msg>work around Windows command line limits<commit_after>from __future__ import absolute_import, division, print_function

import glob
import os

import procrunner
import pytest
import scitbx

def test_thing_1(tmpdir, dials_regression):
  '''Would you like to know more about what this test is supposed to do?
     I would love to. Always remember to use descriptive names.'''

  data_dir = os.path.join(dials_regression, "indexing_test_data", "trypsin")
  pickle_path1 = os.path.join(data_dir, "strong_P1_X6_1_0-1.pickle")
  pickle_path2 = os.path.join(data_dir, "strong_P1_X6_2_0-1.pickle")
  datablock_path1 = os.path.join(data_dir, "datablock_P1_X6_1.json")
  datablock_path2 = os.path.join(data_dir, "datablock_P1_X6_2.json")

  tmpdir.chdir()

  args = ["dials.search_beam_position",
          datablock_path1,
          datablock_path2,
          pickle_path1,
          pickle_path2]

  print(args)
  result = procrunner.run_process(args)
  assert result['stderr'] == '' and result['exitcode'] == 0
  assert os.path.exists('optimized_datablock.json')

  from dxtbx.serialize import load
  datablocks = load.datablock(datablock_path1, check_format=False)
  original_imageset = datablocks[0].extract_imagesets()[0]
  optimized_datablock = load.datablock('optimized_datablock.json',
                                       check_format=False)
  detector_1 = original_imageset.get_detector()
  detector_2 = optimized_datablock[0].unique_detectors()[0]
  shift = (scitbx.matrix.col(detector_1[0].get_origin()) -
           scitbx.matrix.col(detector_2[0].get_origin()))
  assert shift.elems == pytest.approx((0.037, 0.061, 0.0), abs=1e-1)

def test_thing_2(tmpdir, xia2_regression_build):
  '''Would you like to know more about what this test is supposed to do?
     I would love to. Always remember to use descriptive names.'''

  data_dir = os.path.join(xia2_regression_build, "test_data", "i04_bag_training")

  g = glob.glob(os.path.join(data_dir, "*.cbf*"))
  if not g:
    pytest.skip('xia2_regression files not downloaded. Run xia2_regression.fetch_test_data first')

  tmpdir.chdir()

  # beam centre from image headers: 205.28,210.76 mm
  args = ["dials.import", "mosflm_beam_centre=207,212"] + g
  print(args)
  if os.name != 'nt':
    result = procrunner.run_process(args)
    assert result['stderr'] == '' and result['exitcode'] == 0
  else:
    # Can't run this command on Windows, as it will exceed the maximum Windows command length limits.
    # So, instead:
    import mock
    import sys
    with mock.patch.object(sys, 'argv', args):
      import dials.command_line.dials_import
      dials.command_line.dials_import.Script().run()

  assert os.path.exists('datablock.json')

  # spot-finding, just need a subset of the data
  args = ["dials.find_spots", "datablock.json",
          "scan_range=1,10", "scan_range=531,540"]
  print(args)
  result = procrunner.run_process(args)
  assert result['stderr'] == '' and result['exitcode'] == 0
  assert os.path.exists('strong.pickle')

  # actually run the beam centre search
  args = ["dials.search_beam_position", "datablock.json",
          "strong.pickle"]
  print(args)
  result = procrunner.run_process(args)
  assert result['stderr'] == '' and result['exitcode'] == 0
  assert os.path.exists('optimized_datablock.json')

  # look at the results
  from dxtbx.serialize import load
  datablocks = load.datablock("datablock.json", check_format=False)
  original_imageset = datablocks[0].extract_imagesets()[0]
  optimized_datablock = load.datablock('optimized_datablock.json',
                                       check_format=False)
  detector_1 = original_imageset.get_detector()
  detector_2 = optimized_datablock[0].unique_detectors()[0]
  shift = (scitbx.matrix.col(detector_1[0].get_origin()) -
{'fraction_non_alphanumeric': 0.12111498257839722, 'fraction_numerical': 0.02564459930313589, 'mean_word_length': 4.170028818443804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6532547', 'n_tokens_mistral': 2594, 'n_tokens_neox': 2476, 'n_words': 502}