def write_fit_group(f, label, group):
  if (label == "h_sds"): # retro-fitted
    group = list(reversed(group))
  id = identifier(label)
  s = "D %s_s[] = {" % id
  for fit in group:
    s += " " + str(fit.stol) + ","
  s = s[:-1] + " };"
  print(s, file=f)
  print("D %s_e[] = {" % id, file=f)
  if (label == "h_sds"): # retro-fitted
    from cctbx.eltbx.development.hydrogen_plots import fit_input
    from scitbx.array_family import flex
    fi = fit_input()
  for fit in group:
    sel = fi.stols <= fit.stol + 1.e-6
    if (label == "h_sds"): # retro-fitted
      gaussian_fit = scitbx.math.gaussian.fit(
        fi.stols.select(sel),
        fi.data.select(sel),
        fi.sigmas.select(sel),
        fit)
      s = str(flex.max(gaussian_fit.significant_relative_errors()))
    else:
      s = str(fit.max_error)
    if (fit is not group[-1]): s += ","
    print(s, file=f)
  print("};", file=f)
  labels = []
  for fit_unsorted in group:
    fit = fit_unsorted.sort()
    lbl = "%s_%d" % (id, fit.n_terms())
    if (fit.use_c()): lbl += "c"
    print("D %s[] = {" % lbl, file=f)
    labels.append(lbl)
    buf = []
    for a,b in zip(fit.array_of_a(), fit.array_of_b()):
      buf.append("%s, %s," % (str(a), str(b)))
    if (fit.use_c()):
      buf.append(str(fit.c()))
    else:
      buf[-1] = buf[-1][:-1]
    for s in buf: print(s, file=f)
    print("};", file=f)
  print("D* %s_c[] = { %s };" % (id, ", ".join(labels)), file=f)
  print(file=f)
  return
  print("""\
""", file=f)
def write_fit_group(f, label, group):
  if (label == "h_sds"): # retro-fitted
    group = list(reversed(group))
  id = identifier(label)
  s = "D %s_s[] = {" % id
  for fit in group:
    s += " " + str(fit.stol) + ","
  s = s[:-1] + " };"
  print >> f, s
  print >> f, "D %s_e[] = {" % id
  if (label == "h_sds"): # retro-fitted
    from cctbx.eltbx.development.hydrogen_plots import fit_input
    from scitbx.array_family import flex
    fi = fit_input()
  for fit in group:
    sel = fi.stols <= fit.stol + 1.e-6
    if (label == "h_sds"): # retro-fitted
      gaussian_fit = scitbx.math.gaussian.fit(
        fi.stols.select(sel),
        fi.data.select(sel),
        fi.sigmas.select(sel),
        fit)
      s = str(flex.max(gaussian_fit.significant_relative_errors()))
    else:
      s = str(fit.max_error)
    if (fit is not group[-1]): s += ","
    print >> f, s
  print >> f, "};"
  labels = []
  for fit_unsorted in group:
    fit = fit_unsorted.sort()
    lbl = "%s_%d" % (id, fit.n_terms())
    if (fit.use_c()): lbl += "c"
    print >> f, "D %s[] = {" % lbl
    labels.append(lbl)
    buf = []
    for a,b in zip(fit.array_of_a(), fit.array_of_b()):
      buf.append("%s, %s," % (str(a), str(b)))
    if (fit.use_c()):
      buf.append(str(fit.c()))
    else:
      buf[-1] = buf[-1][:-1]
    for s in buf: print >> f, s
    print >> f, "};"
  print >> f, "D* %s_c[] = { %s };" % (id, ", ".join(labels))
  print >> f
  return
  print >> f, """\
Beispiel #3
0
def run(file_name,
        args,
        cutoff,
        params,
        zig_zag=False,
        six_term=False,
        full_fits=None,
        plots_dir="itvc_fits_plots",
        verbose=0):
    tab = itvc_section61_io.read_table6111(file_name)
    chunk_n = 1
    chunk_i = 0
    if (len(args) > 0 and len(args[0].split(",")) == 2):
        chunk_n, chunk_i = [int(i) for i in args[0].split(",")]
        args = args[1:]
    if (not six_term and not zig_zag):
        if (not os.path.isdir(plots_dir)):
            print("No plots because target directory does not exist (mkdir %s)." % \
              plots_dir)
            plots_dir = None
        if (chunk_n > 1):
            assert plots_dir is not None
    stols_more = cctbx.eltbx.gaussian_fit.international_tables_stols
    sel = stols_more <= cutoff + 1.e-6
    stols = stols_more.select(sel)
    i_chunk = 0
    for element in tab.elements + ["O2-", "SDS"]:
        if (len(args) > 0 and element not in args): continue
        flag = i_chunk % chunk_n == chunk_i
        i_chunk += 1
        if (not flag):
            continue
        results = {}
        results["fit_parameters"] = params
        if (element == "SDS"):
            wrk_lbl = element
            from cctbx.eltbx.development.hydrogen_plots import fit_input
            fi = fit_input()
            sel = fi.stols <= cutoff + 1.e-6
            null_fit = scitbx.math.gaussian.fit(
                fi.stols.select(sel), fi.data.select(sel),
                fi.sigmas.select(sel), xray_scattering.gaussian(0, False))
            null_fit_more = scitbx.math.gaussian.fit(
                fi.stols, fi.data, fi.sigmas,
                xray_scattering.gaussian(0, False))
        else:
            wrk_lbl = xray_scattering.wk1995(element, True)
            if (element != "O2-"):
                entry = tab.entries[element]
                null_fit = scitbx.math.gaussian.fit(
                    stols, entry.table_y[:stols.size()],
                    entry.table_sigmas[:stols.size()],
                    xray_scattering.gaussian(0, False))
                null_fit_more = scitbx.math.gaussian.fit(
                    stols_more, entry.table_y[:stols_more.size()],
                    entry.table_sigmas[:stols_more.size()],
                    xray_scattering.gaussian(0, False))
            else:
                rrg_stols_more = rez_rez_grant.table_2_stol
                sel = rrg_stols_more <= cutoff + 1.e-6
                rrg_stols = rrg_stols_more.select(sel)
                null_fit = scitbx.math.gaussian.fit(
                    rrg_stols,
                    rez_rez_grant.table_2_o2minus[:rrg_stols.size()],
                    rez_rez_grant.table_2_sigmas[:rrg_stols.size()],
                    xray_scattering.gaussian(0, False))
                null_fit_more = scitbx.math.gaussian.fit(
                    rrg_stols_more,
                    rez_rez_grant.table_2_o2minus[:rrg_stols_more.size()],
                    rez_rez_grant.table_2_sigmas[:rrg_stols_more.size()],
                    xray_scattering.gaussian(0, False))
        if (zig_zag):
            results[wrk_lbl] = cctbx.eltbx.gaussian_fit.zig_zag_fits(
                label=wrk_lbl,
                null_fit=null_fit,
                null_fit_more=null_fit_more,
                params=params)
        elif (full_fits is not None):
            assert len(full_fits.all[wrk_lbl]) == 1
            results[wrk_lbl] = cctbx.eltbx.gaussian_fit.decremental_fits(
                label=wrk_lbl,
                null_fit=null_fit,
                full_fit=full_fits.all[wrk_lbl][0],
                params=params,
                plots_dir=plots_dir,
                verbose=verbose)
        elif (not six_term):
            results[wrk_lbl] = cctbx.eltbx.gaussian_fit.incremental_fits(
                label=wrk_lbl,
                null_fit=null_fit,
                params=params,
                plots_dir=plots_dir,
                verbose=verbose)
        else:
            best_min = scitbx.math.gaussian_fit.fit_with_golay_starts(
                label=wrk_lbl,
                null_fit=null_fit,
                null_fit_more=null_fit_more,
                params=params)
            g = best_min.final_gaussian_fit
            results[wrk_lbl] = [
                xray_scattering.fitted_gaussian(stol=g.table_x()[-1],
                                                gaussian_sum=g)
            ]
        sys.stdout.flush()
        pickle_file_name = "%s_fits.pickle" % identifier(wrk_lbl)
        easy_pickle.dump(pickle_file_name, results)
Beispiel #4
0
def run(file_name, args, cutoff, params,
        zig_zag=False, six_term=False, full_fits=None,
        plots_dir="itvc_fits_plots", verbose=0):
  tab = itvc_section61_io.read_table6111(file_name)
  chunk_n = 1
  chunk_i = 0
  if (len(args) > 0 and len(args[0].split(",")) == 2):
    chunk_n, chunk_i = [int(i) for i in args[0].split(",")]
    args = args[1:]
  if (not six_term and not zig_zag):
    if (not os.path.isdir(plots_dir)):
      print "No plots because target directory does not exist (mkdir %s)." % \
        plots_dir
      plots_dir = None
    if (chunk_n > 1):
      assert plots_dir is not None
  stols_more = cctbx.eltbx.gaussian_fit.international_tables_stols
  sel = stols_more <= cutoff + 1.e-6
  stols = stols_more.select(sel)
  i_chunk = 0
  for element in tab.elements + ["O2-", "SDS"]:
    if (len(args) > 0 and element not in args): continue
    flag = i_chunk % chunk_n == chunk_i
    i_chunk += 1
    if (not flag):
      continue
    results = {}
    results["fit_parameters"] = params
    if (element == "SDS"):
      wrk_lbl = element
      from cctbx.eltbx.development.hydrogen_plots import fit_input
      fi = fit_input()
      sel = fi.stols <= cutoff + 1.e-6
      null_fit = scitbx.math.gaussian.fit(
        fi.stols.select(sel),
        fi.data.select(sel),
        fi.sigmas.select(sel),
        xray_scattering.gaussian(0, False))
      null_fit_more = scitbx.math.gaussian.fit(
        fi.stols,
        fi.data,
        fi.sigmas,
        xray_scattering.gaussian(0, False))
    else:
      wrk_lbl = xray_scattering.wk1995(element, True)
      if (element != "O2-"):
        entry = tab.entries[element]
        null_fit = scitbx.math.gaussian.fit(
          stols,
          entry.table_y[:stols.size()],
          entry.table_sigmas[:stols.size()],
          xray_scattering.gaussian(0, False))
        null_fit_more = scitbx.math.gaussian.fit(
          stols_more,
          entry.table_y[:stols_more.size()],
          entry.table_sigmas[:stols_more.size()],
          xray_scattering.gaussian(0, False))
      else:
        rrg_stols_more = rez_rez_grant.table_2_stol
        sel = rrg_stols_more <= cutoff + 1.e-6
        rrg_stols = rrg_stols_more.select(sel)
        null_fit = scitbx.math.gaussian.fit(
          rrg_stols,
          rez_rez_grant.table_2_o2minus[:rrg_stols.size()],
          rez_rez_grant.table_2_sigmas[:rrg_stols.size()],
          xray_scattering.gaussian(0, False))
        null_fit_more = scitbx.math.gaussian.fit(
          rrg_stols_more,
          rez_rez_grant.table_2_o2minus[:rrg_stols_more.size()],
          rez_rez_grant.table_2_sigmas[:rrg_stols_more.size()],
          xray_scattering.gaussian(0, False))
    if (zig_zag):
      results[wrk_lbl] = cctbx.eltbx.gaussian_fit.zig_zag_fits(
        label=wrk_lbl,
        null_fit=null_fit,
        null_fit_more=null_fit_more,
        params=params)
    elif (full_fits is not None):
      assert len(full_fits.all[wrk_lbl]) == 1
      results[wrk_lbl] = cctbx.eltbx.gaussian_fit.decremental_fits(
        label=wrk_lbl,
        null_fit=null_fit,
        full_fit=full_fits.all[wrk_lbl][0],
        params=params,
        plots_dir=plots_dir,
        verbose=verbose)
    elif (not six_term):
      results[wrk_lbl] = cctbx.eltbx.gaussian_fit.incremental_fits(
        label=wrk_lbl,
        null_fit=null_fit,
        params=params,
        plots_dir=plots_dir,
        verbose=verbose)
    else:
      best_min = scitbx.math.gaussian_fit.fit_with_golay_starts(
        label=wrk_lbl,
        null_fit=null_fit,
        null_fit_more=null_fit_more,
        params=params)
      g = best_min.final_gaussian_fit
      results[wrk_lbl] = [xray_scattering.fitted_gaussian(
        stol=g.table_x()[-1], gaussian_sum=g)]
    sys.stdout.flush()
    pickle_file_name = "%s_fits.pickle" % identifier(wrk_lbl)
    easy_pickle.dump(pickle_file_name, results)