Skip to content

Commit

Permalink
Add TsFile Python.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinLeeo committed Jun 18, 2024
1 parent 0b496e8 commit 2c870d0
Show file tree
Hide file tree
Showing 13 changed files with 1,170 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ jobs:
- name: Build and test with Maven (All others)
shell: bash
run: |
./mvnw${{ steps.platform_suffix.outputs.platform_suffix }} -P with-java,with-cpp clean verify
./mvnw${{ steps.platform_suffix.outputs.platform_suffix }} -P with-java,with-cpp,with-python clean verify
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ docs/node_modules/
docs/src/.vuepress/.cache/
docs/src/.vuepress/.temp/
docs/src/.vuepress/dist/

# python files
python/build
python/tsfile/__pycache__
python/tsfile/*.so*
python/tsfile/TsFile_cwrapper.h
python/tsfile/*.cpp
python/data
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<exclude>**/.clang-format</exclude>
<!-- generated by cython-->
<exclude>**/tsfile/tsfile_pywrapper.cpp</exclude>
<exclude>**/venv/**</exclude>
</excludes>
</configuration>
</plugin>
Expand Down Expand Up @@ -579,6 +580,13 @@
<module>cpp</module>
</modules>
</profile>
<!-- Build the python version of TsFile -->
<profile>
<id>with-python</id>
<modules>
<module>python</module>
</modules>
</profile>
<profile>
<id>.java-9-and-above</id>
<activation>
Expand Down Expand Up @@ -653,6 +661,8 @@
<os.suffix>linux</os.suffix>
<os.classifier>linux-x86_64</os.classifier>
<cmake.generator>Unix Makefiles</cmake.generator>
<python.venv.bin>venv/bin/</python.venv.bin>
<python.exe.bin>python3</python.exe.bin>
</properties>
</profile>
<!-- Profile for linux amd64 (mainly AMD Processors) (Self-Enabling) -->
Expand All @@ -669,6 +679,8 @@
<os.suffix>linux</os.suffix>
<os.classifier>linux-amd64</os.classifier>
<cmake.generator>Unix Makefiles</cmake.generator>
<python.venv.bin>venv/bin/</python.venv.bin>
<python.exe.bin>python3</python.exe.bin>
</properties>
</profile>
<!-- Profile for linux aarch64 (mainly newer Mac or Raspberry PI Processors) (Self-Enabling) -->
Expand All @@ -685,6 +697,8 @@
<os.suffix>linux</os.suffix>
<os.classifier>linux-${os.arch}</os.classifier>
<cmake.generator>Unix Makefiles</cmake.generator>
<python.venv.bin>venv/bin/</python.venv.bin>
<python.exe.bin>python3</python.exe.bin>
</properties>
</profile>
<!-- Profile for mac x86_64 (mainly Intel Processors) (Self-Enabling) -->
Expand All @@ -700,6 +714,8 @@
<os.suffix>mac</os.suffix>
<os.classifier>mac-x86_64</os.classifier>
<cmake.generator>Unix Makefiles</cmake.generator>
<python.venv.bin>venv/bin/</python.venv.bin>
<python.exe.bin>python3</python.exe.bin>
</properties>
</profile>
<!-- Profile for mac aarch64 (mainly AMD Processors) (Self-Enabling) -->
Expand All @@ -715,6 +731,8 @@
<os.suffix>mac</os.suffix>
<os.classifier>mac-aarch64</os.classifier>
<cmake.generator>Unix Makefiles</cmake.generator>
<python.venv.bin>venv/bin/</python.venv.bin>
<python.exe.bin>python3</python.exe.bin>
</properties>
</profile>
<!-- profile for windows x86_64 (mainly Intel Processors) (Self-Enabling) -->
Expand All @@ -732,6 +750,8 @@
<cmake.generator>MinGW Makefiles</cmake.generator>
<!-- The generated code relied on Boost and that relies on VS and can't be built with MinGW -->
<!--cmake.generator>Visual Studio 17 2022</cmake.generator-->
<python.venv.bin>venv/Scripts/</python.venv.bin>
<python.exe.bin>python</python.exe.bin>
</properties>
</profile>
<!-- profile for windows amd64 (mainly AMD Processors) (Self-Enabling) -->
Expand All @@ -749,6 +769,8 @@
<cmake.generator>MinGW Makefiles</cmake.generator>
<!-- The generated code relied on Boost and that relies on VS and can't be built with MinGW -->
<!--cmake.generator>Visual Studio 17 2022</cmake.generator-->
<python.venv.bin>venv/Scripts/</python.venv.bin>
<python.exe.bin>python</python.exe.bin>
</properties>
</profile>
<!-- profile for windows aarch64 (mainly newer Mac or Raspberry PI Processors) (Self-Enabling) -->
Expand All @@ -766,6 +788,8 @@
<cmake.generator>MinGW Makefiles</cmake.generator>
<!-- The generated code relied on Boost and that relies on VS and can't be built with MinGW -->
<!--cmake.generator>Visual Studio 17 2022</cmake.generator-->
<python.venv.bin>venv/Scripts/</python.venv.bin>
<python.exe.bin>python</python.exe.bin>
</properties>
</profile>
<!-- Little helper profile that will disable running the cmake tests when the maven tests are being skipped -->
Expand Down
72 changes: 72 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# TsFile Python Document

<pre>
___________ ___________.__.__
\__ ___/____\_ _____/|__| | ____
| | / ___/| __) | | | _/ __ \
| | \___ \ | \ | | |_\ ___/
|____|/____ >\___ / |__|____/\___ > version 1.0.0
\/ \/ \/
</pre>


## Introduction

This directory contians the Python implementation of TsFile. The Python version of TsFile is implemented based on the CPP version. It utilizes the Cython package to integrate the read and write capabilities of TsFile CPP into the Python environment. Users can read and write TsFile just like they use read_csv and write_csv in Pandas.

The source code can be found in the `./tsfile` directory. Files ending with `.pyx` and `.pyd` are wrapper code written in Cython. The `tsfile/tsfile.py` defines some user interfaces. You can find some examples of reading and writing in the `.examples/examples.py`.


## How to make contributions

Using pylint to check Python code is recommended. However, there is no suitable style checking tool for Cython code, and this part of the code should be consistent with the Python style required by pylint.

**Feature List**
- [ ] In pywrapper, invoke the batch reading interface implemented in tsfile_CPP.



## Build

You can build tsfile_python by mvn or shell command.

Before constructing tsfile_python, it is necessary to build [tsfile_cpp](../cpp/README.md) first, because tsfile_python relies on the shared library files provided by tsfile_cpp.

Build by mvn in this directory:

```sh
mvn initialize compile test
```

Build by mvn in tsfile root directory:

```sh
mvn clean package -P with-python
```

Build by python command:
```sh
python3 setup.py build_ext --inplace
```

79 changes: 79 additions & 0 deletions python/examlpes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

import numpy as np
import pandas as pd
import os

import tsfile as ts


# test writing data
data_dir = os.path.join(os.path.dirname(__file__), "test.tsfile")
TABLE_NAME = "test_table"

# 1000 rows data
time = np.arange(1, 1001, dtype=np.int64)
level = np.linspace(2000, 3000, num=1000, dtype=np.float32)
num = np.arange(10000, 11000, dtype=np.int64)
df = pd.DataFrame({"Time": time, "level": level, "num": num})

if os.path.exists(data_dir):
os.remove(data_dir)
ts.write_tsfile(data_dir, TABLE_NAME, df)


# read data we already wrote
# with 20 chunksize
tsfile_ret = ts.read_tsfile(data_dir, TABLE_NAME, ["level", "num"], chunksize=20)
print(tsfile_ret.shape)

# # with 100 chunksize
tsfile_ret = ts.read_tsfile(data_dir, TABLE_NAME, ["level", "num"], chunksize = 100)
print(tsfile_ret.shape)

# # get all data
tsfile_ret = ts.read_tsfile(data_dir, TABLE_NAME, ["level", "num"])
print(tsfile_ret.shape)

# # with iterator
with ts.read_tsfile(data_dir, TABLE_NAME, ["level", "num"], iterator=True, chunksize=100) as reader:
for chunk in reader:
print(chunk.shape)

# # with time scale and chunksize
tsfile_ret = ts.read_tsfile(data_dir, TABLE_NAME,
["level"], start_time=50, end_time=100, chunksize=10)
print(tsfile_ret.shape)

# with time scale
tsfile_ret = ts.read_tsfile(data_dir, TABLE_NAME, ["num"], start_time=50, end_time=100)
print(tsfile_ret.shape)


with ts.read_tsfile(
data_dir,
TABLE_NAME,
["level", "num"],
iterator=True,
start_time=100,
end_time=500,
chunksize=100,
) as reader:
for chunk in reader:
print(chunk.shape)
133 changes: 133 additions & 0 deletions python/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile-parent</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>tsfile-python</artifactId>
<packaging>pom</packaging>
<name>TsFile: Python</name>
<properties>
<unity.version>2.6.0</unity.version>
<groovy.version>4.0.21</groovy.version>
<!-- Tell Sonar where to find the sources -->
<sonar.sources>tsfile</sonar.sources>
<sonar.cfamily.build-wrapper-output>${project.build.directory}/build-wrapper-output</sonar.cfamily.build-wrapper-output>
</properties>
<build>
<sourceDirectory>${project.basedir}</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<!-- Create python virtual environment -->
<execution>
<id>python-venv</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${python.exe.bin}</executable>
<arguments>
<argument>-m</argument>
<argument>venv</argument>
<argument>${project.basedir}/venv</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>python-upgrade-pip</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${python.venv.bin}${python.exe.bin}</executable>
<arguments>
<argument>-m</argument>
<argument>pip</argument>
<argument>install</argument>
<argument>--upgrade</argument>
<argument>pip</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>python-install-requirements</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${python.venv.bin}${python.exe.bin}</executable>
<arguments>
<argument>-m</argument>
<argument>pip</argument>
<argument>install</argument>
<argument>-r</argument>
<argument>${project.basedir}/requirements.txt</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>compile-python-code</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>python3</executable>
<arguments>
<argument>setup.py</argument>
<argument>build_ext</argument>
<argument>--inplace</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>run-python-tests</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${python.venv.bin}${python.exe.bin}</executable>
<arguments>
<argument>${project.basedir}/test.py</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 2c870d0

Please sign in to comment.