a python typeError
0
0
Entering edit mode
9.0 years ago
lilepisorus ▴ 40

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
SEEDGWAS1003    27086    20.485433    -89.732539
SEEDGWAS1004    27110    20.969048    -97.382255
SEEDGWAS1006    27507    -23.267981    -57.309104
SEEDGWAS1007    27713    8.433333333    -62.46666667

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 = []
        f.next() # skip header
        for line in f:
            fields = line.strip().split()
            latitude1 = float(fields[2])
            longitude1 = float(fields[3])
            sample1 = fields[0]
            self.latitudes.append(latitude1)
            self.longitudes.append(longitude1)
            self.samples.append(sample1)


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])):
    outFile.write(str(geoDis[0][num]))
    outFile.write("\t")
    outFile.write(str(geoDis[1][num])+"\n")

And I have got errors as follows:

Traceback (most recent call last):
  File "geoDisFromLongiLatiDecimal.py", line 49, in <module>
    geoDis = haversine(filename, 18.099138, -100.243303)
  File "geoDisFromLongiLatiDecimal.py", 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!

Li

python • 2.1k views
ADD COMMENT
0
Entering edit mode

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 REPLY
1
Entering edit mode

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 REPLY
2
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 2614 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6