7 from CoolRunQuery.utils.AtlRunQueryLookup
import DQChannelDict
9 from warnings
import warn
12 warn(
"Failed to import DQChannelDict from CoolRunQuery. "
13 "Can't perform channel conversions.")
18 channel_mapping = DQChannelDict.copy()
19 channel_names = channel_mapping.keys()
21 cm_reversed = dict((value, key)
for key, value
in six.iteritems(channel_mapping))
22 channel_mapping.update(cm_reversed)
26 Given a channel, return a name detector or an integer (depending on want_id)
28 from builtins
import int
29 if isinstance(name, int):
30 if name
not in channel_mapping:
31 raise RuntimeError(
"ChannelID %r is not in channel_mapping" % name)
32 return name
if want_id
else channel_mapping[name]
34 elif isinstance(name, str):
35 if name
not in channel_mapping:
36 raise RuntimeError(
"ChannelID %r is not in channel_mapping" % name)
37 return name
if not want_id
else channel_mapping[name]
39 raise RuntimeError(
"I don't know how to convert %r into "
40 "a ChannelSelection" % name)
45 Given a list, return a channelselection.
47 Does the hard work of merging together adjacent channel numbers. Will also
48 convert channel names to numbers (if the folder is using DQ channels,
49 otherwise will give an incorrect result or fail to convert the strings to
52 from PyCool
import cool
53 if not list_
and as_list:
return []
54 if not list_:
return cool.ChannelSelection()
55 if set(map(type, list_)) !=
set([int])
and convert_channel:
56 list_ = map(convert_channel, list_)
62 for this, next
in zip(list_, list_[1:] + [-1]):
66 ranges.append((start, this))
72 assert len(ranges) < 50, (
73 "Cool has a 50 channel selection limit (%s selected)" % ranges)
75 selection = cool.ChannelSelection(*ranges.pop(0))
76 for start, end
in ranges:
77 selection.addRange(start, end)
83 Helper function which can convert a channel id, name, or list of either into
84 a cool.ChannelSelection. Includes protections for invalid channels.
86 from PyCool
import cool
88 mapping = channel_mapping
89 if cs
is None or cs == []:
90 return cool.ChannelSelection()
91 elif isinstance(cs, (int, str)):
93 return cool.ChannelSelection(channel_id, channel_id)
94 elif hasattr(cs,
"__iter__"):
97 elif isinstance(cs, cool.ChannelSelection):
99 raise RuntimeError(
"I don't know how to convert %r into a "
100 "ChannelSelection" % cs)
104 Given a `folder` name, retrieve a list of channel ids, names and return a
105 dictionary which will translate in either direction.
107 channel_ids =
list(folder.listChannels())
108 channel_names = folder.listChannelsWithNames()
109 channel_names =
list(map(channel_names.__getitem__, channel_ids))
110 channel_dict = dict(
list(zip(channel_ids, channel_names))
111 +
list(zip(channel_names, channel_ids)))
112 return channel_ids, channel_names, channel_dict