Ejemplo n.º 1
0
  def create_trial(self, d_min = 1.5, n_bins = 10, **kwargs):
    # d_min and n_bins only used if isoforms are in this trial

    trial = Trial(self, d_min = d_min, **kwargs)
    if trial.target_phil_str is not None:
      from iotbx.phil import parse
      backend = ['labelit', 'dials'][['cxi.xtc_process', 'cctbx.xfel.xtc_process'].index(self.params.dispatcher)]
      if backend == 'labelit':
        from spotfinder.applications.xfel import cxi_phil
        trial_params = cxi_phil.cxi_versioned_extract().persist.phil_scope.fetch(parse(trial.target_phil_str)).extract()
        isoforms = trial_params.isoforms
      elif backend == 'dials':
        from xfel.command_line.xtc_process import phil_scope
        trial_params = phil_scope.fetch(parse(trial.target_phil_str)).extract()
        isoforms = trial_params.indexing.stills.isoforms
      else:
        assert False
      if len(isoforms) > 0:
        for isoform in isoforms:
          print "Creating isoform", isoform.name
          db_isoform = Isoform(self,
                               name = isoform.name,
                               trial_id = trial.id)
          a, b, c, alpha, beta, gamma = isoform.cell.parameters()
          cell = self.create_cell(cell_a = a, cell_b = b, cell_c = c,
                                  cell_alpha = alpha, cell_beta = beta, cell_gamma = gamma,
                                  lookup_symbol = isoform.lookup_symbol,
                                  isoform_id = db_isoform.id)
          from cctbx.crystal import symmetry

          cs = symmetry(unit_cell = isoform.cell,space_group_symbol=str(isoform.lookup_symbol))
          mset = cs.build_miller_set(anomalous_flag=False, d_min=d_min)
          binner = mset.setup_binner(n_bins=n_bins)
          for i in binner.range_used():
            d_max, d_min = binner.bin_d_range(i)
            Bin(self, number = i, d_min = d_min, d_max = d_max,
                total_hkl = binner.counts_complete()[i], cell_id = cell.id)
      elif backend == 'labelit':
        pass # TODO: labelit target
      elif backend == 'dials':
        if trial_params.indexing.known_symmetry.unit_cell is not None and \
            trial_params.indexing.known_symmetry.space_group is not None:
          print "Creating target cell"
          unit_cell = trial_params.indexing.known_symmetry.unit_cell
          symbol = str(trial_params.indexing.known_symmetry.space_group)
          a, b, c, alpha, beta, gamma = unit_cell.parameters()
          cell = self.create_cell(cell_a = a, cell_b = b, cell_c = c,
                                  cell_alpha = alpha, cell_beta = beta, cell_gamma = gamma,
                                  lookup_symbol = symbol,
                                  trial_id = trial.id)
          from cctbx.crystal import symmetry

          cs = symmetry(unit_cell = unit_cell, space_group_symbol = symbol)
          mset = cs.build_miller_set(anomalous_flag=False, d_min=d_min)
          binner = mset.setup_binner(n_bins=n_bins)
          for i in binner.range_used():
            d_max, d_min = binner.bin_d_range(i)
            Bin(self, number = i, d_min = d_min, d_max = d_max,
                total_hkl = binner.counts_complete()[i], cell_id = cell.id)
    return trial
Ejemplo n.º 2
0
def log_frame(experiments,
              reflections,
              params,
              run,
              n_strong,
              timestamp=None,
              two_theta_low=None,
              two_theta_high=None,
              db_event=None,
              app=None,
              trial=None):
    if app is None:
        app = dxtbx_xfel_db_application(params, mode='cache_commits')
    else:
        app.mode = 'cache_commits'

    if isinstance(run, int) or isinstance(run, str):
        db_run = app.get_run(run_number=run)
    else:
        db_run = run

    if trial is None:
        if params.input.trial is None:
            db_trial = app.get_trial(trial_id=params.input.trial_id)
            params.input.trial = db_trial.trial
        else:
            db_trial = app.get_trial(trial_number=params.input.trial)
    else:
        db_trial = trial

    if db_event is None:
        if params.input.rungroup is None:
            db_event = app.create_event(timestamp=timestamp,
                                        run_id=db_run.id,
                                        trial_id=db_trial.id,
                                        n_strong=n_strong,
                                        two_theta_low=two_theta_low,
                                        two_theta_high=two_theta_high)
        else:
            db_event = app.create_event(timestamp=timestamp,
                                        run_id=db_run.id,
                                        trial_id=db_trial.id,
                                        rungroup_id=params.input.rungroup,
                                        n_strong=n_strong,
                                        two_theta_low=two_theta_low,
                                        two_theta_high=two_theta_high)

    inserts = ""

    if app.last_query is None:
        app.last_query = ""

    def save_last_id(name):
        nonlocal inserts
        inserts += app.last_query + ";\n"
        inserts += "SELECT LAST_INSERT_ID() INTO @%s_id;\n" % name

    if experiments:
        save_last_id('event')
    else:
        inserts += app.last_query + ";\n"

    for i, experiment in enumerate(experiments or []):
        reflections_i = reflections.select(reflections['id'] == i)

        imageset = Imageset(app)
        save_last_id('imageset')

        beam = Beam(app, beam=experiment.beam)
        save_last_id('beam')

        detector = Detector(app, detector=experiment.detector)
        save_last_id('detector')

        cell = Cell(app, crystal=experiment.crystal, isoform_id=None)
        save_last_id('cell')

        crystal = Crystal(app,
                          crystal=experiment.crystal,
                          make_cell=False,
                          cell_id="@cell_id")
        save_last_id('crystal')

        inserts += ("INSERT INTO `%s_experiment` (imageset_id, beam_id, detector_id, crystal_id, crystal_cell_id) " + \
                    "VALUES (@imageset_id, @beam_id, @detector_id, @crystal_id, @cell_id);\n") % (
          params.experiment_tag)

        inserts += "INSERT INTO `%s_imageset_event` (imageset_id, event_id, event_run_id) VALUES (@imageset_id, @event_id, %d);\n" % (
            params.experiment_tag, db_run.id)

        d = experiment.crystal.get_unit_cell().d(
            reflections['miller_index']).select(reflections['id'] == i)
        from cctbx.crystal import symmetry
        cs = symmetry(unit_cell=experiment.crystal.get_unit_cell(),
                      space_group=experiment.crystal.get_space_group())
        mset = cs.build_miller_set(anomalous_flag=False, d_min=db_trial.d_min)
        n_bins = 10  # FIXME use n_bins as an attribute on the trial table
        binner = mset.setup_binner(n_bins=n_bins)
        for i in binner.range_used():
            d_max, d_min = binner.bin_d_range(i)
            Bin(app,
                number=i,
                d_min=d_min,
                d_max=d_max,
                total_hkl=binner.counts_complete()[i],
                cell_id='@cell_id')
            save_last_id('bin')

            sel = (d <= float(d_max)) & (d > float(d_min))
            sel &= reflections_i['intensity.sum.value'] > 0
            refls = reflections_i.select(sel)
            n_refls = len(refls)
            Cell_Bin(
                app,
                count=n_refls,
                bin_id='@bin_id',
                crystal_id='@crystal_id',
                avg_intensity=flex.mean(refls['intensity.sum.value'])
                if n_refls > 0 else None,
                avg_sigma=flex.mean(flex.sqrt(refls['intensity.sum.variance']))
                if n_refls > 0 else None,
                avg_i_sigi=flex.mean(
                    refls['intensity.sum.value'] /
                    flex.sqrt(refls['intensity.sum.variance']))
                if n_refls > 0 else None)
            inserts += app.last_query + ";\n"
    app.mode = 'execute'
    return inserts
Ejemplo n.º 3
0
def log_frame(experiments,
              reflections,
              params,
              run,
              n_strong,
              timestamp=None,
              two_theta_low=None,
              two_theta_high=None,
              db_event=None):
    app = dxtbx_xfel_db_application(params)
    db_run = app.get_run(run_number=run)
    if params.input.trial is None:
        db_trial = app.get_trial(trial_id=params.input.trial_id)
        params.input.trial = db_trial.trial
    else:
        db_trial = app.get_trial(trial_number=params.input.trial)

    if db_event is None:
        if params.input.rungroup is None:
            db_event = app.create_event(timestamp=timestamp,
                                        run_id=db_run.id,
                                        trial_id=db_trial.id,
                                        n_strong=n_strong,
                                        two_theta_low=two_theta_low,
                                        two_theta_high=two_theta_high)
        else:
            db_event = app.create_event(timestamp=timestamp,
                                        run_id=db_run.id,
                                        trial_id=db_trial.id,
                                        rungroup_id=params.input.rungroup,
                                        n_strong=n_strong,
                                        two_theta_low=two_theta_low,
                                        two_theta_high=two_theta_high)

    for i, experiment in enumerate(experiments or []):
        reflections_i = reflections.select(reflections['id'] == i)
        db_experiment = app.create_experiment(experiment)
        app.link_imageset_frame(db_experiment.imageset, db_event)

        d = experiment.crystal.get_unit_cell().d(
            reflections['miller_index']).select(reflections['id'] == i)
        if len(
                db_experiment.crystal.cell.bins
        ) == 0:  # will be [] if there are no isoforms and no target cells
            from cctbx.crystal import symmetry
            cs = symmetry(
                unit_cell=db_experiment.crystal.cell.unit_cell,
                space_group_symbol=db_experiment.crystal.cell.lookup_symbol)
            mset = cs.build_miller_set(anomalous_flag=False,
                                       d_min=db_trial.d_min)
            n_bins = 10  # FIXME use n_bins as an attribute on the trial table
            binner = mset.setup_binner(n_bins=n_bins)
            for i in binner.range_used():
                d_max, d_min = binner.bin_d_range(i)
                Bin(app,
                    number=i,
                    d_min=d_min,
                    d_max=d_max,
                    total_hkl=binner.counts_complete()[i],
                    cell_id=db_experiment.crystal.cell.id)
            db_experiment.crystal.cell.bins = app.get_cell_bins(
                db_experiment.crystal.cell.id)
            assert len(db_experiment.crystal.cell.bins) == n_bins

        for db_bin in db_experiment.crystal.cell.bins:
            sel = (d <= float(db_bin.d_max)) & (d > float(db_bin.d_min))
            sel &= reflections_i['intensity.sum.value'] > 0
            refls = reflections_i.select(sel)
            n_refls = len(refls)
            Cell_Bin(
                app,
                count=n_refls,
                bin_id=db_bin.id,
                crystal_id=db_experiment.crystal.id,
                avg_intensity=flex.mean(refls['intensity.sum.value'])
                if n_refls > 0 else None,
                avg_sigma=flex.mean(flex.sqrt(refls['intensity.sum.variance']))
                if n_refls > 0 else None,
                avg_i_sigi=flex.mean(
                    refls['intensity.sum.value'] /
                    flex.sqrt(refls['intensity.sum.variance']))
                if n_refls > 0 else None)
    return db_event