Question: a python typeError
gravatar for lilepisorus
5.5 years ago by
United States
lilepisorus30 wrote:

I am writing a python scripts to deal with my input file containing latitude and longitude infos, which i will convert to the geographical distance. 

Here is a short list of my input entries 

sampleID.unique    general_identifier    latitude    longitude </b>
SEEDGWAS1003    27086    20.485433    -89.732539 </b>
SEEDGWAS1004    27110    20.969048    -97.382255 </b>
SEEDGWAS1006    27507    -23.267981    -57.309104 </b>
SEEDGWAS1007    27713    8.433333333    -62.46666667 </b>

And my scripts is as follows:
import math
from math import radians, cos, sin, asin, sqrt
import sys

filename = sys.argv[1]
outFileName = sys.argv[2]

class latiLongi:
    def __init__(self, filename):
        f = open(filename, "r")
        self.latitudes = []
        self.longitudes = []
        self.samples = [] # skip header
        for line in f:
            fields = line.strip().split()
            latitude1 = float(fields[2])
            longitude1 = float(fields[3])
            sample1 = fields[0]


def haversine(filename, lat2, lon2):
    LL = latiLongi(filename)
    lat1 = [lati for lati in LL.latitudes]
    lon1 = [longi for longi in LL.longitudes]
    sample = [sample for sample in LL.samples]
    lat2 = float(lat2)
    lon2 = float(lon2)
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    km = 6367 * c
    return (sample, km)
geoDis = haversine(filename, 18.099138, -100.243303)

outFile = open(outFileName, 'w')

for num in range(0, len(geoDis[0])):
##And I have got errors as follows:
Traceback (most recent call last):
  File "", line 49, in <module>
    geoDis = haversine(filename, 18.099138, -100.243303)
  File "", line 40, in haversine
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
TypeError: a float is required

I am a newbie in python. Could someone help to tackle my errors? Thanks!


python • 1.4k views
ADD COMMENTlink written 5.5 years ago by lilepisorus30

maybe more appropriate elsewhere, this being a bioinformatics site.

But aren't you passing a list, when you should be passing a float? As a first step, write a test of haversine; or rewrite haversine as two functions, one that takes a file and one that manipulates the extracted data

ADD REPLYlink written 5.5 years ago by russhh5.5k

Thanks! I got it fixed. As my lat1 and lon1 are a series of numbers, while lat2 and lon2 just single number. I shouldnot do any computation between them.

ADD REPLYlink written 5.5 years ago by lilepisorus30

Pro tip: why the succinctness in variable names? Don't be afraid to spend a couple more keystrokes and name your variables descriptively,e.g.  lat_series, lon_series, lat_coordinate, lon_coordinate. Will make issues like these immediately obvious.

ADD REPLYlink written 5.5 years ago by Saulius Lukauskas530
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1709 users visited in the last hour