Skip to content

Commit

Permalink
New examples
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Oct 12, 2024
1 parent fedec4b commit 4590cf1
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
85 changes: 85 additions & 0 deletions pyxcp/examples/ex_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import argparse
import logging
import os
import csv
from array import array
from dataclasses import dataclass, field
from mmap import PAGESIZE
from pathlib import Path
from typing import Any, List

from pyxcp.recorder import XcpLogFileDecoder
from pyxcp.recorder.converter import MAP_TO_ARRAY


MAP_TO_SQL = {
"U8": "INTEGER",
"I8": "INTEGER",
"U16": "INTEGER",
"I16": "INTEGER",
"U32": "INTEGER",
"I32": "INTEGER",
"U64": "INTEGER",
"I64": "INTEGER",
"F32": "FLOAT",
"F64": "FLOAT",
"F16": "FLOAT",
"BF16": "FLOAT",
}

logger = logging.getLogger("PyXCP")

parser = argparse.ArgumentParser(description="Use .xmraw files in an Apache Arrow application.")
parser.add_argument("xmraw_file", help=".xmraw file")
args = parser.parse_args()


@dataclass
class Storage:
name: str
arrow_type: Any
arr: array


@dataclass
class StorageContainer:
name: str
arr: List[Storage] = field(default_factory=[])
ts0: List[int] = field(default_factory=lambda: array("Q"))
ts1: List[int] = field(default_factory=lambda: array("Q"))


class Decoder(XcpLogFileDecoder):

def __init__(self, recording_file_name: str):
super().__init__(recording_file_name)

def initialize(self) -> None:
self.arrow_tables = []
self.csv_writers = []
for dl in self.daq_lists:
result = []
for name, type_str in dl.headers:
array_txpe = MAP_TO_ARRAY[type_str]
sql_type = MAP_TO_SQL[type_str]
sd = Storage(name, sql_type, array(array_txpe))
result.append(sd)
sc = StorageContainer(dl.name, result)
writer = csv.writer(open(f"{sc.name}.csv", "w", newline=""), dialect="excel")
headers = ["ts0", "ts1"] + [e.name for e in sc.arr]
writer.writerow(headers)
self.csv_writers.append(writer)
self.arrow_tables.append(sc)
print("\nInserting data...")

def finalize(self) -> None:
print("Done.")

def on_daq_list(self, daq_list_num: int, timestamp0: int, timestamp1: int, measurements: list) -> None:
sc = self.arrow_tables[daq_list_num]
writer = self.csv_writers[daq_list_num]
data = [timestamp0, timestamp1, *measurements]
writer.writerow(data)

decoder = Decoder(args.xmraw_file)
decoder.run()
95 changes: 95 additions & 0 deletions pyxcp/examples/ex_excel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import argparse
import logging
import os
import xlsxwriter
from array import array
from dataclasses import dataclass, field
from mmap import PAGESIZE
from pathlib import Path
from typing import Any, List

from pyxcp.recorder import XcpLogFileDecoder
from pyxcp.recorder.converter import MAP_TO_ARRAY


MAP_TO_SQL = {
"U8": "INTEGER",
"I8": "INTEGER",
"U16": "INTEGER",
"I16": "INTEGER",
"U32": "INTEGER",
"I32": "INTEGER",
"U64": "INTEGER",
"I64": "INTEGER",
"F32": "FLOAT",
"F64": "FLOAT",
"F16": "FLOAT",
"BF16": "FLOAT",
}

logger = logging.getLogger("PyXCP")

parser = argparse.ArgumentParser(description="Use .xmraw files in an Apache Arrow application.")
parser.add_argument("xmraw_file", help=".xmraw file")
args = parser.parse_args()


@dataclass
class Storage:
name: str
arrow_type: Any
arr: array


@dataclass
class StorageContainer:
name: str
arr: List[Storage] = field(default_factory=[])
ts0: List[int] = field(default_factory=lambda: array("Q"))
ts1: List[int] = field(default_factory=lambda: array("Q"))


class Decoder(XcpLogFileDecoder):

def __init__(self, recording_file_name: str):
super().__init__(recording_file_name)
self.xls_file_name = Path(recording_file_name).with_suffix(".xlsx")
try:
os.unlink(self.xls_file_name)
except Exception as e:
print(e)

def initialize(self) -> None:
self.arrow_tables = []
self.xls_workbook = xlsxwriter.Workbook(self.xls_file_name)
self.xls_sheets = []
self.rows = []
for dl in self.daq_lists:
result = []
for name, type_str in dl.headers:
array_txpe = MAP_TO_ARRAY[type_str]
sql_type = MAP_TO_SQL[type_str]
sd = Storage(name, sql_type, array(array_txpe))
result.append(sd)
sc = StorageContainer(dl.name, result)
sheet = self.xls_workbook.add_worksheet(sc.name)
self.xls_sheets.append(sheet)
headers = ["ts0", "ts1"] + [e.name for e in sc.arr]
sheet.write_row(0, 0, headers)
self.rows.append(1)
self.arrow_tables.append(sc)
print("\nInserting data...")

def finalize(self) -> None:
self.xls_workbook.close()
print("Done.")

def on_daq_list(self, daq_list_num: int, timestamp0: int, timestamp1: int, measurements: list) -> None:
sheet = self.xls_sheets[daq_list_num]
row = self.rows[daq_list_num]
data = [timestamp0, timestamp1] + measurements
sheet.write_row(row, 0, data)
self.rows[daq_list_num] += 1

decoder = Decoder(args.xmraw_file)
decoder.run()

0 comments on commit 4590cf1

Please sign in to comment.