Skip to content

Commit 0280ed8

Browse files
committed
bump to version 0.1.1 with automatic publish on pypi and fix some tests
1 parent 65e634c commit 0280ed8

6 files changed

Lines changed: 227 additions & 5 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This workflow will upload a Python Package using Twine when a release is created
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
3+
4+
# This workflow uses actions that are not certified by GitHub.
5+
# They are provided by a third-party and are governed by
6+
# separate terms of service, privacy policy, and support
7+
# documentation.
8+
9+
name: Upload Python Package
10+
11+
on:
12+
release:
13+
types: [published]
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
deploy:
20+
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- uses: actions/checkout@v3
25+
- name: Set up Python
26+
uses: actions/setup-python@v3
27+
with:
28+
python-version: '3.10'
29+
- name: Install dependencies
30+
run: |
31+
python -m pip install --upgrade pip
32+
pip install build
33+
- name: Build package
34+
run: python -m build
35+
- name: Install built package
36+
run: |
37+
pip install tensorflow
38+
find dist/ -name leap_net\*.whl -type f -exec pip install {}[test] \;
39+
# If you install TensorFlow, critically, you should reinstall Keras 3 afterwards.
40+
# This is a temporary step while TensorFlow is pinned to Keras 2, and will no
41+
# longer be necessary after TensorFlow 2.16. The cause is that tensorflow==2.15
42+
# will overwrite your Keras installation with keras==2.15.
43+
- name: test package
44+
run: |
45+
cd leap_net/test
46+
python -m unittest discover
47+
- name: Publish package
48+
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
49+
with:
50+
user: __token__
51+
password: ${{ secrets.PYPI_TOKEN }}

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Change Log
22
===========
33

4+
[0.1.1] - 2024-02-21
5+
--------------------
6+
- [FIXED] Broken tests
7+
- [ADDED] test of both keras v3 and tf_keras implementation (when appropriate)
8+
(*eg* not for python 3.8 where keras v3 is not available)
9+
- [ADDED] automatic upload on pypi on new version
10+
411
[0.1.0] - 2024-01-15
512
----------------------
613
- [BREAKING] refactoring of the code to use keras >= 3.0 (compatible with

leap_net/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# SPDX-License-Identifier: MPL-2.0
77
# This file is part of leap_net, leap_net a keras implementation of the LEAP Net model.
88

9-
__version__ = "0.1.0"
9+
__version__ = "0.1.1"
1010
__all__ = []
1111

1212
try:
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
2+
# See AUTHORS.txt
3+
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
4+
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
5+
# you can obtain one at http://mozilla.org/MPL/2.0/.
6+
# SPDX-License-Identifier: MPL-2.0
7+
# This file is part of leap_net, leap_net a keras implementation of the LEAP Net model.
8+
9+
import logging
10+
import os
11+
import numpy as np
12+
import unittest
13+
logging.disable(logging.WARNING)
14+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
15+
16+
try:
17+
import keras
18+
from keras.layers import Input
19+
from keras.models import Model
20+
from leap_net.keras import Ltau
21+
except ImportError:
22+
pass
23+
24+
import pdb
25+
26+
import sys
27+
28+
29+
class Test(unittest.TestCase):
30+
def setUp(self):
31+
if sys.version_info.major == 3 and sys.version_info.minor == 8:
32+
self.skipTest("Keras v3 not available on python 3.8")
33+
self.tol = 1e-5 # use to compare results that should be strictly equal, up to numerical error
34+
self.tol_learn = 1e-2 # use to compare results from a test set
35+
36+
# to have "reproducible" results
37+
np.random.seed(1)
38+
keras.utils.set_random_seed(1)
39+
40+
def test_ok_tau0(self):
41+
dim_x = 10
42+
n_elem = 5
43+
dim_tau = 1
44+
45+
x = Input(shape=(dim_x,), name="x")
46+
tau = Input(shape=(dim_tau,), name="tau")
47+
48+
res_model = Ltau()((x, tau))
49+
model = Model(inputs=[x, tau], outputs=[res_model])
50+
51+
X_train = np.random.normal(size=(n_elem, dim_x)).astype(np.float32)
52+
TAU_train = np.zeros(shape=(n_elem, dim_tau), dtype=np.float32)
53+
res = model.predict([X_train, TAU_train])
54+
assert np.all(res == X_train)
55+
56+
def test_ok_tau1(self):
57+
dim_x = 10
58+
n_elem = 100
59+
dim_tau = 1
60+
X_train = np.random.normal(size=(n_elem, dim_x)).astype(np.float32)
61+
TAU_train = np.ones(shape=(n_elem, dim_tau), dtype=np.float32)
62+
63+
# the keras model
64+
x = Input(shape=(dim_x,), name="x")
65+
tau = Input(shape=(dim_tau,), name="tau")
66+
res_model = Ltau(initializer='ones', use_bias=False)((x, tau))
67+
model = Model(inputs=[x, tau], outputs=[res_model])
68+
69+
# make predictions
70+
res = model.predict([X_train, TAU_train])
71+
72+
# LEAP Net implementation in numpy in case tau is not 0
73+
res_th = np.matmul(X_train, np.ones((dim_x, dim_tau), dtype=np.float32))
74+
res_th = np.multiply(res_th, TAU_train)
75+
res_th = np.matmul(res_th, np.ones((dim_tau, dim_x), dtype=np.float32))
76+
res_th += X_train
77+
assert np.mean(np.abs(res - res_th)) <= self.tol, "problem with l1"
78+
assert np.max(np.abs(res - res_th)) <= self.tol, "problem with linf"
79+
80+
def test_ok_tau_rand(self):
81+
dim_x = 10
82+
n_elem = 100
83+
dim_tau = 20
84+
85+
X_train = np.random.normal(size=(n_elem, dim_x)).astype(np.float32)
86+
TAU_train = np.random.normal(size=(n_elem, dim_tau)).astype(np.float32)
87+
88+
# the keras model
89+
x = Input(shape=(dim_x,), name="x")
90+
tau = Input(shape=(dim_tau,), name="tau")
91+
res_model = Ltau(initializer='ones', use_bias=False)((x, tau))
92+
model = Model(inputs=[x, tau], outputs=[res_model])
93+
94+
# make predictions
95+
res = model.predict([X_train, TAU_train])
96+
97+
# LEAP Net implementation in numpy in case tau is not 0
98+
res_th = np.matmul(X_train, np.ones((dim_x, dim_tau), dtype=np.float32))
99+
res_th = np.multiply(res_th, TAU_train)
100+
res_th = np.matmul(res_th, np.ones((dim_tau, dim_x), dtype=np.float32))
101+
res_th += X_train
102+
assert np.mean(np.abs(res - res_th)) <= self.tol, "problem with l1"
103+
assert np.max(np.abs(res - res_th)) <= self.tol, "problem with linf"
104+
105+
def test_can_learn(self):
106+
dim_x = 30
107+
n_elem = 32*32
108+
dim_tau = 5
109+
110+
X_train = np.random.normal(size=(n_elem, dim_x)).astype(np.float32)
111+
TAU_train = np.random.normal(size=(n_elem, dim_tau)).astype(np.float32)
112+
113+
e = np.random.normal(size=(dim_x, dim_tau)).astype(np.float32)
114+
d = np.random.normal(size=(dim_tau, dim_x)).astype(np.float32)
115+
116+
Y_train = np.matmul(X_train, e)
117+
Y_train = np.multiply(Y_train, TAU_train)
118+
Y_train = np.matmul(Y_train, d)
119+
Y_train += X_train
120+
121+
# the keras model
122+
x = Input(shape=(dim_x,), name="x")
123+
tau = Input(shape=(dim_tau,), name="tau")
124+
res_model = Ltau()((x, tau))
125+
model = Model(inputs=[x, tau], outputs=[res_model])
126+
127+
adam_ = keras.optimizers.Adam(learning_rate=1e-3)
128+
model.compile(optimizer=adam_, loss='mse')
129+
## train it
130+
model.fit(x=[X_train, TAU_train], y=[Y_train], epochs=200, batch_size=32, verbose=False)
131+
132+
# test it has learn something relevant
133+
X_test = np.random.normal(size=(n_elem, dim_x)).astype(np.float32)
134+
TAU_test = np.random.normal(size=(n_elem, dim_tau)).astype(np.float32)
135+
Y_test = np.matmul(X_test, e)
136+
Y_test = np.multiply(Y_test, TAU_test)
137+
Y_test = np.matmul(Y_test, d)
138+
Y_test += X_test
139+
res = model.predict([X_test, TAU_test])
140+
assert np.mean(np.abs(res - Y_test)) <= self.tol_learn, "problem with l1"
141+
assert np.max(np.abs(res - Y_test)) <= self.tol_learn, "problem with linf"
142+
143+
# TODO test saving / loading
144+
# TODO test name and graph visualizing
145+
# TODO test resnet too
146+
147+
148+
if __name__ == "__main__":
149+
unittest.main()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from tensorflow.keras.layers import Input
1818
from tensorflow.keras.models import Model
1919

20-
from leap_net import Ltau
20+
from leap_net.tf_keras import Ltau
2121
import pdb
2222

2323

@@ -117,7 +117,7 @@ def test_can_learn(self):
117117
res_model = Ltau()((x, tau))
118118
model = Model(inputs=[x, tau], outputs=[res_model])
119119

120-
adam_ = tf.optimizers.Adam(lr=1e-3)
120+
adam_ = tf.optimizers.Adam(learning_rate=1e-3)
121121
model.compile(optimizer=adam_, loss='mse')
122122
## train it
123123
model.fit(x=[X_train, TAU_train], y=[Y_train], epochs=200, batch_size=32, verbose=False)

setup.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# SPDX-License-Identifier: MPL-2.0
77
# This file is part of leap_net, leap_net a keras implementation of the LEAP Net model.
88

9+
import sys
910
import setuptools
1011
from setuptools import setup
1112

@@ -21,17 +22,31 @@
2122
"grid2op",
2223
"pandas",
2324
"tqdm",
24-
'sklearn',
25+
'scikit-learn',
2526
"tensorflow"
2627
]
2728
}
2829
}
2930

31+
if sys.version_info.major == 3 and sys.version_info.minor == 8:
32+
# no keras v3 in python 3.8
33+
pkgs["required"] = [el for el in pkgs["required"] if not "keras" in el]
34+
pkgs["required"].append("tensorflow")
35+
36+
37+
pkgs["extras"]["test"] = [el for el in pkgs["extras"]["recommended"] if not "tensorflow" in el]
38+
# from here https://keras.io/getting_started/
39+
# If you install TensorFlow, critically, you should reinstall Keras 3 afterwards.
40+
# This is a temporary step while TensorFlow is pinned to Keras 2,
41+
# and will no longer be necessary after TensorFlow 2.16. The cause is that tensorflow==2.15
42+
# will overwrite your Keras installation with keras==2.15.
43+
# This is why in the tests I just skip tensorflow for now. Will be fixed later
44+
3045
with open("README.md", "r", encoding="utf-8") as fh:
3146
long_description = fh.read()
3247

3348
setup(name='leap_net',
34-
version='0.1.0',
49+
version='0.1.1',
3550
description='An implementation in keras 3.0 (and tensorflow keras) of the LeapNet model',
3651
long_description=long_description,
3752
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)