0

I have data that is a list of positions from 0-20 with corresponding intensities, in increments of 0.1. They are discrete values and not a function. The values are (close to) symmetrical and there should be a center point around 10 where if you fold the plot on itself you would have overlapping points. I've done this so far by summing the difference between each pair of values equidistant from center. The data is currently in a dataframe.

This seems different than the question "how to minimize a function with discrete variable values in scipy" because there the objective was actually to find the minimum of a function but I don't have a function.

The issue is there are many options that might actually be the best center position (realistically, from ~9-11 in increments of 0.1) and I don't want to have to manually change the center value, but it's not a function so fmin from scipy.optimize returns values for the center which are not in 0.1 increments. My code so far that does it is:

#Position data has values from 0-20 in 0.1 increments
stepsize = 0.1
max_pos = 15 #max that center could be at
HAB_slice['Position Relative to Center'] = HAB_slice['Position'] - center_guess #Defines position relative to center
HAB_slice['Radial Position'] = np.abs(HAB_slice['Position Relative to Center'].values) #absolute value based on previous

possible_pos = np.linspace(0, max_pos, max_pos / stepsize+1) 

for i in range(0, len(possible_pos)): # Loop sums absolute values of difference between same absolute difference from zero. Smaller value is generally best value
    temp = HAB_slice[HAB_slice['Radial Position']==possible_pos[i]]
        if len(temp) == 2:
            center_sum += np.abs(temp['I'].diff().values[1])   

And then manually changing the value for center_guess until center_sum is the smallest that it gets, which is really tedious. Note 'I' values are basically the y values.

Can someone show me a method of automating this that does not require the minimizing thing to be a function so that it iterates through the actual values of 'Position' to find the one that yields the smallest center_sum?

ShellieJ
  • 35
  • 8
  • Possible duplicate of [how to minimize a function with discrete variable values in scipy](http://stackoverflow.com/questions/17864474/how-to-minimize-a-function-with-discrete-variable-values-in-scipy) – Stephen Rauch May 15 '17 at 21:24
  • The minimum modification that I can see working with your current code would be to wrap your loop into a function that returns the sum for a given offset, then wrap that in a [generator](https://docs.python.org/3.6/reference/expressions.html#grammar-token-generator_expression) or [comprehension](https://docs.python.org/3.6/tutorial/datastructures.html#list-comprehensions) that you can pass to the built-in [`min`](https://docs.python.org/3/library/functions.html#min) function. – Mad Physicist May 15 '17 at 21:31
  • Can you elaborate a little on how a generator or comprehension would help? I'm just not sure I understand their purpose. – ShellieJ May 16 '17 at 12:37

0 Answers0