|
# Points class method |
|
from napari.io import write_csv |
|
|
|
... |
|
def to_table(self, path=None): |
|
"""Constructs a table of point coordinate data. |
|
|
|
Parameters |
|
---------- |
|
path : str, optional |
|
If a filename is provided, the table will be saved as a csv file. |
|
|
|
Returns |
|
------- |
|
tuple |
|
(table, column_names) where table is a list of lists, and |
|
column_names is a list of strings. |
|
|
|
Notes |
|
----- |
|
You can construct a pandas dataframe from the results like this: |
|
``` |
|
table, column_names = my_points.to_table() |
|
points_dataframe = pandas.DataFrame(table, columns=column_names) |
|
``` |
|
""" |
|
n_dimensions = self.data.shape[1] |
|
column_names = ['coord_id'] + [ |
|
'dim_' + str(n) for n in range(n_dimensions) |
|
] |
|
table = [] |
|
for idx, row in enumerate(self.data): |
|
data = [idx] + list(row) |
|
table.append(data) |
|
if path is not None: |
|
write_csv(path, table, column_names) |
|
return table, column_names |
|
|
|
|
|
# Shapes class method |
|
from napari.io import write_csv |
|
|
|
... |
|
def to_table(self, path=None): |
|
"""Constructs a table of shape coordinate data. |
|
|
|
Parameters |
|
---------- |
|
path : str, optional |
|
If a filename is provided, the table will be saved as a csv file. |
|
|
|
Returns |
|
------- |
|
tuple |
|
(table, column_names) where table is a list of lists, and |
|
column_names is a list of strings. |
|
|
|
Notes |
|
----- |
|
You can construct a pandas dataframe from the results like this: |
|
``` |
|
table, column_names = my_shapes.to_table() |
|
points_dataframe = pandas.DataFrame(table, columns=column_names) |
|
``` |
|
""" |
|
n_dimensions = max([i.shape[1] for i in self.data]) |
|
column_names = ['shape_id', 'shape_type', 'coord_id'] + [ |
|
'dim_' + str(n) for n in range(n_dimensions) |
|
] |
|
table = [] |
|
for idx, row in enumerate(self.data): |
|
data = [idx_shape, shape_type, idx] + list(row) |
|
table.append[data] |
|
if path is not None: |
|
write_csv(path, table, column_names) |
|
return table, column_names |
|
|
|
|
|
# Vector class method |
|
# ... this can be left for another time |
|
|
|
# Surface class method |
|
# ... this can be left for another time |