Exemplo n.º 1
0
def process_image(path):
    global align

    img = cv2.imread(path)
    img_n,img_m,_ = img.shape
    
    midline = [8, 27, 28, 29, 51, 57, 62, 66] # All midline points except for two points on the tip of the nose, which are left out
    
    # Rotate until we are within 0.5 dgerees of straight
    for i in range(5):
        bb = align.getLargestFaceBoundingBox(img)
        landmarks = align.findLandmarks(img,bb)
        
        # Find line of symmetry and rotate image accordingly
        invslope, invintercept, _, _, _ = orgr.orthoregress([ landmarks[i][1] for i in midline ], [ landmarks[i][0] for i in midline ])
        # Rotate
        if abs(invslope) < 0.0087: # Approximately 0.5 degrees
            # everything is great, no rotation required
            break
        else:
            img = imutils.rotate_bound(img, atan(invslope)*180/pi)
    
    bb = align.getLargestFaceBoundingBox(img)
    landmarks = align.findLandmarks(img,bb)
    #~ invslope, invintercept, _, _, _ = scipy.stats.linregress([ landmarks[i][1] for i in midline ], [ landmarks[i][0] for i in midline ])
    invslope, invintercept, _, _, _ = orgr.orthoregress([ landmarks[i][1] for i in midline ], [ landmarks[i][0] for i in midline ])
    assert (abs(atan(invslope)*180/pi) < 0.5)
    
    # Crop the face part
    ys = [ p[1] for p in landmarks ]
    xs = [ p[0] for p in landmarks ]
    miny = min(ys)
    maxy = max(ys)
    minx = min(xs)
    maxx = max(xs)
    # Make sure axis of symmetry is in the middle of the image
    minx = int(floor(min(minx, 2*invintercept-maxx)))
    maxx = int(ceil(max(maxx, 2*invintercept-minx)))
    # Do the actual cropping
    img = img[miny:maxy, minx:maxx]
    # Scale image to width 500
    scale_factor = 500./(maxx-minx)
    img = cv2.resize(img, None, fx = scale_factor, fy = scale_factor, interpolation = cv2.INTER_CUBIC)
    
    # Re-adjust landmarks
    for i in range(len(landmarks)):
        x,y = landmarks[i]
        landmarks[i] = int(round((x-minx)*scale_factor)), int(round((y-miny)*scale_factor))
    
    if DISPLAY_IMAGE:
        # Mark face landmarks on image
        for i,point in enumerate(landmarks):
            #~ if i not in midline:
                #~ continue
            cv2.circle(img,point,2,(255,0,0),-1)
        
        # Draw line of symmetry on the face
        img_n,img_m,_ = img.shape
        cv2.line(img,(img_m/2,0),(img_m/2,img_n),(255,0,0),1)
    
    return landmarks, img
Exemplo n.º 2
0
def main():
    # This function reformats Bart's file

    # hubble constant used throughout
    hubbleC = 71

    # open the files
    if getpass.getuser() == 'frenchd':
        filename = '/usr/data/moosejaw/frenchd/GT_update2/returnPA11111111111111.csv'
    else:
        print 'Could not determine username. Exiting.'
        sys.exit()

    diameterFile = open(filename, 'rU')

    # read in the csv files as dictionary csv files
    reader = csv.DictReader(diameterFile)

    count = 0
    dict = {}

    #     surveys = [\
    #     'r (SDSS Petrosian)',\
    #     'r (SDSS Exponential)',\
    #     'r (SDSS Isophotal)',\
    #     'K_s (2MASS isophotal)',\
    #     'K_s (2MASS "total")',\
    #     'ESO-LV "Quick Blue" IIa-O',\
    #     'POSS1 103a-O',\
    #     'r (SDSS deVaucouleurs)',\
    #     'r (SDSS de Vaucouleurs)',\
    #     'RC3 D_25, R_25 (blue)',\
    #     'RC3 D_0 (blue)',\
    #     'ESO-Uppsala "Quick Blue" IIa-O',\
    #     'ESO-LV IIIa-F',\
    #     'RC3 A_e (Johnson B)',\
    #     'POSS1 103a-E',\
    #     'K_s (LGA/2MASS isophotal)',\
    #     'K_s (LGA/2MASS "total")',\
    #     'B (Johnson)',\
    #     'R (Kron-Cousins)',\
    #     '408 & 1407 MHz',\
    #     '1407 MHz']

    # survey 2 is always the same, we're comparing survey1 to 'K_s (2MASS "total")' each time
    survey2 = 'K_s (2MASS "total")'
    #     survey2 = 'r (SDSS Petrosian)'

    s2List = []

    #     survey1 = 'ESO-LV "Quick Blue" IIa-O'
    #     survey1 = 'r (SDSS Petrosian)'
    #     survey1 = 'r (SDSS Exponential)'
    #     survey1 = 'r (SDSS Isophotal)'
    #     survey1 = 'K_s (2MASS isophotal)'
    #     survey1 = 'K_s (2MASS "total")'
    #     survey1 = 'POSS1 103a-O'
    #     survey1 = 'r (SDSS deVaucouleurs)'
    #     survey1 = 'r (SDSS de Vaucouleurs)'
    #     survey1 = 'RC3 D_25, R_25 (blue)'
    #     survey1 = 'RC3 D_0 (blue)'
    #     survey1 = 'ESO-Uppsala "Quick Blue" IIa-O'
    #     survey1 = 'ESO-Uppsala "Quick Blue" IIa-O' - none w/ r Petro
    #     survey1 = 'ESO-LV IIIa-F' - none w/ r Petro
    #     survey1 = 'ESO-LV IIIa-F'
    #     survey1 = 'RC3 A_e (Johnson B)'
    #     survey1 = 'POSS1 103a-E' - none w/ r Petro
    #     survey1 = 'POSS1 103a-E'
    #     survey1 = 'K_s (LGA/2MASS isophotal)' - only 1 w/ K total
    #     survey1 = 'K_s (LGA/2MASS "total")' - only 1 w/ K total
    #     survey1 = 'B (Johnson)'
    survey1 = 'R (Kron-Cousins)'
    #     survey1 = '408 & 1407 MHz' - none w/ r Petro
    #     survey1 = '408 & 1407 MHz' - none w/ K total
    #     survey1 = '1407 MHz' - only 1 w/ r Petro
    #     survey1 = '1407 MHz' - only 1 w/ K total

    s1List = []

    # true for major axis comparison, false for minor axis comparison
    major = True
    dif = 10000
    cutoff = 2000000
    for line in reader:
        count += 1
        if count < 455000000:
            oldName = line['oldName']
            oldCompleteList = eval(line['completeList'])

            # switch major and minor if they are mixed up.
            completeList = []
            if len(str(oldCompleteList)) > 5:
                #                 print 'completeList: ',oldCompleteList
                for m in oldCompleteList:
                    diameters = m[1]
                    ratio = m[2]
                    maj, minor = diameters[0], diameters[1]

                    if isNumber(maj):
                        if float(maj) < cutoff:
                            # switch them if the minor axis is larger than the major
                            if isNumber(maj) and isNumber(minor):
                                if maj < minor:
                                    minor = float(maj) * float(ratio)

                            if isNumber(minor) and not isNumber(maj):
                                maj, minor = minor, maj

                            completeList.append(
                                [m[0], (maj, minor), m[2], m[3]])

            if len(completeList)>1 and str(completeList).find(survey1)!=-1 and\
            str(completeList).find(survey2)!=-1:

                # first choose only one of the measurements from each telescope if there
                # are multiple available (choose the largest)
                innerDict = {}
                for m in completeList:
                    survey = m[0]
                    diameters = m[1]
                    maj, minor = diameters[0], diameters[1]

                    if major:
                        axis = maj
                    else:
                        axis = minor

                    # check if the survey has been added previously
                    if isNumber(axis):
                        if float(axis) > 0:
                            if innerDict.has_key(survey):
                                i = innerDict[survey]
                                prevMaj, prevMin = i[1][0], i[1][1]

                                if major:
                                    prevAxis = prevMaj
                                else:
                                    prevAxis = prevMin

                                if isNumber(prevAxis) and isNumber(axis):
                                    if prevAxis < axis:
                                        innerDict[survey] = m

                            else:
                                innerDict[survey] = m

                allSurveys = innerDict.keys()
                uniqueList = []
                for survey in allSurveys:
                    m = innerDict[survey]
                    uniqueList.append(m)

#                 print 'uniqueList: ',uniqueList

                len1before = len(s1List)
                len2before = len(s2List)
                allList = []
                for m in uniqueList:
                    if major:
                        allList.append(m[1][0])
                    else:
                        allList.append(m[1][1])

#                 print 'allList: ',allList
                med = median(allList)
                val1 = 'x'
                val2 = 'x'
                avg = average(allList)
                num = len(allList)
                for m in uniqueList:
                    survey = m[0]
                    diameters = m[1]
                    ratio = m[2]
                    pa = m[3]

                    if survey == survey1 and len(s1List) == len1before:
                        #                         if float(diameters[0]) > 53 and float(diameters[0]) <55:
                        #                             print 'Oldname: ',oldName
                        #                             print completeList
                        #                             print
                        if major:
                            if isOdd(diameters[0], med, avg, dif, num):
                                print 'odd: ', m, '  --  ', med
                                print 'for: ', oldName
                                print
                                break
                            else:
                                val1 = diameters[0]
                        else:
                            if isOdd(diameters[1], med, avg, dif, num):
                                print 'odd: ', m, '  --  ', med
                                print 'for: ', oldName
                                print
                                break
                            else:
                                val1 = diameters[1]

                    # add this to the list if it has not already been added
                    if survey == survey2 and len(s2List) == len2before:
                        if major:
                            if isOdd(diameters[0], med, avg, dif, num):
                                print 'odd: ', m, '  --  ', med
                                print 'for: ', oldName
                                print
                                break
                            else:
                                val2 = diameters[0]
                        else:
                            if isOdd(diameters[1], med, avg, dif, num):
                                print 'odd: ', m, '  --  ', med
                                print 'for: ', oldName
                                print
                                break
                            else:
                                val2 = diameters[1]

                if isNumber(val1) and isNumber(val2):
                    s1List.append(val1)
                    s2List.append(val2)

    print 'len(1): ', len(s1List)
    print 'len(2): ', len(s2List)

    max1 = max(s1List)
    max2 = max(s2List)
    maxAll = max(max1, max2)
    print 'maxAll: ', maxAll

    ##########################################################################################

    xdata = s1List
    ydata = s2List

    ##########################################################################################

    def func(p, x):
        b, c = p
        return b * x + c

    # Run the regression
    xerr = np.array(xdata) * 0.05
    yerr = np.array(ydata) * 0.05

    out = orthoregress.orthoregress(xdata, ydata, xerr, yerr)
    m_lr, b_lr, r_value, p_value, std_err = linregress(s1List, s2List)

    print
    print 'out.beta: ', out.beta
    print 'out.sd_beta: ', out.sd_beta

    #print fit parameters and 1-sigma estimates
    popt = out.beta
    perr = out.sd_beta
    print
    print('fit parameter 1-sigma error')
    print
    print('---------')
    for i in range(len(popt)):
        print(str(popt[i]) + ' +- ' + str(perr[i]))

    # prepare confidence level curves
    # to draw 5-sigma intervals
    nstd = 5.
    popt_up5 = popt + nstd * perr
    popt_dw5 = popt - nstd * perr

    # and 1-sigma intervals
    popt_up1 = popt + perr
    popt_dw1 = popt - perr

    x_fit = np.linspace(min(xdata), max(xdata), 100)
    fit = func(popt, x_fit)
    fit_up5 = func(popt_up5, x_fit)
    fit_dw5 = func(popt_dw5, x_fit)

    fit_up1 = func(popt_up1, x_fit)
    fit_dw1 = func(popt_dw1, x_fit)

    # linregress result
    fit_lr = func([m_lr, b_lr], x_fit)

    ##########################################################################################

    # round stuff for nice formatting
    m_lr2 = round_to_sig(m_lr, sig=3)
    b_lr2 = round_to_sig(b_lr, sig=3)
    r_value2 = round_to_sig(r_value, sig=3)
    p_value2 = round_to_sig(p_value, sig=3)
    std_err2 = round_to_sig(std_err, sig=3)

    m_odr = round_to_sig(popt[0], sig=3)
    b_odr = round_to_sig(popt[1], sig=3)
    merr_odr = round_to_sig(perr[0], sig=3)
    berr_odr = round_to_sig(perr[1], sig=3)

    # plot the fit
    #     xp = np.linspace(0, max1, 100)
    #     ax.plot(xp, m2 * xp + b2, '-', label='1D fit: y = {0}*x + {1}'.format(m2,b2))
    #     ax.plot(xp, (m2 + std_err) * xp + b2, '-',color='red', label='1D fit: y = {0}*x + {1}'.format(m2,b2))
    #     ax.plot(xp, (m2 - std_err) * xp + b2, '-',color='red', label='1D fit: y = {0}*x + {1}'.format(m2,b2))

    fitLabel = '1D fit: y = {0}*x + {1}'.format(m_lr2, b_lr2)
    r_valueLabel = 'r_value = {0}'.format(r_value2)
    p_valueLabel = 'p_value = {0}'.format(p_value2)
    std_errLabel = 'std_err = {0}'.format(std_err2)

    #plot
    fig = figure(figsize=(7.7, 5.7))
    ax = fig.add_subplot(111)
    #     fig, ax = plt.subplots(1)
    fsize = 13
    rcParams['font.size'] = fsize
    errorbar(xdata,
             ydata,
             yerr=yerr,
             xerr=xerr,
             hold=True,
             ecolor='k',
             fmt='none',
             label='data')
    xlabel('{0} diameters'.format(survey1), fontsize=fsize)
    ylabel('{0} diameters'.format(survey2), fontsize=fsize)
    title('fit with error on both axis', fontsize=fsize)
    plot(x_fit, fit, 'r', lw=2, label='ODR = {0}*x + {1}'.format(m_odr, b_odr))
    plot(x_fit, fit_lr, color='green', lw=2, label='Linregress fit curve')
    ax.fill_between(x_fit,
                    fit_up1,
                    fit_dw1,
                    alpha=.45,
                    label='1-sigma: m+/- {0}, b+/- {1}'.format(
                        merr_odr, berr_odr))
    ax.fill_between(x_fit,
                    fit_up5,
                    fit_dw5,
                    alpha=.20,
                    label='5-sigma interval')

    legend(loc='upper left', fontsize=fsize)
    #     xlim(0,max(x))
    #     ylim(0,max(y))

    print
    print 'm_lr2: ', m_lr2
    print 'b_lr2: ', b_lr2
    print 'r_value2: ', r_value2
    print 'p_value2: ', p_value2
    print 'std_err2: ', std_err2
    print

    ax.set_ylim(0, max2 + max2 * 0.35)
    ax.set_xlim(0, max1 + max1 * 0.1)

    #     show()
    #     sys.exit()

    survey1 = survey1.replace('/', '_')
    survey2 = survey2.replace('/', '_')
    ans = 'y'
    if ans == 'y':
        directory = '/usr/data/moosejaw/frenchd/GT_update2/diameterFits_odr/'

        # write the figure
        fig.savefig('{0}/{1}-{2}_odrreg.pdf'.format(directory, survey1,
                                                    survey2),
                    format='pdf')

        # write the information file
        fit_result = open(
            '{0}/{1}-{2}_odrreg_fits.txt'.format(directory, survey1, survey2),
            'wt')

        fit_result.write('----Orthogonal Distance Regression results----\n')
        fit_result.write('[m,b] = {0}\n'.format(out.beta))
        fit_result.write('[m_err,b_err] = {0}\n'.format(out.sd_beta))
        fit_result.write('Covariance = {0}\n'.format(out.cov_beta))
        fit_result.write('Residual Variance = {0}\n'.format(out.res_var))
        fit_result.write('Relative Error = {0}\n'.format(out.rel_error))
        fit_result.write('Info = {0}\n'.format(out.info))
        fit_result.write('Stop Reason = {0}\n'.format(out.stopreason))
        fit_result.write('\n')
        fit_result.write('\n')
        fit_result.write('----Standard Linear Regression results----\n')
        fit_result.write('{0}\n'.format(fitLabel))
        fit_result.write('r_value = {0}\n'.format(r_valueLabel))
        fit_result.write('p_value = {0}\n'.format(p_valueLabel))
        fit_result.write('std_err = {0}\n'.format(std_err))

        fit_result.close()


#         fig.savefig('/usr/users/frenchd/Galaxy Table code/diameterFix/finalFits/ESO-LV_"Quick Blue"_IIa-O_vs_K_s(LGA_2MASS)_total.pdf',format='pdf')
    else:
        show()

    diameterFile.close()
Exemplo n.º 3
0
def main():
    # This function reformats Bart's file

    # hubble constant used throughout
    hubbleC = 71

    # open the files
    if getpass.getuser() == 'frenchd':
        filename = '/usr/data/moosejaw/frenchd/GT_update2/returnPA11111111111111.csv'
    else:
        print 'Could not determine username. Exiting.'
        sys.exit()

    diameterFile = open(filename, 'rU')

    # read in the csv files as dictionary csv files
    reader = csv.DictReader(diameterFile)

    count = 0
    dict = {}

    #     surveys = [\
    #     'r (SDSS Petrosian)',\
    #     'r (SDSS Exponential)',\
    #     'r (SDSS Isophotal)',\
    #     'K_s (2MASS isophotal)',\
    #     'K_s (2MASS "total")',\
    #     'ESO-LV "Quick Blue" IIa-O',\
    #     'POSS1 103a-O',\
    #     'r (SDSS deVaucouleurs)',\
    #     'r (SDSS de Vaucouleurs)',\
    #     'RC3 D_25, R_25 (blue)',\
    #     'RC3 D_0 (blue)',\
    #     'ESO-Uppsala "Quick Blue" IIa-O',\
    #     'ESO-LV IIIa-F',\
    #     'RC3 A_e (Johnson B)',\
    #     'POSS1 103a-E',\
    #     'K_s (LGA/2MASS isophotal)',\
    #     'K_s (LGA/2MASS "total")',\
    #     'B (Johnson)',\
    #     'R (Kron-Cousins)',\
    #     '408 & 1407 MHz',\
    #     '1407 MHz']

    # survey 2 is always the same, we're comparing survey1 to 'K_s (2MASS "total")' each time
    survey2 = 'K_s (2MASS "total")'
    #     survey2 = 'r (SDSS Petrosian)'

    s2List = []

    #     survey1 = 'ESO-LV "Quick Blue" IIa-O'
    #     survey1 = 'r (SDSS Petrosian)'
    #     survey1 = 'r (SDSS Exponential)'
    #     survey1 = 'r (SDSS Isophotal)'
    #     survey1 = 'K_s (2MASS isophotal)'
    #     survey1 = 'K_s (2MASS "total")'
    survey1 = 'ESO-LV "Quick Blue" IIa-O'
    #     survey1 = 'POSS1 103a-O'
    #     survey1 = 'r (SDSS deVaucouleurs)'
    #     survey1 = 'r (SDSS de Vaucouleurs)'
    #     survey1 = 'RC3 D_25, R_25 (blue)'
    #     survey1 = 'RC3 D_0 (blue)'
    #     survey1 = 'ESO-Uppsala "Quick Blue" IIa-O'
    #     survey1 = 'ESO-Uppsala "Quick Blue" IIa-O' - none w/ r Petro
    #     survey1 = 'ESO-LV IIIa-F' - none w/ r Petro
    #     survey1 = 'ESO-LV IIIa-F'
    #     survey1 = 'RC3 A_e (Johnson B)'
    #     survey1 = 'POSS1 103a-E' - none w/ r Petro
    #     survey1 = 'POSS1 103a-E'
    #     survey1 = 'K_s (LGA/2MASS isophotal)' - only 1 w/ K total
    #     survey1 = 'K_s (LGA/2MASS "total")' - only 1 w/ K total
    #     survey1 = 'B (Johnson)'
    #     survey1 = 'R (Kron-Cousins)'
    #     survey1 = '408 & 1407 MHz' - none w/ r Petro
    #     survey1 = '408 & 1407 MHz' - none w/ K total
    #     survey1 = '1407 MHz' - only 1 w/ r Petro
    #     survey1 = '1407 MHz' - only 1 w/ K total

    s1List = []

    # true for major axis comparison, false for minor axis comparison
    major = True
    dif = 10
    cutoff = 2000000
    for line in reader:
        count += 1
        if count < 455000000:
            oldName = line['oldName']
            oldCompleteList = eval(line['completeList'])

            # switch major and minor if they are mixed up.
            completeList = []
            if len(str(oldCompleteList)) > 5:
                #                 print 'completeList: ',oldCompleteList
                for m in oldCompleteList:
                    diameters = m[1]
                    ratio = m[2]
                    maj, minor = diameters[0], diameters[1]

                    if isNumber(maj):
                        if float(maj) < cutoff:
                            # switch them if the minor axis is larger than the major
                            if isNumber(maj) and isNumber(minor):
                                if maj < minor:
                                    minor = float(maj) * float(ratio)

                            if isNumber(minor) and not isNumber(maj):
                                maj, minor = minor, maj

                            completeList.append(
                                [m[0], (maj, minor), m[2], m[3]])

            if len(completeList)>1 and str(completeList).find(survey1)!=-1 and\
            str(completeList).find(survey2)!=-1:

                # first choose only one of the measurements from each telescope if there
                # are multiple available (choose the largest)
                innerDict = {}
                for m in completeList:
                    survey = m[0]
                    diameters = m[1]
                    maj, minor = diameters[0], diameters[1]

                    if major:
                        axis = maj
                    else:
                        axis = minor

                    # check if the survey has been added previously
                    if isNumber(axis):
                        if float(axis) > 0:
                            if innerDict.has_key(survey):
                                i = innerDict[survey]
                                prevMaj, prevMin = i[1][0], i[1][1]

                                if major:
                                    prevAxis = prevMaj
                                else:
                                    prevAxis = prevMin

                                if isNumber(prevAxis) and isNumber(axis):
                                    if prevAxis < axis:
                                        innerDict[survey] = m

                            else:
                                innerDict[survey] = m

                allSurveys = innerDict.keys()
                uniqueList = []
                for survey in allSurveys:
                    m = innerDict[survey]
                    uniqueList.append(m)

#                 print 'uniqueList: ',uniqueList

                len1before = len(s1List)
                len2before = len(s2List)
                allList = []
                for m in uniqueList:
                    if major:
                        allList.append(m[1][0])
                    else:
                        allList.append(m[1][1])

#                 print 'allList: ',allList
                med = median(allList)
                val1 = 'x'
                val2 = 'x'
                avg = average(allList)
                num = len(allList)
                for m in uniqueList:
                    survey = m[0]
                    diameters = m[1]
                    ratio = m[2]
                    pa = m[3]

                    if survey == survey1 and len(s1List) == len1before:
                        #                         if float(diameters[0]) > 53 and float(diameters[0]) <55:
                        #                             print 'Oldname: ',oldName
                        #                             print completeList
                        #                             print
                        if major:
                            if isOdd(diameters[0], med, avg, dif, num):
                                print 'odd: ', m, '  --  ', med
                                print 'for: ', oldName
                                print
                                break
                            else:
                                val1 = diameters[0]
                        else:
                            if isOdd(diameters[1], med, avg, dif, num):
                                print 'odd: ', m, '  --  ', med
                                print 'for: ', oldName
                                print
                                break
                            else:
                                val1 = diameters[1]

                    # add this to the list if it has not already been added
                    if survey == survey2 and len(s2List) == len2before:
                        if major:
                            if isOdd(diameters[0], med, avg, dif, num):
                                print 'odd: ', m, '  --  ', med
                                print 'for: ', oldName
                                print
                                break
                            else:
                                val2 = diameters[0]
                        else:
                            if isOdd(diameters[1], med, avg, dif, num):
                                print 'odd: ', m, '  --  ', med
                                print 'for: ', oldName
                                print
                                break
                            else:
                                val2 = diameters[1]

                if isNumber(val1) and isNumber(val2):
                    s1List.append(val1)
                    s2List.append(val2)

    print 'len(1): ', len(s1List)
    print 'len(2): ', len(s2List)

    max1 = max(s1List)
    max2 = max(s2List)
    maxAll = max(max1, max2)
    print 'maxAll: ', maxAll

    fig = figure()
    ax = fig.add_subplot(111)

    # do the fit
    #     f = np.polyfit(s1List, s2List,1,full=True)
    #     fit, residuals, rank, singular_values, rcond = f
    #     print
    #     print 'fit: ',fit
    #     print 'residuals: ',residuals
    #     print 'rank: ',rank
    #     print 'singular_values: ',singular_values
    #     print 'rcond: ',rcond
    #     print

    #     z = np.poly1d(fit)
    #     xp = np.linspace(0, max1, 100)
    #     ax.plot(xp,z(xp),'-',label='1D fit: y = {0}*x + {1}'.format(str(round(fit[0],3)),str(round(fit[1],3))))

    ##########################################################################################

    #     m, b, r_value, p_value, std_err = linregress(s1List, s2List)

    xdata = s1List
    ydata = s2List

    #     pfit, pcov = optimize.curve_fit(lineFunc, xdata, ydata)

    ##########################################################################################

    # These are initial guesses for fits:
    pstart = [1.0, 0.0]

    # bootstrap method

    #     def ff(x, p):
    #         return lineFunc(x, *p)

    # These are the true parameters
    #     p0 = 1.0
    #     p1 = 40
    #     p2 = 2.0

    # These are initial guesses for fits:
    #     pstart = [
    #         p0 + random.random(),
    #         p1 + 5.*random.random(),
    #         p2 + random.random()
    #     ]

    #     %matplotlib inline
    #     import matplotlib.pyplot as plt
    #     xvals = np.linspace(0., 1, 120)
    #     yvals = f(xvals, p0, p1, p2)

    # Generate data with a bit of randomness
    #     xdata = np.array(xvals)
    #     np.random.seed(42)
    #     err_stdev = 0.2
    #     yvals_err =  np.random.normal(0., err_stdev, len(xdata))
    #     ydata = f(xdata, p0, p1, p2) + yvals_err

    #     plt.plot(xvals, yvals)
    #     plt.plot(xdata, ydata, 'o', mfc='None')

    #     pfit, perr = fit_bootstrap(pstart, xdata, ydata, ff)
    #
    #     print 'pfit: ',pfit
    #     print
    #     print 'perr: ',perr

    ##########################################################################################

    #     pfit, perr = fit_curvefit(pstart, xdata, ydata, ff)
    #
    #     pfit, pcov = optimize.curve_fit(lineFunc,xdata,ydata,p0=pstart)
    #     error = []
    #     for i in range(len(pfit)):
    #         try:
    #           error.append(np.absolute(pcov[i][i])**0.5)
    #         except:
    #           error.append( 0.00 )
    #     pfit_curvefit = pfit
    #     perr_curvefit = np.array(error)
    #     print "curve_fit: ", pfit_curvefit, perr_curvefit
    #
    #     print("\nFit parameters and parameter errors from curve_fit method :")
    #     print("pfit = ", pfit)
    #     print("perr = ", perr)

    #     error = []
    #     for i in range(len(pfit)):
    #         try:
    #           error.append(absolute(pcov[i][i])**0.5)
    #         except:
    #           error.append( 0.00 )
    #     pfit_curvefit = pfit
    #     perr_curvefit = array(error)

    #     print("\nFit parameters and parameter errors from curve_fit method :")
    #     print("pfit = ", pfit_curvefit)
    #     print("perr = ", perr_curvefit)

    #     pfit_curvefit = pfit
    #     perr_curvefit = array(perr)
    #
    #     m = pfit_curvefit[0]
    #     b = pfit_curvefit[1]
    #
    #     std_err = perr_curvefit[0]
    #     r_value = 0
    #     p_value = 0

    ##########################################################################################

    #     def func(p, x):
    #         a, b, c = p
    #         return a*x*x + b*x + c

    def func(p, x):
        b, c = p
        return b * x + c

    # Model object
    quad_model = odr.Model(func)

    x = np.array(xdata)
    y = np.array(ydata)
    noise_x = x * 0.05
    noise_y = y * 0.05

    # Create a RealData object
    data = odr.RealData(x, y, sx=noise_x, sy=noise_y)
    #     data = odr.Data(x, y)

    # Set up ODR with the model and data
    #     odr2 = odr.ODR(data, quad_model, beta0=[0., 1., 1.])
    odr2 = odr.ODR(data, quad_model, beta0=[1., 1.])

    # Run the regression
    out = odr2.run()

    #print fit parameters and 1-sigma estimates
    popt = out.beta
    perr = out.sd_beta
    print
    print('fit parameter 1-sigma error')
    print
    print('---------')
    for i in range(len(popt)):
        print(str(popt[i]) + ' +- ' + str(perr[i]))

    # prepare confidence level curves
    # to draw 5-sigma intervals
    nstd = 5.
    popt_up = popt + nstd * perr
    popt_dw = popt - nstd * perr

    x_fit = np.linspace(min(x), max(x), 100)
    fit = func(popt, x_fit)
    fit_up = func(popt_up, x_fit)
    fit_dw = func(popt_dw, x_fit)

    #plot
    fig, ax = plt.subplots(1)
    #     rcParams['font.size']= 15
    #     errorbar(x, y, yerr=noise_y, xerr=noise_x, hold=True, ecolor='k', fmt='none', label='data')
    xlabel('{0} diameters'.format(survey1), fontsize=15)
    ylabel('{0} diameters'.format(survey2), fontsize=15)
    title('fit with error on both axis', fontsize=15)
    plot(x_fit, fit, 'r', lw=2, label='best fit curve')
    #     plot(x0, y0, 'k-', lw=2, label='True curve')
    #     ax.fill_between(x_fit, fit_up, fit_dw, alpha=.25, label='5-sigma interval')
    legend(loc='lower right', fontsize=15)
    xlim(0, max(x))
    ylim(0, max(y))
    show()

    ##########################################################################################

    # try orthoregress modules
    import orthoregress

    xerr = np.array(xdata) * 0.05
    yerr = np.array(ydata) * 0.05

    out = orthoregress.orthoregress(xdata, ydata, xerr, yerr)

    print
    print 'out.beta: ', out.beta
    print 'out.sd_beta: ', out.sd_beta
    print 'out.cov_beta: ', out.cov_beta
    print 'out.delta: ', out.delta
    print 'out.eps: ', out.eps
    print 'out.res_var: ', out.res_var
    print 'out.sum_sqare: ', out.sum_sqare
    print 'out.sum_square_delta: ', out.sum_square_delta
    print 'out.sum_square_eps: ', out.sum_square_eps
    print
    sys.exit()

    # round stuff for nice formatting
    m2 = round_to_sig(m, sig=3)
    b2 = round_to_sig(b, sig=3)
    r_value2 = round_to_sig(r_value, sig=3)
    p_value2 = round_to_sig(p_value, sig=3)
    std_err2 = round_to_sig(std_err, sig=3)

    # plot the fit
    xp = np.linspace(0, max1, 100)
    ax.plot(xp,
            m2 * xp + b2,
            '-',
            label='1D fit: y = {0}*x + {1}'.format(m2, b2))
    ax.plot(xp, (m2 + std_err) * xp + b2,
            '-',
            color='red',
            label='1D fit: y = {0}*x + {1}'.format(m2, b2))
    ax.plot(xp, (m2 - std_err) * xp + b2,
            '-',
            color='red',
            label='1D fit: y = {0}*x + {1}'.format(m2, b2))

    # plot the data
    ax.plot(s1List, s2List, '.', label='Data with dif={0}'.format(dif))

    print
    print 'm2: ', m2
    print 'b2: ', b2
    print 'r_value2: ', r_value2
    print 'p_value2: ', p_value2
    print 'std_err2: ', std_err2
    print

    xLabelPos = 0.6
    yLabelPos = 0.7
    yShift = 0.05
    lsize = 12
    fitLabel = '1D fit: y = {0}*x + {1}'.format(m2, b2)
    r_valueLabel = 'r_value = {0}'.format(r_value2)
    p_valueLabel = 'p_value = {0}'.format(p_value2)
    std_errLabel = 'std_err = {0}'.format(std_err2)

    ax.annotate(fitLabel,
                xy=(xLabelPos, yLabelPos),
                xycoords='axes fraction',
                size=lsize)
    ax.annotate(r_valueLabel,
                xy=(xLabelPos, yLabelPos - yShift),
                xycoords='axes fraction',
                size=lsize)
    ax.annotate(p_valueLabel,
                xy=(xLabelPos, yLabelPos - yShift - yShift),
                xycoords='axes fraction',
                size=lsize)
    ax.annotate(std_errLabel,
                xy=(xLabelPos, yLabelPos - yShift - yShift - yShift),
                xycoords='axes fraction',
                size=lsize)

    #     legend()

    ax.set_xlabel('{0} diameters'.format(survey1))
    ax.set_ylabel('{0} diameters'.format(survey2))
    ax.set_ylim(0, max2 + max2 * 0.3)
    ax.set_xlim(0, max1 + max1 * 0.3)

    #     ans = raw_input("Save (y/n)? ")
    #     while ans != 'y' and ans != 'n':
    #         ans = raw_input("Save (y/n)? ")

    survey1 = survey1.replace('/', '_')
    survey2 = survey2.replace('/', '_')
    ans = 'y'
    if ans == 'y':
        fig.savefig(
            '/usr/data/moosejaw/frenchd/GT_update2/diameterFits/{0}-{1}_dif{2}_linreg3.pdf'
            .format(survey1, survey2, dif),
            format='pdf')


#         fig.savefig('/usr/users/frenchd/Galaxy Table code/diameterFix/finalFits/ESO-LV_"Quick Blue"_IIa-O_vs_K_s(LGA_2MASS)_total.pdf',format='pdf')
    else:
        show()

    diameterFile.close()