From 13a031a3444bd8ef4c68d2b58c5338d583df0bd3 Mon Sep 17 00:00:00 2001
From: Maciej Wielgosz <maciej.wielgosz@nibio.no>
Date: Tue, 18 Oct 2022 13:52:04 +0200
Subject: [PATCH] small bug fix |

---
 nibio_preprocessing/remove_small_tiles.py | 36 +++++------------------
 1 file changed, 8 insertions(+), 28 deletions(-)

diff --git a/nibio_preprocessing/remove_small_tiles.py b/nibio_preprocessing/remove_small_tiles.py
index 5007745..3025412 100644
--- a/nibio_preprocessing/remove_small_tiles.py
+++ b/nibio_preprocessing/remove_small_tiles.py
@@ -1,5 +1,6 @@
 import glob
 import os
+import pandas as pd
 from numpy import number
 import plyfile
 
@@ -28,40 +29,19 @@ class RemoveSmallTiles():
 
         return density, number_of_points
 
-    # @staticmethod
-    # def remove_line_from_csv(path, line):
-    #     """
-    #     Remove a line from a csv file.
-    #     """
-    #     with open(path, 'r') as f:
-    #         lines = f.readlines()
-    #     with open(path, 'w') as f:
-    #         for i in range(len(lines)):
-    #             if i != line:
-    #                 f.write(lines[i])
-    
     @staticmethod
     def remove_all_lines_from_csv(path, list_of_lines):
         """
         Remove all lines from a csv file.
         """
         # open the file
-        with open(path, 'r') as f:
-            lines = f.readlines()
-
-        # split lines into a dictionary with the line number as key and the line as value
-        lines_dict = {}
-        for i, line in enumerate(lines):
-            lines_dict[i] = line
-
-        # remove the lines from the dictionary that are in the list of lines to remove
-        for line in list_of_lines:
-            del lines_dict[line]
-
-        # write the dictionary back to the file
-        with open(path, 'w') as f:
-            for key, value in lines_dict.items():
-                f.write(value)
+        tile_index_csv = pd.read_csv(path, sep=' ', header=None, names=['tile_index', 'x_mean', 'y_mean'])
+
+        # remove the lines
+        tile_index_csv = tile_index_csv[~tile_index_csv.tile_index.isin(list_of_lines)]
+
+        # save the file
+        tile_index_csv.to_csv(path, sep=' ', header=False, index=False)
 
     def get_density_of_all_tiles(self):
         """
-- 
GitLab