The list_utility module

The following is a listing of the list_utility.py file on the LI-6800.

Listing 12‑5. Listing of /home/licor/resources/lib/list_utility.py.

Copy
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Thanks to Ari Kornfeld @ akorn@carnegiescience.edu

import numpy as np
from random import sample

def linearList(vv1, vv2, nn, rounded=2):
    # v1 - starting value,
    # v2 - ending value
    # n - number of values
    v1 = float(vv1)
    v2 = float(vv2)
    n = int(nn)
    v_range = [f for f in list(np.linspace(0, 1, num=n))]
    setpoints = [v1 + f*(v2-v1) for f in v_range]
    return list(np.around(setpoints, rounded))

def randomList(v1, v2, n, rounded=2):
    return sample(linearList(v1, v2, n, rounded=rounded),n)


def makeOrtho(listOfLists, lock_index=-1, max_cor=0.1, outfile=''):
    # listOfLists should contain 2 or more lists of setpoints. If sizes not equal, smallest size is n
    # returns listOfLists with setpoint sorted to provide maximum orthogonality (minimum correlation)
    # if you don't want one of the lists sorted, specify that index as lock_index. 0 = first list, 1 = 2nd, etc.
    listCount = len(listOfLists)
    n = np.min([len(x) for x in listOfLists]) # n is smallest list size 
    print(n)
    iter = 0
    if max_cor < 0.05:
        max_cor = 0.05
    orthogonal_enough = False
    p = lambda i: sample(listOfLists[i], n) if i != lock_index else listOfLists[i][0:n]
    while not orthogonal_enough:
        result = np.matrix([p(i) for i in range(listCount)])
        #to test bad ortho: data.frame(T=T_range, C=C_range, Q = Q_range)
        # now check that the variables aren't too strongly correlated 
        cor1 = np.corrcoef(result);
        np.fill_diagonal(cor1, 0.0) # we don't care about self correlation
#        print(cor1)
        cc = np.max(np.abs(cor1)) 
#        print("cc=", cc)
        orthogonal_enough = (cc < max_cor)
        iter += 1
        if (iter > 500):
            max_cor = 0.2 # safety valve
    print(iter, "iterations, cc=", cc)
    
    if outfile != '':
        try:
            file = open(outfile, 'w')
            print('corr_coeff=', cc, file=file)
            for i in range(n):
                line = ''
                for j in range(listCount):
                    line += str(result[j,:].tolist()[0][i])+' '
                print(line, file=file)
        except Exception as e:
            print('Exception in makeOrtho:', str(e))
            
    return  [result[i,:].tolist()[0] for i in range(listCount)] # return a list of lists
    

    
if __name__ == '__main__':
    n=12
    t = linearList(25, 15, 3) + linearList(17, 45, n-3) # make temp efficient to work through - no big jumps, and start at ambient.
    c = linearList(50, 1000, n, rounded=0) # test size mismatch
    q = linearList(20, 2000, n, rounded=0)
    (t,c,q) = makeOrtho((t,c,q), lock_index=0, outfile="/Users/jon/out.txt")
    print(t)
    print(c)
    print(q)