Skip to content

Commit

Permalink
value code
Browse files Browse the repository at this point in the history
  • Loading branch information
MundVetter committed Mar 29, 2023
1 parent 2a03fa1 commit 2eb4f1b
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Experiments/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import numpy as np
import matplotlib.pyplot as plt

# Define parameters for grid creation
s = 0.1 # size of grid areas
n = 50 # minimum number of points in each area
o = 5 # minimum number of extra points on each side of an area

# Generate some example data for visualization
x = np.random.normal(loc=0.5, scale=0.3, size=1000)
y = np.random.normal(loc=-0.5, scale=0.2, size=1000)
data = np.vstack((x, y)).T

# Create grid areas
x_min, x_max = np.min(x), np.max(x)
y_min, y_max = np.min(y), np.max(y)

x_edges = np.arange(x_min, x_max + s, s)
y_edges = np.arange(y_min, y_max + s, s)

for i in range(1, len(x_edges) - 1):
for j in range(1, len(y_edges) - 1):
x1, x2 = x_edges[i - 1], x_edges[i]
y1, y2 = y_edges[j - 1], y_edges[j]

area = data[(x1 <= data[:, 0]) & (data[:, 0] <= x2) &
(y1 <= data[:, 1]) & (data[:, 1] <= y2)]

if len(area) < n:
# merge with neighbouring areas until minimum number of points is reached
while len(area) < n and i > 1:
i -= 1
x1 = x_edges[i - 1]
area = data[(x1 <= data[:, 0]) & (data[:, 0] <= x2) &
(y1 <= data[:, 1]) & (data[:, 1] <= y2)]
while len(area) < n and i < len(x_edges) - 2:
i += 1
x2 = x_edges[i]
area = data[(x1 <= data[:, 0]) & (data[:, 0] <= x2) &
(y1 <= data[:, 1]) & (data[:, 1] <= y2)]

# increase area size until minimum number of extra points on each side is reached
while (x1 > x_min and np.sum((x1 - s <= data[:, 0]) & (data[:, 0] <= x1)) < o) or \
(x2 < x_max and np.sum((x2 <= data[:, 0]) & (data[:, 0] <= x2 + s)) < o) or \
(y1 > y_min and np.sum((y1 - s <= data[:, 1]) & (data[:, 1] <= y1)) < o) or \
(y2 < y_max and np.sum((y2 <= data[:, 1]) & (data[:, 1] <= y2 + s)) < o):
if x1 > x_min and np.sum((x1 - s <= data[:, 0]) & (data[:, 0] <= x1)) < o:
x1 -= s
if x2 < x_max and np.sum((x2 <= data[:, 0]) & (data[:, 0] <= x2 + s)) < o:
x2 += s
if y1 > y_min and np.sum((y1 - s <= data[:, 1]) & (data[:, 1] <= y1)) < o:
y1 -= s
if y2 < y_max and np.sum((y2 <= data[:, 1]) & (data[:, 1] <= y2 + s)) < o:
y2 += s
area = data[(x1 <= data[:, 0]) & (data[:, 0] <= x2) &
(y1 <= data[:, 1]) & (data[:, 1] <= y2)]

# plot grid area and data points
plt.plot([x1, x2], [y1, y1], 'k-', lw=0.5)
plt.plot([x1, x2], [y2, y2], 'k-', lw=0.5)
plt.plot([x1, x1], [y1, y2], 'k-', lw=0.5)
plt.plot([x2, x2], [y1, y2], 'k-', lw=0.5)
plt.plot(area[:, 0], area[:, 1], 'bo', ms=2)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Overlapping grid visualization')
plt.show()
19 changes: 19 additions & 0 deletions fourier_cost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import numpy as np
def tune_progression(m, max, r = 1, epsilon = 0.01):
if m == 2:
return np.sqrt(max)
r_now = (max/r)**(1/(m-1))
if np.abs(r_now - r) < epsilon:
return r_now
else:
return tune_progression(m, max, r_now)

def calculate_m_cost(max, m_min = 3, m_max = 10):
r = []
for m in range(m_min, m_max):
r.append(tune_progression(m, max))
return np.array(r) * np.arange(m_min, m_max)

_max = 1000 * 5
print(calculate_m_cost(_max, 2, 13), np.argmin(calculate_m_cost(_max, 2, 13)) + 2)
print(tune_progression(9, 1000* 5))
Binary file added img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions img_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# read img.jpg and print the intensity of the first row of the image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('img.jpg')
# normalize between 0 and 1
img = img / 255

print(img[0, :, 0])

def img_sample(img, x, row):
""" x is a value between 0 and 1. return the nearest neighbour."""
return img[row, int(x * img.shape[1]), 0]
print(img_sample(img, 0.5, 0))
49 changes: 49 additions & 0 deletions overlapping_grid_2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import numpy as np

# def compute_overlapping_grid_2d(n, dim, o):
# s = o / n
# areas = n ** dim
# # create a array starting at [0, .., 0] and ending at [n, .., n] with length dim
# arr = np.arange(n)
# # create a meshgrid with the array
# mesh = np.meshgrid(*[arr] * dim)
# # create a list of all the points in the meshgrid
# points = np.vstack(map(np.ravel, mesh)).T
# return np.stack(points, points + s)

def compute_bounds(n, o):
s = o / n
r = np.arange(n) / n
return np.stack((r, r + s), axis=1)

def compute_value_region(x, region_boundaries):
""" Compute the value of the embedding"""
x = np.clip(x - region_boundaries[:, 0], a_min=0, a_max=region_boundaries[:, 1] - region_boundaries[:, 0])
x = x / (region_boundaries[:, 1] - region_boundaries[:, 0])
return x % 1

def compute_value(data, bounds):
x = compute_value_region(data[:, 0], bounds)
y = compute_value_region(data[:, 1], bounds)
x2 = np.concatenate([x, x > 0])
# repeat x2 for len(y) times
x2 = np.repeat(x2, len(y))

y2 = np.concatenate([y, y > 0])
# repeat y2 for len(x) times
y2 = np.repeat(y2, len(x))

return x2 * y2


x = np.array([[0.1, 0.5]])
bounds = compute_bounds(10, 1.1)
z = compute_value(x, bounds)
print(z)

# import matplotlib.pyplot as plt
# # random color per 4 points
# colors = np.random.rand(len(squares) // 4, 3)
# colors = np.repeat(colors, 4, axis=0)
# plt.scatter(squares[:, 0], squares[:, 1], c=colors, s=1000, alpha=0.5)
# plt.show()

0 comments on commit 2eb4f1b

Please sign in to comment.