Skip to content

Commit

Permalink
Merge pull request #3 from 1hada/renaming-global_output_path-to-outpu…
Browse files Browse the repository at this point in the history
…t_path

Renaming global output path to output path
  • Loading branch information
1hada authored Aug 6, 2022
2 parents 19aed0c + 8d1d536 commit 8ad88fe
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tests/MANUAL_TESTS
tests/data/merged_bag.bag
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Basic elements for usage are an in path and out path for data being processed an
* Define an outbag name using the flag.

```--input_paths```
* Define global input paths for the data of interest. (csv or bags)
* Define input paths for the data of interest. (csv or bags)

```--global_output_path```
```--output_path```
* Define where to save newly created data (csv or bags)

```--topics```
Expand All @@ -50,8 +50,8 @@ Basic elements for usage are an in path and out path for data being processed an
### Some environment variables

```
export IN_PATH=/global/path/to/data
export OUT_PATH=/global/path/to/data
export IN_PATH=/path/to/data
export OUT_PATH=/path/to/data
export OUTBAG_NAME=new-bag-name
export INPUT_TOPICS='/topic/namespace_1 /topic/namespace_2 /topic/namespace_3'
```
Expand All @@ -65,34 +65,34 @@ rosbag-merge --write_bag --outbag_name $OUTBAG_NAME

Make a merged bage with all topics.
```
rosbag-merge --input_paths $IN_PATH --global_output_path $OUT_PATH --outbag_name $OUTBAG_NAME --write_bag
rosbag-merge --input_paths $IN_PATH --output_path $OUT_PATH --outbag_name $OUTBAG_NAME --write_bag
```

To merge bag files with select topics, and make single topics csvs.
```
rosbag-merge --input_paths $IN_PATH --global_output_path $OUT_PATH --outbag_name $OUTBAG_NAME --topics $INPUT_TOPICS --write_bag
rosbag-merge --input_paths $IN_PATH --output_path $OUT_PATH --outbag_name $OUTBAG_NAME --topics $INPUT_TOPICS --write_bag
```

To merge bag files with select topics, and make single topics csvs.
```
rosbag-merge --input_paths $IN_PATH --global_output_path $OUT_PATH --outbag_name $OUTBAG_NAME --write_bag --write_csvs
rosbag-merge --input_paths $IN_PATH --output_path $OUT_PATH --outbag_name $OUTBAG_NAME --write_bag --write_csvs
```

Loop through the bag msgs and make single topics csvs.
```
rosbag-merge --input_paths $IN_PATH --global_output_path $OUT_PATH --outbag_name $OUTBAG_NAME --write_csvs
rosbag-merge --input_paths $IN_PATH --output_path $OUT_PATH --outbag_name $OUTBAG_NAME --write_csvs
```

Loop through the bag msgs and make single topics csvs.
```
rosbag-merge --input_paths $IN_PATH --global_output_path $OUT_PATH --outbag_name $OUTBAG_NAME --write_csvs
rosbag-merge --input_paths $IN_PATH --output_path $OUT_PATH --outbag_name $OUTBAG_NAME --write_csvs
```


### Merge CSVs

For now, merging csvs must be done **after** making the CSVs on a previous command.
```
rosbag-merge --input_paths $IN_PATH --global_output_path $OUT_PATH --merge_csvs
rosbag-merge --input_paths $IN_PATH --output_path $OUT_PATH --merge_csvs
```

2 changes: 1 addition & 1 deletion examples/main_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

main_args = argparse.Namespace()
main_args.input_paths = [f"{os.getcwd()}/tests/data/raw"]
main_args.global_output_path = f"{os.getcwd()}/tests/data"
main_args.output_path = f"{os.getcwd()}/tests/data"
main_args.outbag_name = "merged_bag"
main_args.write_bag = True
main_args.topic_file = f"{os.getcwd()}/tests/topic_list.txt"
Expand Down
20 changes: 10 additions & 10 deletions src/rosbag_merge/bag_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
CSVS = rosbag_to_csv.CsvStreams

# inspired from : https://github.com/Kautenja/rosbag-tools/blob/master/merge.py , which has no LICENSE information and is not maintained
def stream(input_bag: Bag, topics: list = ["*"], output_bag: Bag=None, input_bag_filename: str=None, write_csvs : bool=False, global_output_path : str="") -> None:
def stream(input_bag: Bag, topics: list = ["*"], output_bag: Bag=None, input_bag_filename: str=None, write_csvs : bool=False, output_path : str="") -> None:
"""Stream data from an input bag to an output bag.
:param input_bag: the input bag to stream from
Expand All @@ -26,7 +26,7 @@ def stream(input_bag: Bag, topics: list = ["*"], output_bag: Bag=None, input_bag
:param input_bag_filename: used to write the csv name
:param write_csvs: bool to determine whether to write csvs.
:param topics: a list of the topics to include
:param global_output_path: The global path to write the csv with.
:param output_path: The global path to write the csv with.
:return: None
"""
Expand All @@ -41,7 +41,7 @@ def stream(input_bag: Bag, topics: list = ["*"], output_bag: Bag=None, input_bag
merge_bags.write_to_bag(output_bag, topic, msg, time)
if (write_csvs):
# write this message to the output csv
CSVS.write_data(topic,msg,time,input_bag_filename,global_output_path)
CSVS.write_data(topic,msg,time,input_bag_filename,output_path)
# increment the counter of included messages
msg_count += 1
if ( 0 == msg_count % prog_bar_update_interval):
Expand All @@ -51,32 +51,32 @@ def stream(input_bag: Bag, topics: list = ["*"], output_bag: Bag=None, input_bag
prog.set_postfix(msg_count=msg_count, prog_bar_update_interval=prog_bar_update_interval)
prog.close()

def stream_iter(input_bags : 'list[str]', topics : 'list[str]'=["*"], output_bag: Bag=None, write_csvs : bool=False, global_output_path : str=False) -> None:
def stream_iter(input_bags : 'list[str]', topics : 'list[str]'=["*"], output_bag: Bag=None, write_csvs : bool=False, output_path : str=False) -> None:
"""Iterate over the input files
:param input_bags: a list of bag names to write data to.
:param topics: a list of the topics to include
:param output_bag: the output bag to write data to
:param write_csvs: bool to determine whether to write csvs.
:param global_output_path: The global path to write the csv with.
:param output_path: The global path to write the csv with.
:return: None
"""
for filename in tqdm(input_bags, unit='bag'):
# open the input bag with an automatically closing context
with Bag(filename, 'r') as input_bag:
# stream the input bag to the output bag
stream(input_bag, topics=topics, output_bag=output_bag, input_bag_filename=filename, write_csvs=write_csvs, global_output_path=global_output_path)
stream(input_bag, topics=topics, output_bag=output_bag, input_bag_filename=filename, write_csvs=write_csvs, output_path=output_path)

def main(input_bags : 'list[str]', topics : 'list[str]', write_bag : bool, write_csvs : bool, global_output_path : str, outbag_name : str):
def main(input_bags : 'list[str]', topics : 'list[str]', write_bag : bool, write_csvs : bool, output_path : str, outbag_name : str):
try:
if(write_bag):
full_bag_path = os.path.join(global_output_path,outbag_name+".bag")
full_bag_path = os.path.join(output_path,outbag_name+".bag")
# open the output bag in an automatically closing context
with Bag(full_bag_path, 'w') as output_bag:
stream_iter(input_bags, topics = topics, output_bag = output_bag, write_csvs=write_csvs, global_output_path=global_output_path)
stream_iter(input_bags, topics = topics, output_bag = output_bag, write_csvs=write_csvs, output_path=output_path)
else:
stream_iter(input_bags=input_bags, topics = topics, write_csvs=write_csvs, global_output_path=global_output_path)
stream_iter(input_bags=input_bags, topics = topics, write_csvs=write_csvs, output_path=output_path)
except KeyboardInterrupt:
pass
finally:
Expand Down
10 changes: 5 additions & 5 deletions src/rosbag_merge/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def refine_args(args:argparse.Namespace)-> argparse.Namespace:
if (no_outbag_actions):
ic("Writing bags requires an outbag suffix.")
no_input_files = (("input_bags" not in args) or (not len(args.input_bags))) and (("input_csvs" not in args) or (not len(args.input_csvs)))
args.write_bag = True if args.global_output_path and args.outbag_name else False
requesting_write_without_output_path = not args.global_output_path and (args.merge_csvs or args.write_csvs or args.write_bag)
args.write_bag = True if args.output_path and args.outbag_name else False
requesting_write_without_output_path = not args.output_path and (args.merge_csvs or args.write_csvs or args.write_bag)
no_csvs_to_merge = (("input_csvs" not in args) or not (len(args.input_csvs) or args.write_csvs)) and args.merge_csvs
if (no_csvs_to_merge):
ic("unable to merge non-existent (present tense and future) csvs.")
Expand All @@ -78,7 +78,7 @@ def create_parser()-> argparse.ArgumentParser:
# create an argument parser to read arguments from the command line
parser = argparse.ArgumentParser(description=__doc__)
# add an argument for the output bag to create
parser.add_argument('--global_output_path', '-gop',
parser.add_argument('--output_path', '-op',
type=str,
help='The output path for all outputted files. (global paths)',
default="./",
Expand Down Expand Up @@ -145,13 +145,13 @@ def main(args:argparse.Namespace=None):
expecting_to_merge_newly_written_csvs = args.write_csvs and (not len(args.input_csvs))
if(expecting_to_merge_newly_written_csvs):
# gather newly made csvs from the output path
args.input_csvs.extend(glob.glob(os.path.join(args.global_output_path,"*-single-topic.csv")))
args.input_csvs.extend(glob.glob(os.path.join(args.output_path,"*-single-topic.csv")))
csv_merge.merge_csvs_using_dask(args.input_csvs)
else:
bag_stream.main(input_bags=args.input_bags
,write_bag=args.write_bag
,write_csvs=args.write_csvs
,global_output_path=args.global_output_path
,output_path=args.output_path
,outbag_name=args.outbag_name
,topics=args.topics
)
Expand Down
12 changes: 6 additions & 6 deletions src/rosbag_merge/rosbag_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ def __init__(self):
self.replace_newline_char = True
self.newline_char_replacement = '<-NEW-LINE->>,<<-NEW-LINE>' # using new line as csv but retaining history of new line existence

def write_data(self, topic: str, msg: object, time: rospy.rostime.Time , input_bag_file : str, global_output_path: str,use_header: bool = True):
def write_data(self, topic: str, msg: object, time: rospy.rostime.Time , input_bag_file : str, output_path: str,use_header: bool = True):
"""Write data to a csv file.
:param topic: the topic name
:param msg: the ros msg
:param input_bag_file: The input bag file name which is used to construct the output csv name
:param global_output_path: The global path to write the csv with.
:param output_path: The global path to write the csv with.
:param write_header: a bool to write a header or not.
:return: None
"""
stream = self.get_stream(topic,msg,input_bag_file, global_output_path, use_header)
stream = self.get_stream(topic,msg,input_bag_file, output_path, use_header)
# DatetimeIndex format
date_time_str = datetime.fromtimestamp(
time.to_time()).strftime('%Y-%m %d %H:%M:%S.%f')
stream.write(date_time_str)
self.message_to_csv(stream, msg)
stream.write('\n')

def get_stream(self, topic: str, msg : object, input_bag_file : str, global_output_path: str, write_header: bool = True) -> io.TextIOWrapper:
def get_stream(self, topic: str, msg : object, input_bag_file : str, output_path: str, write_header: bool = True) -> io.TextIOWrapper:
"""Open/access a file stream, write a header in the file, and return it.
:param topic: the topic name
:param msg: the ros msg
:param input_bag_file: The input bag file name which is used to construct the output csv name
:param global_output_path: The global path to write the csv with.
:param output_path: The global path to write the csv with.
:param write_header: a bool to write a header or not.
:return: A file ptr/stream which has been opened.
Expand All @@ -59,7 +59,7 @@ def get_stream(self, topic: str, msg : object, input_bag_file : str, global_outp
stream = self.streamdict[topic]
else:
# the input bag name is used to write the outputted csv
outpath_with_file = os.path.join(global_output_path, os.path.basename( input_bag_file ))
outpath_with_file = os.path.join(output_path, os.path.basename( input_bag_file ))
stream = open(
self.format_csv_filename(
self.output_file_format,
Expand Down

0 comments on commit 8ad88fe

Please sign in to comment.