本文整理汇总了Python中libtbx.containers.OrderedDict.keys方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict.keys方法的具体用法?Python OrderedDict.keys怎么用?Python OrderedDict.keys使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类libtbx.containers.OrderedDict
的用法示例。
在下文中一共展示了OrderedDict.keys方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: unique_beams
# 需要导入模块: from libtbx.containers import OrderedDict [as 别名]
# 或者: from libtbx.containers.OrderedDict import keys [as 别名]
def unique_beams(self):
''' Iterate through unique beams. '''
from dxtbx.imageset import ImageSweep
from libtbx.containers import OrderedDict
obj = OrderedDict()
for iset in self._imagesets:
if isinstance(iset, ImageSweep):
obj[iset.get_beam()] = None
else:
for i in range(len(iset)):
obj[iset.get_beam(i)] = None
return obj.keys()
示例2: image_data_cache
# 需要导入模块: from libtbx.containers import OrderedDict [as 别名]
# 或者: from libtbx.containers.OrderedDict import keys [as 别名]
class image_data_cache(object):
def __init__(self, imageset, size=10):
self.imageset = imageset
self.size = size
self._image_data = OrderedDict()
def __getitem__(self, i):
image_data = self._image_data.get(i)
if image_data is None:
image_data = self.imageset.get_raw_data(i)
if len(self._image_data) >= self.size:
# remove the oldest entry in the cache
del self._image_data[self._image_data.keys()[0]]
self._image_data[i] = image_data
return image_data
示例3: _unique_detectors_dict
# 需要导入模块: from libtbx.containers import OrderedDict [as 别名]
# 或者: from libtbx.containers.OrderedDict import keys [as 别名]
def _unique_detectors_dict(self):
''' Returns an ordered dictionary of detector objects. '''
from dxtbx.imageset import ImageSweep
from libtbx.containers import OrderedDict
obj = OrderedDict()
for iset in self._imagesets:
if isinstance(iset, ImageSweep):
obj[iset.get_detector()] = None
else:
for i in range(len(iset)):
obj[iset.get_detector(i)] = None
detector_id = 0
for detector in obj.keys():
obj[detector] = detector_id
detector_id = detector_id + 1
return obj
示例4: unique_scans
# 需要导入模块: from libtbx.containers import OrderedDict [as 别名]
# 或者: from libtbx.containers.OrderedDict import keys [as 别名]
def unique_scans(self):
''' Iterate through unique scans. '''
from dxtbx.imageset import ImageSweep
from libtbx.containers import OrderedDict
obj = OrderedDict()
for iset in self._imagesets:
if isinstance(iset, ImageSweep):
obj[iset.get_scan()] = None
else:
for i in range(len(iset)):
try:
model = iset.get_scan(i)
if model is not None:
obj[model] = None
except Exception:
pass
return obj.keys()
示例5: loop
# 需要导入模块: from libtbx.containers import OrderedDict [as 别名]
# 或者: from libtbx.containers.OrderedDict import keys [as 别名]
class loop(DictMixin):
def __init__(self, header=None, data=None):
self._columns = OrderedDict()
self.keys_lower = {}
if header is not None:
for key in header:
self.setdefault(key, flex.std_string())
if data is not None:
# the number of data items must be an exact multiple of the number of headers
assert len(data) % len(header) == 0, "Wrong number of data items for loop"
n_rows = len(data)//len(header)
n_columns = len(header)
for i in range(n_rows):
self.add_row([data[i*n_columns+j] for j in range(n_columns)])
elif header is None and data is not None:
assert isinstance(data, dict) or isinstance(data, OrderedDict)
self.add_columns(data)
self.keys_lower = dict(
[(key.lower(), key) for key in self._columns.keys()])
def __setitem__(self, key, value):
if not re.match(tag_re, key):
raise Sorry("%s is not a valid data name" %key)
if len(self) > 0:
assert len(value) == self.size()
if not isinstance(value, flex.std_string):
for flex_numeric_type in (flex.int, flex.double):
if isinstance(value, flex_numeric_type):
value = value.as_string()
else:
try:
value = flex_numeric_type(value).as_string()
except TypeError:
continue
else:
break
if not isinstance(value, flex.std_string):
value = flex.std_string(value)
# value must be a mutable type
assert hasattr(value, '__setitem__')
self._columns[key] = value
self.keys_lower[key.lower()] = key
def __getitem__(self, key):
return self._columns[self.keys_lower[key.lower()]]
def __delitem__(self, key):
del self._columns[self.keys_lower[key.lower()]]
del self.keys_lower[key.lower()]
def keys(self):
return self._columns.keys()
def __repr__(self):
return repr(OrderedDict(self.iteritems()))
def name(self):
return common_substring(self.keys()).rstrip('_').rstrip('.')
def size(self):
size = 0
for column in self.values():
size = max(size, len(column))
return size
def n_rows(self):
size = 0
for column in self.values():
size = max(size, len(column))
return size
def n_columns(self):
return len(self.keys())
def add_row(self, row, default_value="?"):
if isinstance(row, dict):
for key in self:
if key in row:
self[key].append(str(row[key]))
else:
self[key].append(default_value)
else:
assert len(row) == len(self)
for i, key in enumerate(self):
self[key].append(str(row[i]))
def add_column(self, key, values):
if self.size() != 0:
assert len(values) == self.size()
self[key] = values
self.keys_lower[key.lower()] = key
def add_columns(self, columns):
assert isinstance(columns, dict) or isinstance(columns, OrderedDict)
for key, value in columns.iteritems():
self.add_column(key, value)
def update_column(self, key, values):
assert type(key)==type(""), "first argument is column key string"
if self.size() != 0:
#.........这里部分代码省略.........
示例6: cif
# 需要导入模块: from libtbx.containers import OrderedDict [as 别名]
# 或者: from libtbx.containers.OrderedDict import keys [as 别名]
class cif(DictMixin):
def __init__(self, blocks=None):
if blocks is not None:
self.blocks = OrderedDict(blocks)
else:
self.blocks = OrderedDict()
self.keys_lower = dict([(key.lower(), key) for key in self.blocks.keys()])
def __setitem__(self, key, value):
assert isinstance(value, block)
if not re.match(tag_re, '_'+key):
raise Sorry("%s is not a valid data block name" %key)
self.blocks[key] = value
self.keys_lower[key.lower()] = key
def get(self, key, default=None):
key_lower = self.keys_lower.get(key.lower())
if (key_lower is None):
return default
return self.blocks.get(key_lower, default)
def __getitem__(self, key):
result = self.get(key)
if (result is None):
raise KeyError('Unknown CIF data block name: "%s"' % key)
return result
def __delitem__(self, key):
del self.blocks[self.keys_lower[key.lower()]]
del self.keys_lower[key.lower()]
def keys(self):
return self.blocks.keys()
def __repr__(self):
return repr(OrderedDict(self.iteritems()))
def __copy__(self):
return cif(self.blocks.copy())
copy = __copy__
def __deepcopy__(self, memo):
return cif(copy.deepcopy(self.blocks, memo))
def deepcopy(self):
return copy.deepcopy(self)
def show(self, out=None, indent=" ", indent_row=None,
data_name_field_width=34,
loop_format_strings=None):
if out is None:
out = sys.stdout
for name, block in self.items():
print >> out, "data_%s" %name
block.show(
out=out, indent=indent, indent_row=indent_row,
data_name_field_width=data_name_field_width,
loop_format_strings=loop_format_strings)
def __str__(self):
s = StringIO()
self.show(out=s)
return s.getvalue()
def validate(self, dictionary, show_warnings=True, error_handler=None, out=None):
if out is None: out = sys.stdout
from iotbx.cif import validation
errors = {}
if error_handler is None:
error_handler = validation.ErrorHandler()
for key, block in self.blocks.iteritems():
error_handler = error_handler.__class__()
dictionary.set_error_handler(error_handler)
block.validate(dictionary)
errors.setdefault(key, error_handler)
if error_handler.error_count or error_handler.warning_count:
error_handler.show(show_warnings=show_warnings, out=out)
return error_handler
def sort(self, recursive=False, key=None, reverse=False):
self.blocks = OrderedDict(sorted(self.blocks.items(), key=key, reverse=reverse))
if recursive:
for b in self.blocks.values():
b.sort(recursive=recursive, reverse=reverse)
示例7: multi_crystal_analysis
# 需要导入模块: from libtbx.containers import OrderedDict [as 别名]
# 或者: from libtbx.containers.OrderedDict import keys [as 别名]
class multi_crystal_analysis(object):
def __init__(self, unmerged_intensities, batches_all, n_bins=20, d_min=None,
id_to_batches=None):
sel = unmerged_intensities.sigmas() > 0
unmerged_intensities = unmerged_intensities.select(sel)
batches_all = batches_all.select(sel)
unmerged_intensities.setup_binner(n_bins=n_bins)
unmerged_intensities.show_summary()
self.unmerged_intensities = unmerged_intensities
self.merged_intensities = unmerged_intensities.merge_equivalents().array()
separate = separate_unmerged(
unmerged_intensities, batches_all, id_to_batches=id_to_batches)
self.intensities = separate.intensities
self.batches = separate.batches
run_id_to_batch_id = separate.run_id_to_batch_id
self.individual_merged_intensities = OrderedDict()
for k in self.intensities.keys():
self.intensities[k] = self.intensities[k].resolution_filter(d_min=d_min)
self.batches[k] = self.batches[k].resolution_filter(d_min=d_min)
self.individual_merged_intensities[k] = self.intensities[k].merge_equivalents().array()
if run_id_to_batch_id is not None:
labels = run_id_to_batch_id.values()
else:
labels = None
racc = self.relative_anomalous_cc()
if racc is not None:
self.plot_relative_anomalous_cc(racc, labels=labels)
correlation_matrix, linkage_matrix = self.compute_correlation_coefficient_matrix()
self._cluster_dict = self.to_dict(correlation_matrix, linkage_matrix)
self.plot_cc_matrix(correlation_matrix, linkage_matrix, labels=labels)
self.write_output()
def to_dict(self, correlation_matrix, linkage_matrix):
from scipy.cluster import hierarchy
tree = hierarchy.to_tree(linkage_matrix, rd=False)
leaves_list = hierarchy.leaves_list(linkage_matrix)
d = {}
# http://w3facility.org/question/scipy-dendrogram-to-json-for-d3-js-tree-visualisation/
# https://gist.github.com/mdml/7537455
def add_node(node):
if node.is_leaf(): return
cluster_id = node.get_id() - len(linkage_matrix) - 1
row = linkage_matrix[cluster_id]
d[cluster_id+1] = {
'datasets': [i+1 for i in sorted(node.pre_order())],
'height': row[2],
}
# Recursively add the current node's children
if node.left: add_node(node.left)
if node.right: add_node(node.right)
add_node(tree)
return d
def relative_anomalous_cc(self):
if self.unmerged_intensities.anomalous_flag():
d_min = min([ma.d_min() for ma in self.intensities.values()])
racc = flex.double()
full_set_anom_diffs = self.merged_intensities.anomalous_differences()
for i_wedge in self.individual_merged_intensities.keys():
ma_i = self.individual_merged_intensities[i_wedge].resolution_filter(d_min=d_min)
anom_i = ma_i.anomalous_differences()
anom_cc = anom_i.correlation(full_set_anom_diffs, assert_is_similar_symmetry=False).coefficient()
racc.append(anom_cc)
return racc
def plot_relative_anomalous_cc(self, racc, labels=None):
perm = flex.sort_permutation(racc)
fig = pyplot.figure(dpi=1200, figsize=(16,12))
pyplot.bar(range(len(racc)), list(racc.select(perm)))
if labels is None:
labels = ["%.0f" %(j+1) for j in perm]
assert len(labels) == len(racc)
pyplot.xticks([i+0.5 for i in range(len(racc))], labels)
locs, labels = pyplot.xticks()
pyplot.setp(labels, rotation=70)
pyplot.xlabel("Dataset")
pyplot.ylabel("Relative anomalous correlation coefficient")
fig.savefig("racc.png")
def compute_correlation_coefficient_matrix(self):
from scipy.cluster import hierarchy
import scipy.spatial.distance as ssd
correlation_matrix = flex.double(
flex.grid(len(self.intensities), len(self.intensities)))
#.........这里部分代码省略.........
示例8: XInfo
# 需要导入模块: from libtbx.containers import OrderedDict [as 别名]
# 或者: from libtbx.containers.OrderedDict import keys [as 别名]
class XInfo(object):
'''A class to represent all of the input to the xia2dpa system, with
enough information to allow structure solution, as parsed from a
.xinfo file, an example of which is in the source code.'''
def __init__(self, xinfo_file, sweep_ids=None, sweep_ranges=None):
'''Initialise myself from an input .xinfo file.'''
# first initialise all of the data structures which will hold the
# information...
self._project = None
self._crystals = OrderedDict()
if sweep_ids is not None:
sweep_ids = [s.lower() for s in sweep_ids]
if sweep_ranges is not None:
assert sweep_ids is not None
assert len(sweep_ids) == len(sweep_ranges)
self._sweep_ids = sweep_ids
self._sweep_ranges = sweep_ranges
# read the contents of the xinfo file
self._parse_project(xinfo_file)
self._validate()
return
def get_output(self):
'''Generate a string representation of the project.'''
text = 'Project %s\n' % self._project
for crystal in self._crystals.keys():
text += 'Crystal %s\n' % crystal
text += '%s\n' % self._crystals[crystal].get_output()
# remove a trailing newline...
return text[:-1]
def get_project(self):
return self._project
def get_crystals(self):
return self._crystals
def _validate(self):
'''Validate the structure of this object, ensuring that
everything looks right... raise exception if I find something
wrong.'''
return True
def _parse_project(self, xinfo_file):
'''Parse & validate the contents of the .xinfo file. This parses the
project element (i.e. the whole thing..)'''
project_records = []
for r in open(xinfo_file, 'r').readlines():
record = r.strip()
if not record:
pass
elif record[0] == '!' or record[0] == '#':
pass
else :
# then it may contain something useful...
project_records.append(record)
# so now we have loaded the whole file into memory stripping
# out the crud... let's look for something useful
for i in range(len(project_records)):
record = project_records[i]
if 'BEGIN PROJECT' in record:
self._project = record.replace('BEGIN PROJECT', '').strip()
if 'END PROJECT' in record:
if not self._project == record.replace(
'END PROJECT', '').strip():
raise RuntimeError, 'error parsing END PROJECT record'
# next look for crystals
if 'BEGIN CRYSTAL ' in record:
crystal_records = [record]
while True:
i += 1
record = project_records[i]
crystal_records.append(record)
if 'END CRYSTAL ' in record:
break
self._parse_crystal(crystal_records)
# that's everything, because parse_crystal handles
# the rest...
return
#.........这里部分代码省略.........