Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
km3io
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
km3py
km3io
Commits
c216576a
Commit
c216576a
authored
5 years ago
by
Zineb Aly
Browse files
Options
Downloads
Patches
Plain Diff
write/read to/from csv 1st attempt
parent
a0928bcd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!10
Add header readout for offline files. Closes #13
Pipeline
#7885
passed with warnings
5 years ago
Stage: test
Stage: coverage
Changes
1
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
km3io/write.py
+94
-0
94 additions, 0 deletions
km3io/write.py
with
94 additions
and
0 deletions
km3io/write.py
0 → 100644
+
94
−
0
View file @
c216576a
import
os
import
inspect
import
numpy
as
np
import
km3io
as
ki
import
awkward
as
aw
CLASSES
=
tuple
(
m
[
1
]
for
m
in
inspect
.
getmembers
(
ki
.
offline
,
inspect
.
isclass
))
def
to_csv
(
f_name
,
**
kwargs
):
"""
writes data in a csv file
Parameters
----------
f_name : str
Description
**kwargs
Description
"""
if
os
.
path
.
isfile
(
f_name
):
raise
OSError
(
"'
{}
'
already exists! change the file name or delete it
"
.
format
(
f_name
))
with
open
(
f_name
+
'
.csv
'
,
'
a
'
)
as
fp
:
for
key
,
value
in
kwargs
.
items
():
# write events, hits and tracks classes
if
isinstance
(
value
,
CLASSES
):
for
k
,
v
in
zip
(
value
.
_keys
,
np
.
array
(
value
.
_values
)):
# if it doesn't work change enumerate(v) to enumerate(np.array(v))
fp
.
write
(
'
# {}
\n
'
.
format
(
k
))
for
i
,
j
in
enumerate
(
v
):
np
.
savetxt
(
fp
,
j
,
header
=
str
(
i
),
comments
=
'
##
'
)
# write arrays
if
isinstance
(
value
,
np
.
ndarray
):
np
.
savetxt
(
fp
,
value
,
header
=
key
,
comments
=
'
#
'
)
else
:
try
:
# write a jagged array
if
value
.
dtype
.
char
==
'
O
'
:
# O is the unique char that identifies 'awkward.array.chunked.ChunkedArray'
fp
.
write
(
'
# {}
\n
'
.
format
(
key
))
for
i
,
j
in
enumerate
(
value
):
np
.
savetxt
(
fp
,
j
,
header
=
str
(
i
),
comments
=
'
##
'
)
except
Exception
:
raise
TypeError
(
"'
{}
'
is not a valid data structure
"
.
format
(
type
(
value
)))
def
from_csv
(
f_name
,
headers
=
None
):
"""
reads a csv file written by km3io
Parameters
----------
f_name : str
Description
headers : None, optional
list of str of headers
"""
if
headers
is
None
:
keys
=
[]
values
=
[]
found_header
=
False
found_sub_header
=
False
# read the entire file
with
open
(
f_name
,
'
r
'
)
as
fp
:
jagged_data
=
[]
jagged_sub_data
=
[]
for
line
in
fp
:
if
'
#
'
in
line
:
found_header
=
True
keys
.
append
(
line
.
split
()[
1
])
# convert str to float
if
found_header
:
if
'
#
'
in
line
:
found_header
=
False
if
'
##
'
in
line
:
found_sub_header
=
True
else
:
jagged_data
.
append
(
line
.
split
()[
0
])
if
found_header
and
found_sub_header
:
if
'
##
'
in
line
:
found_sub_header
=
False
jagged_data
.
append
(
jagged_sub_data
)
else
:
jagged_sub_data
.
append
(
lin
.
split
()[
0
])
#write in hdf
#write in root (NOT NECESSARY)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment