sparse.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 #
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2008, Willow Garage, Inc.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of the Willow Garage nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 
35 ##\author Kevin Watts
36 ##\brief Make CSV files smaller for use in spreadsheet software
37 
38 PKG = 'diagnostic_analysis'
39 import roslib
40 roslib.load_manifest(PKG)
41 
42 import csv, os, sys
43 
44 ##\brief Makes sparse CSV by skipping every nth value
45 ##\param csv_file str : CSV filename
46 ##\param skip int : Write every nth row to sparse CSV
47 ##\return Path of output file
48 def make_sparse_skip(csv_file, skip):
49  output_file = csv_file[:-4] + '_sparse.csv'
50 
51  input_reader = csv.reader(open(csv_file, 'rb'))
52 
53  f = open(output_file, 'wb')
54  output_writer = csv.writer(f)
55 
56  skip_count = skip
57  for row in input_reader:
58  if skip_count == skip:
59  output_writer.writerow(row)
60  skip_count = 0
61 
62  skip_count = skip_count + 1
63 
64  return output_file
65 
66 ##\brief Makes sparse CSV with the given number of rows
67 ##\param csv_file str : CSV filename
68 ##\param length int : Desired number of rows in CSV
69 ##\return Path of output file
70 def make_sparse_length(csv_file, length):
71  output_file = csv_file[:-4] + '_sprs_len.csv'
72 
73  input_reader = csv.reader(open(csv_file, 'rb'))
74 
75  f = open(output_file, 'wb')
76  output_writer = csv.writer(f)
77 
78  # Calculate skip count for file
79  orig_len = len(open(csv_file, 'r').read().split('\n'))
80  skip = max(int(orig_len / length), 1)
81 
82  skip_count = skip
83  for row in input_reader:
84  if skip_count >= skip:
85  output_writer.writerow(row)
86  skip_count = 0
87 
88  skip_count = skip_count + 1
89 
90  return output_file
def make_sparse_length(csv_file, length)
Makes sparse CSV with the given number of rows.
Definition: sparse.py:70
def make_sparse_skip(csv_file, skip)
Makes sparse CSV by skipping every nth value.
Definition: sparse.py:48


diagnostic_analysis
Author(s): Kevin Watts, Brice Rebsamen , Eric Berger, Kevin Watts
autogenerated on Thu Oct 8 2020 03:19:33