Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numpy slicing problem

import numpy as np

a  = np.concatenate((np.linspace(10,200,25),np.geomspace(200,360,25)))
b  = np.concatenate((np.linspace(20,-180,25),np.linspace(-180,230,25)))

c = a[0]+(b[0])/np.pi
if c >= 360:
   c = 360
d= a[3]+(b[3])/np.pi
if d >= 360:
   d = 360
e = np.linspace(c,d,50)

f = open("new.txt","w")
for i in range(50):
   f.write("gmic someFilter params: "+str(a[i])+","+str(b[i])+","+str(e[i])+" -o somefiles%04d.file \n" % i)

Tryin to get it so that in all 50 outputs to the text file, the value of 'e' is: a + b / pi (or 360 if e >= 360).
First output seems ok i.e.: gmic someFilter params: 10.0,20.0,16.366197723675814 -o somefiles0000.file
but subsequent values are messed up, i.e. the last one: gmic someFilter params: 360.0,230.0,32.15845056908105 -o somefiles0049.file
.. the value of 'e' in this case should be about 82, not 32..
My slicing is probably the issue as I've no experience doing this, any help gratefully received.

'.. the value of 'e' in this case should be about 187, not 32..'
Strange way to use numpy. The whole point of numpy is do add vectors in one operation, so you can compute an intermediate vector that holds your c and d values using;
# The vector of the sums:
# The vector of the sum, clamped at 360:
# The vector of the sums, clamped and in radians

The strange thing in your code is that when you do
c = a[0]+(b[0])/np.pi

Only the b[0] part is divided by np.pi, which looks odd, and the parentheses around the (b[0]) don't serve any purpose. So I assume you meant:

c = (a[0]+b[0])/np.pi

Since you have values up to 360, I assume you are trying to cover a full circle so, you may want to divide by 2*np.pi if you really want a value in radians, because a full circle is 2×π

Then I don't really understand what you do with e (linear sample between two values with an interval of 4?)? And what happens when i>=7?
Pi is a red-herring here I'm afraid!! It's an arbitrary value, I could have divided by 5 or 10 or anythibg but chose pi for no particular reason...

Well, I managed to get somewhere with my code, thanks for your help..  I used np.clip in the end to help acheive the results I desired.

(Even though in the example given, no values of e are over 360, I checked with different array parameters and the code I used (below) clamps the result to 360 or below.)
Thanks again Ofnuts !!

import numpy as np

a  = np.concatenate((np.linspace(13.76,239.04,100),np.geomspace(239.04,360,100)))
b  = np.concatenate((np.linspace(20.16,-184.32,100),np.linspace(-184.32,229.68,100)))

e = np.clip((np.linspace(a[0]+b[0],a[-1]+b[-1],200)/np.pi),-360,360)

f = open("newl.txt","w")
for i in range(200):
   f.write("gmic v -99 rund.png mercator "+str(a[i])+","+str(b[i])+","+str(e[i])+",0,85 resize 1280,720 normalize 0,255 o merc_%04d.png \n" % i)

Forum Jump: