<commit_before>#!/usr/bin/env python
# DialsSpotfinder.py
#
#   Copyright (C) 2013 Diamond Light Source, <NAME>
#
#   This code is distributed under the BSD license, a copy of which is
#   included in the root directory of this package.
#
# Find spots for autoindexing using the DIALS code; this will probably be 
# renamed to Spotfinder at some point.

from __future__ import division

def DialsSpotfinder(DriverType = None):
  '''A factory for DialsSpotfinderWrapper classes.'''

  from Driver.DriverFactory import DriverFactory
  DriverInstance = DriverFactory.Driver(DriverType)

  class DialsSpotfinderWrapper(DriverInstance.__class__):

    def __init__(self):
      DriverInstance.__class__.__init__(self)

      self.set_executable('dials.spotfinder')
      
      return

    def __call__(self, fp, images, dials_spotfinder_phil = None):
      from Handlers.Streams import Debug
      Debug.write('Running dials.spotfinder to find spots')

      if dials_spotfinder_phil:
        # write phil as file to disk
        # pass phil on command-line below
        pass

      spotfinder_images = [fp.get_image_name(i) for i in images]
      self.clear_command_line()  
      self.add_command_line(spotfinder_images)
      self.add_command_line('-o')
      self.add_command_line('spots.pickle')    
      self.start()
      self.close_wait()

      # FIXME I should really gather some interesting information about
      import os
      return os.path.join(self.get_working_directory(), 'spots.pickle')

  return DialsSpotfinderWrapper()<commit_msg>wrapper for dials.spotfinder that does not assume dials is built into cctbx.python that was used to launch xia2<commit_after>#!/usr/bin/env python
# DialsSpotfinder.py
#
#   Copyright (C) 2013 Diamond Light Source, <NAME>, <NAME>
#
#   This code is distributed under the BSD license, a copy of which is
#   included in the root directory of this package.
#
# Find spots for autoindexing using the DIALS code; this will probably be
# renamed to Spotfinder at some point.

from __future__ import division

import os
import sys

if not os.environ.has_key('XIA2CORE_ROOT'):
  raise RuntimeError, 'XIA2CORE_ROOT not defined'

if not os.environ.has_key('XIA2_ROOT'):
  raise RuntimeError, 'XIA2_ROOT not defined'

if not os.path.join(os.environ['XIA2CORE_ROOT'],
                    'Python') in sys.path:
  sys.path.append(os.path.join(os.environ['XIA2CORE_ROOT'],
                               'Python'))

if not os.environ['XIA2_ROOT'] in sys.path:
  sys.path.append(os.environ['XIA2_ROOT'])


# interfaces that this inherits from ...
from Schema.Interfaces.FrameProcessor import FrameProcessor

from Handlers.Flags import Flags


def DialsSpotfinder(DriverType = None):
  '''A factory for DialsSpotfinderWrapper classes.'''

  from Driver.DriverFactory import DriverFactory
  DriverInstance = DriverFactory.Driver(DriverType)

  class DialsSpotfinderWrapper(DriverInstance.__class__,
                               FrameProcessor):

    def __init__(self):
      DriverInstance.__class__.__init__(self)
      FrameProcessor.__init__(self)

      self._images = []
      self._spot_range = []

      self.set_executable('dials.spotfinder')

      self._input_data_files = { }
      self._output_data_files = { }

      self._input_data_files_list = []
      self._output_data_files_list = []

      return

    def setup_from_image(self, image):
      FrameProcessor.setup_from_image(self, image)
      for i in self.get_matching_images():
        self._images.append(self.get_image_name(i))

    def setup_from_sweep(self, sweep):
      self._images.extend(sweep.paths())

    def set_input_data_file(self, name, data):
      self._input_data_files[name] = data
      return

    def get_output_data_file(self, name):
      return self._output_data_files[name]

    def run(self):
      from Handlers.Streams import Debug
      Debug.write('Running dials.spotfinder')

      self.clear_command_line()
      for image in self._images:
        self.add_command_line(image)
      for file_name in self._input_data_files.keys():
        self.add_command_line(file_name)

      self.add_command_line('-o')
      self.add_command_line('spots.pickle')
      nproc = Flags.get_parallel()
      self.add_command_line('--nproc=%i' %nproc)
      self.start()
      self.close_wait()

      # XXX probably too large to store in memory?
      #self._output_data_files.setdefault(
        #'spots.pickle',
        #open(os.path.join(self.get_working_directory(), 'spots.pickle'), 'rb')
        #.read())

      # XXX this won't work as yet if DIALS is not built into cctbx.python
      # that was used to launch xia2 but eventually the reflection list object
      # should be moved to cctbx anyway
      import cPickle as pickle
      self.reflections = pickle.load(open(
        os.path.join(self.get_working_directory(), 'spots.pickle'), 'rb'))

  return DialsSpotfinderWrapper()

if __name__ == '__main__':
  import sys
  image_files = sys.argv[1:]
  assert len(image_files) > 0
  first_image = image_files[0]
  from Wrappers.Dials.DialsImport import DialsImport
  importer = DialsImport()
  importer.setup_from_image(first_image)
  importer.run()
  print importer.sweep.get_detector()
  print importer.sweep.get_beam()
  print importer.sweep.get_goniometer()
  print importer.sweep.get_scan()
  spotfinder = DialsSpotfinder()
  spotfinder.setup_from_sweep(importer.sweep)
  # or this way:
  #spotfinder.set_input_data_file(
    #'sweep.json', importer.get_output_data_file('sweep.json'))
  # or this way:
  #spotfinder.setup_from_image(first_image)
  spotfinder.run()
  assert os.path.exists('spots.pickle')
  print "dials.spotfinder found %s spots" %len(spotfinder.reflections)
{'fraction_non_alphanumeric': 0.0951048951048951, 'fraction_numerical': 0.0036713286713286712, 'mean_word_length': 3.480031323414252, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4101798', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1761, 'n_words': 436}