import numpy as np
import matplotlib.pyplot as plt
import os
from cil.optimisation.algorithms import PDHG
from cil.optimisation.utilities import Sampler, SamplerRandom
import time
sampler=Sampler.staggered(21,5)
print(sampler.get_samples(22))
for _ in range(21):
sampler.next()
[ 0 5 10 15 20 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 0]
def test_function(iteration_number):
if iteration_number<500:
#print(iteration_number)
np.random.seed(iteration_number)
return(np.random.choice(49,1)[0])
else:
np.random.seed(iteration_number)
return(np.random.choice(50,1)[0])
sampler=Sampler.from_function(50, test_function)
for _ in range(11):
print(sampler.next())
print(list(sampler.get_samples(25)))
print(sampler)
44 37 40 42 46 35 10 47 3 28 9 [44, 37, 40, 42, 46, 35, 10, 47, 3, 28, 9, 25, 11, 18, 43, 8, 41, 47, 42, 29, 35, 9, 4, 19, 34] Sampler that selects from a list of indices {0, 1, …, S-1}, where S is the number of indices. Type : from_function Current iteration number : 11 Number of indices : 50 Probability weights : [0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02]
custom_list=[1,1,1,0,0,11,5,9,8,3]
num_indices=13
def test_function(iteration_number, custom_list=custom_list):
return(custom_list[iteration_number%len(custom_list)])
#calculate prob weights
temp_list = []
for i in range(num_indices):
temp_list.append(custom_list.count(i))
total = sum(temp_list)
prob_weights = [x/total for x in temp_list]
sampler=Sampler.from_function(num_indices=num_indices, function=test_function, prob_weights=prob_weights)
for _ in range(11):
print(sampler.next())
print(list(sampler.get_samples(25)))
print(sampler)
1 1 1 0 0 11 5 9 8 3 1 [1, 1, 1, 0, 0, 11, 5, 9, 8, 3, 1, 1, 1, 0, 0, 11, 5, 9, 8, 3, 1, 1, 1, 0, 0] Sampler that selects from a list of indices {0, 1, …, S-1}, where S is the number of indices. Type : from_function Current iteration number : 11 Number of indices : 13 Probability weights : [0.2, 0.3, 0.0, 0.1, 0.0, 0.1, 0.0, 0.0, 0.1, 0.1, 0.0, 0.1, 0.0]
sampler=Sampler.sequential(10)
print(sampler.get_samples(20))
for _ in range(11):
print(sampler.next())
next(sampler)
np.array([0 ,1 ,2 ,3, 4, 5, 6 ,7, 8, 9, 0 ,1,2 ,3 ,4, 5, 6 ,7, 8, 9])
print(sampler)
[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9] 0 1 2 3 4 5 6 7 8 9 0 Sampler that selects from a list of indices {0, 1, …, S-1}, where S is the number of indices. Type : sequential Current iteration number : 12 Number of indices : 10 Probability weights : [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
sampler=Sampler.random_without_replacement(7, seed=1)
print(list(sampler.get_samples(25)))
[2, 5, 0, 1, 4, 3, 6, 1, 6, 0, 4, 2, 3, 5, 5, 6, 2, 4, 0, 1, 3, 0, 2, 6, 3]
sampler=Sampler.herman_meyer(16)
print(sampler.get_samples(16))
for _ in range(15):
print(next(sampler))
print(sampler)
[ 0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15] 0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 Sampler that selects from a list of indices {0, 1, …, S-1}, where S is the number of indices. Type : herman_meyer Current iteration number : 15 Number of indices : 16 Probability weights : [0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625]
sampler=Sampler.random_with_replacement(5, seed=5)
for _ in range(30):
print(sampler.next())
print(list(sampler.get_samples(25)))
print(sampler)
1 4 1 4 2 3 3 2 1 0 0 3 2 0 4 1 2 1 3 2 2 1 1 1 1 0 0 4 4 0 [1, 4, 1, 4, 2, 3, 3, 2, 1, 0, 0, 3, 2, 0, 4, 1, 2, 1, 3, 2, 2, 1, 1, 1, 1] Sampler that selects from a list of indices {0, 1, …, S-1}, where S is the number of indices. Type : random_with_replacement Current iteration number : 30 Number of indices : 5 Probability weights : [0.2, 0.2, 0.2, 0.2, 0.2] Seed : 5
sampler=Sampler.random_with_replacement(4, [0.7,0.1,0.1,0.1], seed=5)
print(list(sampler.get_samples(25)))
print(sampler)
[0, 2, 0, 3, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Sampler that selects from a list of indices {0, 1, …, S-1}, where S is the number of indices. Type : random_with_replacement Current iteration number : 0 Number of indices : 4 Probability weights : [0.7, 0.1, 0.1, 0.1] Seed : 5
sampler=Sampler.staggered(21,4)
print(sampler.get_samples(5))
for _ in range(15):
print(sampler.next())
print(sampler.get_samples(5))
print(sampler)
[ 0 4 8 12 16] 0 4 8 12 16 20 1 5 9 13 17 2 6 10 14 [ 0 4 8 12 16] Sampler that selects from a list of indices {0, 1, …, S-1}, where S is the number of indices. Type : staggered Current iteration number : 15 Number of indices : 21 Probability weights : [0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616, 0.047619047619047616]
#Custom sampler example
class DelayedRegularisationSampler(SamplerRandom):
def function(self, iteration_number):
""" For each iteration number this function samples from a randomly generated list in order. Every num_indices the list is re-created. For the first aproximately 20*(num_indices -1) iterations the last index is never called. """
if iteration_number < 20*(self.num_indices - 1):
location = iteration_number % (self.num_indices - 1)
if location == 0:
self._sampling_list = self._generator.choice(self.num_indices-1, self.num_indices - 1, p=[
1/(self.num_indices-1)]*(self.num_indices-1), replace=self.replace)
else:
location = iteration_number % self.num_indices
if location == 0:
self._sampling_list = self._generator.choice(
self.num_indices, self.num_indices, p=self.prob_weights, replace=self.replace)
out = self._sampling_list[location]
return out
def __init__(self, num_indices, seed=None, replace=False, prob=None, sampling_type='Mantid Sampler'):
super(DelayedRegularisationSampler, self).__init__(
num_indices, seed, replace, prob, sampling_type)
sampler=DelayedRegularisationSampler(5)
print(sampler.get_samples(120))
[0 1 3 2 1 3 2 0 2 3 0 1 0 3 1 2 1 0 2 3 0 1 3 2 2 3 0 1 0 1 3 2 3 1 2 0 2 3 1 0 0 3 1 2 1 3 2 0 2 3 0 1 2 0 3 1 2 0 3 1 1 2 0 3 2 1 0 3 0 3 2 1 1 3 2 0 3 0 1 2 4 3 2 1 0 3 4 2 1 0 4 1 0 3 2 4 2 0 1 3 3 0 4 2 1 4 0 2 1 3 2 4 0 3 1 3 1 0 4 2]