Skip to content

Commit

Permalink
# bug changes
Browse files Browse the repository at this point in the history
  • Loading branch information
udayRage committed Dec 4, 2024
1 parent ee17afc commit dfed058
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
71 changes: 46 additions & 25 deletions PAMI/extras/neighbours/FindNeighboursUsingEuclidean.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#




__copyright__ = """
Copyright (C) 2021 Rage Uday Kiran
Expand All @@ -30,9 +28,11 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

import sys
import re
from math import sqrt
import time
import sys, psutil, os


class FindNeighboursUsingEuclidean:
"""
Expand All @@ -42,8 +42,6 @@ class FindNeighboursUsingEuclidean:
:param iFile : file
Input file name or path of the input file
:param oFile : file
Output file name or path pf the output file
:param maxDist : int
The user can specify maxEuclideanDistance.
This program find pairs of values whose Euclidean distance is less than or equal to maxEucledianDistace
Expand All @@ -69,22 +67,20 @@ class FindNeighboursUsingEuclidean:
obj.save()
"""

def __init__(self,iFile: str,oFile: str,maxDist: int, sep='\t') -> None:
def __init__(self, iFile: str, maxDist: int, sep='\t') -> None:
self.iFile = iFile
self.oFile = oFile
self.maxEucledianDistance = maxDist
self.seperator = sep
self.result = {}


def create(self):
self._startTime = time.time()
coordinates = []
result = {}
with open(self.iFile,"r") as f:
with open(self.iFile, "r") as f:
for line in f:
l = line.rstrip().split(self.seperator)
#print(l)
l[0] = re.sub(r'[^0-9. ]', '', l[0])
coordinates.append(l[0].rstrip().split(' '))
#print(l[0])
for i in range(len(coordinates)):
for j in range(len(coordinates)):
if i != j:
Expand All @@ -94,26 +90,51 @@ def __init__(self,iFile: str,oFile: str,maxDist: int, sep='\t') -> None:
y1 = float(firstCoordinate[1])
x2 = float(secondCoordinate[0])
y2 = float(secondCoordinate[1])
ansX = x2-x1
ansY = y2-y1
dist = abs(pow(ansX,2) - pow(ansY,2))
ansX = x2 - x1
ansY = y2 - y1
dist = abs(pow(ansX, 2) - pow(ansY, 2))
norm = sqrt(dist)
if norm <= float(self.maxEucledianDistance):
result[tuple(firstCoordinate)] = result.get(tuple(firstCoordinate),[])
result[tuple(firstCoordinate)].append(secondCoordinate)

with open(self.oFile,"w+") as f:
for i in result:
string = "Point(" +i[0]+" "+i[1] + ")"+ self.seperator
self.result[tuple(firstCoordinate)] = self.result.get(tuple(firstCoordinate), [])
self.result[tuple(firstCoordinate)].append(secondCoordinate)
self._endTime = time.time()

def save(oFile: str) -> None:
with open(oFile, "w+") as f:
for i in self.result:
string = "Point(" + i[0] + " " + i[1] + ")" + self.seperator
f.write(string)
for j in result[i]:
string = "Point(" + j[0] + " " + j[1] + ")"+ self.seperator
for j in self.result[i]:
string = "Point(" + j[0] + " " + j[1] + ")" + self.seperator
f.write(string)
f.write("\n")

def getNeighbors():
return self.result

def getRuntime(self) -> float:
"""
Get the runtime of the transactional database
:return: the runtime of the transactional database
:rtype: float
"""
return self._endTime - self._startTime

def getMemoryUSS(self) -> float:

process = psutil.Process(os.getpid())
self._memoryUSS = process.memory_full_info().uss
return self._memoryUSS

def getMemoryRSS(self) -> float:

process = psutil.Process(os.getpid())
self._memoryRSS = process.memory_info().rss
return self._memoryRSS

def getFileName(self) -> str:
return self.oFile

if __name__ == "__main__":
obj = FindNeighboursUsingEuclidean(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
22 changes: 11 additions & 11 deletions PAMI/extras/neighbours/FindNeighboursUsingGeodesic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import sys
import re
from geopy.distance import geodesic

import time
import sys, psutil, os

class FindNeighboursUsingGeodesic:
"""
Expand Down Expand Up @@ -70,14 +71,14 @@ class FindNeighboursUsingGeodesic:
obj.save()
"""

def __init__(self, iFile: str, oFile: str, maxDist: float, sep='\t'):
def __init__(self, iFile: str, maxDist: float, sep='\t'):
self.iFile = iFile
self.oFile = oFile
self.maxGeodesicDistance = maxDist
self.seperator = sep

def create(self)->None:
coordinates = []
result = {}
self.result = {}
with open(self.iFile, "r") as f:
for line in f:
l = line.rstrip().split(self.seperator)
Expand All @@ -98,20 +99,19 @@ def __init__(self, iFile: str, oFile: str, maxDist: float, sep='\t'):
dist = geodesic((lat1, long1), (lat2, long2)).kilometers

if dist <= float(self.maxGeodesicDistance):
result[tuple(firstCoordinate)] = result.get(tuple(firstCoordinate), [])
result[tuple(firstCoordinate)].append(secondCoordinate)
self.result[tuple(firstCoordinate)] = self.result.get(tuple(firstCoordinate), [])
self.result[tuple(firstCoordinate)].append(secondCoordinate)

with open(self.oFile, "w+") as f:
for i in result:
def save(self,oFile:str)->None:
with open(oFile, "w+") as f:
for i in self.result:
string = "Point(" + i[0] + " " + i[1] + ")" + self.seperator
f.write(string)
for j in result[i]:
for j in self.result[i]:
string = "Point(" + j[0] + " " + j[1] + ")" + self.seperator
f.write(string)
f.write("\n")

def getFileName(self):
return self.oFile


if __name__ == "__main__":
Expand Down

0 comments on commit dfed058

Please sign in to comment.