Repository 'nepenthes_3dpca'
hg clone https://toolshed.g2.bx.psu.edu/repos/mb2013/nepenthes_3dpca

Changeset 15:60ed96f5706e (2014-05-20)
Previous changeset 14:70fc3c02ca14 (2014-05-20) Next changeset 16:fea55f4efcce (2014-05-20)
Commit message:
Uploaded
added:
ReMarker.py
b
diff -r 70fc3c02ca14 -r 60ed96f5706e ReMarker.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ReMarker.py Tue May 20 03:28:59 2014 -0400
[
b'@@ -0,0 +1,306 @@\n+#Program for correcting landmarks.\n+#The object will be rotated, based on 3 symmetry points.\n+#The differences between the mirror points will be calculated and\n+# the coordinates will be corrected where needed.\n+\n+#MB\n+\n+import math\n+from math import *\n+import sys\n+import numpy\n+import os\n+\n+# Main function\n+def main():\n+    file_name = sys.argv[1] # dta file\n+    file_connect = sys.argv[2] # connect file\n+    side = sys.argv[3] # correct side of object\n+    \n+    file_outputname5 = sys.argv[5] # output dta file\n+    output5 = open(file_outputname5, \'w\')\n+\n+    # to function \'open connect file\'\n+    sym1,sym2,sym3, listmirror, listspec = open_connect_file(file_connect)\n+\n+    # to function \'open dta file\'\n+    listdata = open_dta_file(file_name, output5)\n+\n+    # to function \'shift to zero\'\n+    shift_to_zero(listdata,sym1,sym2,sym3,listmirror,listspec, side, output5)\n+    \n+# Function for extracting values of connect file\n+def open_connect_file(file_connect):\n+    mirrorpoints = open(file_connect)\n+    lines= mirrorpoints.readlines()\n+    listmirror = [] #with mirror points\n+    listsym = [] #with symmetry points\n+    listspec = [] #with spec points\n+\n+    #extracting symmetry landmarks and mirror landmarks of connect file\n+    for x in range(0, len(lines)):\n+        line = lines[x].strip()\n+        if line == \'sym\': # extract points on symmetry plane\n+            for a in range(1,4):\n+                listsym.append(lines[a].strip().split())\n+        if line == \'mirror\': #  extract mirror points\n+            for y in range(x + 1,len(lines)):\n+                try:\n+                    if lines[y].strip() != \'spec\':\n+                        if lines[y].strip() == \'\':\n+                            break\n+                        else:\n+                            listmirror.append(lines[y].strip().split()) # add mirror points \n+                        \n+                    else:\n+                        break\n+                        \n+                except:\n+                    break\n+        if line == \'spec\':# if specs are present\n+            for b in range(x+1, len(lines)):\n+                listspec.append(lines[b].strip().split()) # add points in specs\n+    #Symmetry points\n+    sym1 = int(listsym[0][0]) -1\n+    sym2 = int(listsym[1][0]) -1 \n+    sym3 = int(listsym[2][0]) -1\n+    \n+    mirrorpoints.close()\n+    return sym1, sym2, sym3, listmirror, listspec\n+\n+# Function open .dta file\n+def open_dta_file(file_name, output5):\n+    datapoints = open(file_name)#open file\n+    datalines = datapoints.readlines()\n+    listdata = [] # landmarklist\n+    listdta = [] # coordinates\n+    sublist = [] # sublist\n+    for a  in range(0,len(datalines)):\n+        aline = datalines[a].strip()\n+        listdta.append(datalines[a].strip().split(\'  \')) # notice space\n+\n+        #numbers in dta file to list\n+        if len(listdta[a]) == 3:\n+            sublist.append(float(listdta[a][0]))\n+            sublist.append(float(listdta[a][1]))\n+            sublist.append(float(listdta[a][2]))\n+            listdata.append(sublist)\n+            sublist = []\n+            \n+        #write header of dta file to outputfile\n+        else:\n+            output5.write("%s\\n"%(aline))\n+\n+    datapoints.close()\n+    return listdata\n+\n+# Function to calculate the values in the Z rotation matrix\n+def Rz_matrix(z_angle): # Rz rotation matrix\n+    return [[cos(math.radians(z_angle)), -sin(math.radians(z_angle)), 0.0],[sin(math.radians(z_angle)),cos(math.radians(z_angle)),0.0],[0.0, 0.0, 1.0]]\n+\n+# Function to calculate the new coordinates rotated with Z rotation matrix\n+def Z_rotation(point2, z_angle): # multiplication rotation matrix and coordinates \n+    r_z = Rz_matrix(z_angle)\n+    rotated_z = []               \n+    for i in range(3):\n+        rotated_z.append((sum([r_z[i][j] * point2[j] for j in range(3)])))\n+    return rotated_z\n+\n+# Function to calculate the values in the X rotation matrix\n+def Rx_matrix(x_angle): #rotation matrix x-axis\n+'..b')        \n+\n+# Function writing rotated coordinates to file\n+def write_rotate_co(listdata6):\n+    file_outputname4 = sys.argv[4]\n+    output4 = open(file_outputname4, \'w\')\n+    #writing new coordinates to outputfile\n+    for x in range(0,len(listdata6)):\n+        output4.write("%.7f\\t%.7f\\t%.7f\\n"%(listdata6[x][0], listdata6[x][1], listdata6[x][2]))\n+\n+#Function for correcting the landmarks     \n+def correct_landmarks(listdata6, listmirror, side,output5):\n+\n+    percentage_distance = []\n+    #calculate marge of distance\n+    for x in range(0,len(listmirror)):  \n+        left = listmirror[x][0] #left landmark\n+        right = listmirror[x][1] #right landmark\n+        co_left = listdata6[int(left)-1] #left landmark coordinates\n+        co_left_x = co_left[0] # left landmark coordinate x\n+        co_right = listdata6[int(right)-1]#right landmark coordinates\n+        co_right_x = co_right[0] #right landmark coordinate x\n+        distance_x = float(co_left[0]) + float(co_right[0])\n+        \n+        # if left is correct\n+        if int(side) == 0:\n+            percentage_distance.append(abs(distance_x) / abs(co_left_x)) #percentage \n+\n+        # if right is correct\n+        elif int(side) == 1:\n+            percentage_distance.append(abs(distance_x) / abs(co_right_x)) #percentage\n+        else:\n+            print \'something wrong\'\n+\n+\n+    mean_percentage = numpy.mean(percentage_distance) #mean of percentages\n+    std_percentage = numpy.std(percentage_distance)\n+    #range of correct values\n+    left_range = mean_percentage - std_percentage #left range mean minus one standard deviation\n+    right_range = mean_percentage + std_percentage # rigth range mean plus one standard deviation\n+\n+    #correcting the landmarks coordinates#\n+    for x in range(0,len(listmirror)):\n+        left = listmirror[x][0]\n+        right = listmirror[x][1]\n+        co_left = listdata6[int(left)-1]\n+        co_right = listdata6[int(right)-1]\n+        #if there is to much deviation take correct coordinate and project it to the other side. \n+        if (percentage_distance[x] > right_range) or (percentage_distance[x] < left_range):\n+            \n+            #if left side is correct\n+            if int(side) == 0:\n+                listdata6[int(right)-1][0] = float(co_left[0]) * -1\n+                listdata6[int(right)-1][1] = float(co_left[1])\n+                listdata6[int(right)-1][2] = float(co_left[2])\n+            #if right side is correct \n+            if int(side) == 1:\n+                listdata6[int(left)-1][0] = float(co_right[0]) * -1\n+                listdata6[int(left)-1][1] = float(co_right[1])\n+                listdata6[int(left)-1][2] = float(co_right[2])\n+    write_output(listdata6,output5)\n+\n+#Function for correcting landmarks, defined in specs\n+def correct_specs(listdata6, listmirror, listspec,output5):\n+    for x in range(0,len(listspec)):\n+        number = listspec[x][0]\n+        for spec in range(0, len(listmirror)):\n+            try: \n+                pos = listmirror[spec].index(number) #find number in mirror list\n+                #extracting the opposite landmark number\n+                if pos == 0: \n+                    number2 = listmirror[spec][1]\n+                elif pos == 1 :\n+                    number2 = listmirror[spec][0]\n+                else:\n+                    print \'wrong\'\n+                #replace the wrong coordinates \n+                listdata6[int(number)-1][0] = float(listdata6[int(number2)-1][0])*-1\n+                listdata6[int(number)-1][1] = float(listdata6[int(number2)-1][1])\n+                listdata6[int(number)-1][2] = float(listdata6[int(number2)-1][2])  \n+            except:\n+                continue\n+    write_output(listdata6,output5)\n+\n+#Function for writing the corrected landmarks to outputfile\n+def write_output(listdata6,output5):\n+\n+    for x in range(0,len(listdata6)):\n+        output5.write("%.7f  %.7f  %.7f\\n"%(listdata6[x][0], listdata6[x][1], listdata6[x][2]))\n+    output5.close()\n+main()\n'