本文整理汇总了Python中gnome.utilities.orderedcollection.OrderedCollection.index方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedCollection.index方法的具体用法?Python OrderedCollection.index怎么用?Python OrderedCollection.index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gnome.utilities.orderedcollection.OrderedCollection
的用法示例。
在下文中一共展示了OrderedCollection.index方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_index
# 需要导入模块: from gnome.utilities.orderedcollection import OrderedCollection [as 别名]
# 或者: from gnome.utilities.orderedcollection.OrderedCollection import index [as 别名]
def test_index(self):
oc = OrderedCollection([1, 2, 3, 4, 5])
assert oc.index(id(3)) == 2
oc[id(3)] = 6
assert oc.index(id(6)) == 2
del oc[id(6)]
assert oc.index(id(4)) == 2
示例2: test_index
# 需要导入模块: from gnome.utilities.orderedcollection import OrderedCollection [as 别名]
# 或者: from gnome.utilities.orderedcollection.OrderedCollection import index [as 别名]
def test_index(self):
"behaves like index for a list"
oc = OrderedCollection([1, 2, 3, 4, 5])
assert oc.index(3) == 2
assert oc.index(s_id(3)) == 2
oc[s_id(3)] = 6
assert oc.index(6) == 2
assert oc.index(s_id(6)) == 2
del oc[s_id(6)]
assert oc.index(4) == 2
assert oc.index(s_id(4)) == 2
示例3: SpillContainer
# 需要导入模块: from gnome.utilities.orderedcollection import OrderedCollection [as 别名]
# 或者: from gnome.utilities.orderedcollection.OrderedCollection import index [as 别名]
class SpillContainer(SpillContainerData):
"""
Container class for all spills -- it takes care of capturing the released
LEs from all the spills, putting them all in a single set of arrays.
Many of the "fields" associated with a collection of elements are optional,
or used only by some movers, so only the ones required will be requested
by each mover.
The data for the elements is stored in the _data_arrays dict. They can be
accessed by indexing. For example:
positions = spill_container['positions'] : returns a (num_LEs, 3) array of
world_point_types
"""
def __init__(self, uncertain=False):
super(SpillContainer, self).__init__(uncertain=uncertain)
self.spills = OrderedCollection(dtype=gnome.spill.Spill)
self.rewind()
# don't want user to add to array_types in middle of run. Since its
# not possible to throw an error in this case, let's just make it a
# bit difficult to do.
# dict must be updated via prepar_for_model_run() only at beginning of
# run. Make self._array_types an an instance variable
self._reset_arrays()
def __setitem__(self, data_name, array):
"""
Invoke baseclass __setitem__ method so the _data_array is set correctly
In addition, create the appropriate ArrayType if it wasn't created by
the user.
"""
super(SpillContainer, self).__setitem__(data_name, array)
if data_name not in self._array_types:
shape = self._data_arrays[data_name].shape[1:]
dtype = self._data_arrays[data_name].dtype.type
self._array_types[data_name] = gnome.array_types.ArrayType(shape, dtype)
def _reset_arrays(self):
"""
reset _array_types dict so it contains default keys/values
"""
gnome.array_types.reset_to_defaults(["spill_num", "id"])
self._array_types = {
"positions": gnome.array_types.positions,
"next_positions": gnome.array_types.next_positions,
"last_water_positions": gnome.array_types.last_water_positions,
"status_codes": gnome.array_types.status_codes,
"spill_num": gnome.array_types.spill_num,
"id": gnome.array_types.id,
"mass": gnome.array_types.mass,
}
self._data_arrays = {}
@property
def array_types(self):
"""
user can modify ArrayType initial_value in middle of run. Changing
the shape should throw an error. Change the dtype at your own risk.
This returns a new dict so user cannot add/delete an ArrayType in
middle of run. Use prepare_for_model_run() to do add an ArrayType.
"""
return dict(self._array_types)
def rewind(self):
"""
In the rewind operation, we:
- rewind all the spills
- restore _array_types to contain only defaults
- movers/weatherers could have been deleted and we don't want to
carry associated data_arrays
- prepare_for_model_run() will be called before the next run and
new arrays can be given
- purge the data arrays
- we gather data arrays for each contained spill
- the stored arrays are cleared, then replaced with appropriate
empty arrays
"""
for spill in self.spills:
spill.rewind()
# create a full set of zero-sized arrays. If we rewound, something
# must have changed so let's get back to default _array_types
self._reset_arrays()
self.initialize_data_arrays()
def get_spill_mask(self, spill):
return self["spill_num"] == self.spills.index(spill.id)
def uncertain_copy(self):
"""
Returns a copy of the spill_container suitable for uncertainty
It has all the same spills, with the same ids, and the uncertain
flag set to True
"""
#.........这里部分代码省略.........
示例4: Model
# 需要导入模块: from gnome.utilities.orderedcollection import OrderedCollection [as 别名]
# 或者: from gnome.utilities.orderedcollection.OrderedCollection import index [as 别名]
#.........这里部分代码省略.........
(sc["positions"])[:] = sc["next_positions"]
def weather_elements(self):
"""
Weathers elements:
- loops through all the weatherers, passing in the spill_container
and the time range
- a weatherer modifies the data arrays in the spill container, so a
particular time range should not be run multiple times. It is
expected that we are processing a sequence of contiguous time ranges.
- Note: If there are multiple sequential weathering processes, some
inaccuracy could occur. A proposed solution is to
'super-sample' the model time step so that it will be replaced
with many smaller time steps. We'll have to see if this pans
out in practice.
"""
for sc in self.spills.items():
for w in self.weatherers:
for model_time, time_step in self._split_into_substeps():
w.weather_elements(sc, time_step, model_time)
def _split_into_substeps(self):
"""
:return: sequence of (datetime, timestep)
(Note: we divide evenly on second boundaries.
Thus, there will likely be a remainder
that needs to be included. We include
this remainder, which results in
1 more sub-step than we requested.)
"""
time_step = int(self._time_step)
sub_step = time_step / self.weathering_substeps
indexes = [idx for idx in range(0, time_step + 1, sub_step)]
res = [(idx, next_idx - idx) for idx, next_idx in zip(indexes, indexes[1:])]
if sum(res[-1]) < time_step:
# collect the remaining slice
res.append((sum(res[-1]), time_step % sub_step))
res = [(self.model_time + timedelta(seconds=idx), delta) for idx, delta in res]
return res
def step_is_done(self):
"""
Loop through movers and call model_step_is_done
"""
for mover in self.movers:
for sc in self.spills.items():
mover.model_step_is_done(sc)
for w in self.weatherers:
w.model_step_is_done()
for sc in self.spills.items():
"removes elements with oil_status.to_be_removed"
sc.model_step_is_done()
# age remaining particles
sc["age"][:] = sc["age"][:] + self.time_step
for outputter in self.outputters:
outputter.model_step_is_done()
def write_output(self):
示例5: SpillContainer
# 需要导入模块: from gnome.utilities.orderedcollection import OrderedCollection [as 别名]
# 或者: from gnome.utilities.orderedcollection.OrderedCollection import index [as 别名]
class SpillContainer(AddLogger, SpillContainerData):
"""
Container class for all spills -- it takes care of capturing the released
LEs from all the spills, putting them all in a single set of arrays.
Many of the "fields" associated with a collection of elements are optional,
or used only by some movers, so only the ones required will be requested
by each mover.
The data for the elements is stored in the _data_arrays dict. They can be
accessed by indexing. For example:
positions = spill_container['positions'] : returns a (num_LEs, 3) array of
world_point_types
"""
def __init__(self, uncertain=False):
super(SpillContainer, self).__init__(uncertain=uncertain)
self.spills = OrderedCollection(dtype=gnome.spill.Spill)
self.spills.register_callback(self._spills_changed,
('add', 'replace', 'remove'))
self.rewind()
def __setitem__(self, data_name, array):
"""
Invoke base class __setitem__ method so the _data_array is set
correctly. In addition, create the appropriate ArrayType if it wasn't
created by the user.
"""
super(SpillContainer, self).__setitem__(data_name, array)
if data_name not in self._array_types:
shape = self._data_arrays[data_name].shape[1:]
dtype = self._data_arrays[data_name].dtype.type
self._array_types[data_name] = ArrayType(shape, dtype,
name=data_name)
def _reset_arrays(self):
'''
reset _array_types dict so it contains default keys/values
'''
gnome.array_types.reset_to_defaults(['spill_num', 'id'])
self._array_types = {'positions': positions,
'next_positions': next_positions,
'last_water_positions': last_water_positions,
'status_codes': status_codes,
'spill_num': spill_num,
'id': id,
'mass': mass,
'age': age}
self._data_arrays = {}
def _reset__substances_spills(self):
'''
reset internal attributes to None and empty list []:
1. _substances_spills: data structure to contain spills per substance
2. _oil_comp_array_len: max number of psuedocomponents - relevant if
more than one substance is used.
3. _fate_data_list: list of FateDataView() objects. One object per
substance if substance is not None
'''
# Initialize following either the first time it is used or in
# prepare_for_model_run() -- it could change with each new spill
self._substances_spills = None
self._oil_comp_array_len = None
def _reset__fate_data_list(self):
# define the fate view of the data if 'fate_status' is in data arrays
# 'fate_status' is included if weathering is on
self._fate_data_list = []
def reset_fate_dataview(self):
'''
reset data arrays for each fate_dataviewer. Each substance that is not
None has a fate_dataviewer object.
'''
for viewer in self._fate_data_list:
viewer.reset()
def _set_substancespills(self):
'''
_substances could change when spills are added/deleted
using _spills_changed callback to reset self._substance_spills to None
If 'substance' is None, we still include it in this data structure -
all spills that are 'on' are included. A spill that is off isn't really
being modeled so ignore it.
.. note::
Should not be called in middle of run. prepare_for_model_run()
will invoke this if self._substance_spills is None. This is another
view of the data - it doesn't contain any state that needs to be
persisted.
'''
subs = []
spills = []
if self._oil_comp_array_len is None:
self._oil_comp_array_len = 1
#.........这里部分代码省略.........
示例6: Model
# 需要导入模块: from gnome.utilities.orderedcollection import OrderedCollection [as 别名]
# 或者: from gnome.utilities.orderedcollection.OrderedCollection import index [as 别名]
#.........这里部分代码省略.........
def weather_elements(self):
'''
Weathers elements:
- loops through all the weatherers, passing in the spill_container
and the time range
- a weatherer modifies the data arrays in the spill container, so a
particular time range should not be run multiple times. It is
expected that we are processing a sequence of contiguous time ranges.
- Note: If there are multiple sequential weathering processes, some
inaccuracy could occur. A proposed solution is to
'super-sample' the model time step so that it will be replaced
with many smaller time steps. We'll have to see if this pans
out in practice.
'''
for sc in self.spills.items():
for w in self.weatherers:
for model_time, time_step in self._split_into_substeps():
w.weather_elements(sc, time_step, model_time)
def _split_into_substeps(self):
'''
:return: sequence of (datetime, timestep)
(Note: we divide evenly on second boundaries.
Thus, there will likely be a remainder
that needs to be included. We include
this remainder, which results in
1 more sub-step than we requested.)
'''
time_step = int(self._time_step)
sub_step = time_step / self.weathering_substeps
indexes = [idx for idx in range(0, time_step + 1, sub_step)]
res = [(idx, next_idx - idx)
for idx, next_idx in zip(indexes, indexes[1:])]
if sum(res[-1]) < time_step:
# collect the remaining slice
res.append((sum(res[-1]), time_step % sub_step))
res = [(self.model_time + timedelta(seconds=idx), delta)
for idx, delta in res]
return res
def step_is_done(self):
'''
Loop through movers and call model_step_is_done
'''
for mover in self.movers:
for sc in self.spills.items():
mover.model_step_is_done(sc)
for w in self.weatherers:
w.model_step_is_done()
for sc in self.spills.items():
'removes elements with oil_status.to_be_removed'
sc.model_step_is_done()
# age remaining particles
sc['age'][:] = sc['age'][:] + self.time_step
for outputter in self.outputters:
outputter.model_step_is_done()