Python more_itertools.pairwise() Examples

The following are 12 code examples of more_itertools.pairwise(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module more_itertools , or try the search function .
Example #1
Source File: college_admission_util.py    From ml-fairness-gym with Apache License 2.0 6 votes vote down vote up
def realign_history(history):
  """"Realigns history so as to be compatible with auditors.

  Since the true applicants groups, unmanipulated test scores and true_eligible
  are generated before the agent's action, they are in the previous state, so we
  push them one step ahead in history and ignore the first step.

  Args:
    history: A list of tuples of state, action pairs.

  Returns:
    A realigned history with changed state, action pairs.
  """
  realign_variables = [
      'test_scores_x', 'applicant_groups', 'true_eligible', 'params'
  ]
  realigned_history = []
  for (state, _), (next_state,
                   next_action) in more_itertools.pairwise(history):
    new_history_point = core.HistoryItem(
        state=copy.deepcopy(next_state), action=copy.deepcopy(next_action))
    for variable in realign_variables:
      setattr(new_history_point.state, variable, getattr(state, variable))
    realigned_history.append(new_history_point)
  return realigned_history 
Example #2
Source File: test_recipes.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_base_case(self):
        """ensure an iterable will return pairwise"""
        p = mi.pairwise([1, 2, 3])
        self.assertEqual([(1, 2), (2, 3)], list(p)) 
Example #3
Source File: test_recipes.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_short_case(self):
        """ensure an empty iterator if there's not enough values to pair"""
        p = mi.pairwise("a")
        self.assertRaises(StopIteration, lambda: next(p)) 
Example #4
Source File: test_recipes.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_base_case(self):
        """ensure an iterable will return pairwise"""
        p = mi.pairwise([1, 2, 3])
        self.assertEqual([(1, 2), (2, 3)], list(p)) 
Example #5
Source File: test_recipes.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_short_case(self):
        """ensure an empty iterator if there's not enough values to pair"""
        p = mi.pairwise("a")
        self.assertRaises(StopIteration, lambda: next(p)) 
Example #6
Source File: model.py    From quantumsim with GNU General Public License v3.0 5 votes vote down vote up
def add_waiting_gates(self, circuit):
        """Insert missing waiting placeholders.

        Parameters
        ----------
        circuit : quantumsim.circuits.TimeAware

        Returns
        -------
        quantumsim.circuits.Circuit
        """
        gates_dict = defaultdict(list)
        for gate in circuit.gates:
            for qubit in gate.qubits:
                gates_dict[qubit].append(gate)
        time_start = circuit.time_start
        time_end = circuit.time_end
        margin = 1e-1
        waiting_gates = []

        for qubit, gates in gates_dict.items():
            duration = gates[0].time_start - time_start
            if duration > margin:
                waiting_gates.append(
                    self.waiting_gate(qubit, duration)
                        .shift(time_start=time_start))
            duration = time_end - gates[-1].time_end
            if duration > margin:
                waiting_gates.append(
                    self.waiting_gate(qubit, duration)
                        .shift(time_end=time_end))
            for gate1, gate2 in pairwise(gates):
                duration = gate2.time_start - gate1.time_end
                if duration > margin:
                    waiting_gates.append(self.waiting_gate(qubit, duration)
                                         .shift(time_start=gate1.time_end))
        gates = sorted(circuit.gates + waiting_gates,
                       key=lambda g: g.time_start)
        return Circuit(circuit.qubits, gates) 
Example #7
Source File: test_recipes.py    From pipenv with MIT License 5 votes vote down vote up
def test_base_case(self):
        """ensure an iterable will return pairwise"""
        p = mi.pairwise([1, 2, 3])
        self.assertEqual([(1, 2), (2, 3)], list(p)) 
Example #8
Source File: test_recipes.py    From pipenv with MIT License 5 votes vote down vote up
def test_short_case(self):
        """ensure an empty iterator if there's not enough values to pair"""
        p = mi.pairwise("a")
        self.assertRaises(StopIteration, lambda: next(p)) 
Example #9
Source File: core.py    From ml-fairness-gym with Apache License 2.0 5 votes vote down vote up
def _validate_history(self, history):
    """Checks that a history can be replayed using the metric's simulation.

    Args:
      history: an iterable of (state, action) pairs.

    Raises:
      ValueError if the metric's simulation and the history do not match.
    """
    history = copy.deepcopy(history)
    for idx, (step, next_step) in enumerate(more_itertools.pairwise(history)):
      simulated_state = self._simulate(step.state, step.action)
      if simulated_state != next_step.state:
        raise ValueError('Invalid history at step %d %s != %s' %
                         (idx, step, next_step)) 
Example #10
Source File: college_admission.py    From ml-fairness-gym with Apache License 2.0 5 votes vote down vote up
def realign_history(self, history):
    """"Realigns history so as to be compatible with auditors.

    Since the true applicants groups, unmanipulated test scores and
    true_eligible
    are generated before the agent's action, they are in the previous state, so
    we
    push them one step ahead in history and ignore the first step.

    Args:
      history: A list of tuples of state, action pairs.

    Returns:
      A realigned history with changed state, action pairs.
    """
    realign_variables = [
        'test_scores_x', 'applicant_groups', 'true_eligible', 'params'
    ]
    realigned_history = []
    for (state, _), (next_state,
                     next_action) in more_itertools.pairwise(history):
      new_history_point = core.HistoryItem(
          state=copy.deepcopy(next_state), action=copy.deepcopy(next_action))
      for variable in realign_variables:
        setattr(new_history_point.state, variable, getattr(state, variable))
      realigned_history.append(new_history_point)
    return realigned_history 
Example #11
Source File: test_recipes.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_base_case(self):
        """ensure an iterable will return pairwise"""
        p = mi.pairwise([1, 2, 3])
        self.assertEqual([(1, 2), (2, 3)], list(p)) 
Example #12
Source File: test_recipes.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_short_case(self):
        """ensure an empty iterator if there's not enough values to pair"""
        p = mi.pairwise("a")
        self.assertRaises(StopIteration, lambda: next(p))