Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Last active February 6, 2020 01:45
Show Gist options
  • Save GenevieveBuckley/eeeae8a803dc0d955354dcd365eef7ad to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/eeeae8a803dc0d955354dcd365eef7ad to your computer and use it in GitHub Desktop.
points to csv

Points csv

coord_id dim_0 dim_1 ... dim_n
0
1
...
n

Shapes csv

shape_id shape_type coord_id dim_0 dim_1 dim_2 ... dim_n
0 Ellipse 0
0 Ellipse 1
0 Ellipse 2
0 Ellipse 3
1 Rectangle 0
1 Rectangle 1
... ... ...
n Path n-1
n Path n

Vectors csv

??? This can be left for another time

Surfaces csv

??? This can be left for another time

# 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
#napari.io.py
import csv
def write_csv(path, data, column_names=None):
"""Write a csv file.
Parameters
----------
path : str
Filename for saving csv.
data : list or ndarray
Table values, contained in a list of lists or an ndarray.
column_names : list, optional
List of column names for table data.
"""
with open(path, mode='w') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL
)
if column_names is not None:
writer.writerow(column_names)
for row in data:
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment