Skip to content

Commit

Permalink
Conditionally add tensorflow dependent primitives to top level primit…
Browse files Browse the repository at this point in the history
…ives (#105)

* conditionally add USE to top level primitives

* check for both tensorflow and tensorflow_hub

* conditionally import both elmo and use

* test USE primitive in list_primitives

* fix indent

* Create optional_primitive_test.yml

* Update names of new CI tests
  • Loading branch information
rwedge authored Feb 28, 2022
1 parent 2f9cf16 commit dfde88e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/optional_primitive_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
on:
pull_request:
types: [opened, synchronize]
push:
branches:
- main

name: Optional Primitive Test
jobs:
optional_primitive_test:
name: Python ${{ matrix.python_version }} - Featuretools ${{ matrix.featuretools_version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python_version: ["3.7", "3.8", "3.9"]
featuretools_version: ["Release", "Main"]
steps:
- name: Set up python ${{ matrix.python_version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python_version }}
- name: Checkout repository
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Install standard requirements
run: |
pip config --site set global.progress_bar off
make installdeps
- if: ${{ matrix.featuretools_version == 'Main' }}
name: Install latest version of Featuretools from main branch
run: pip install git+https://github.com/alteryx/featuretools.git@main#egg=featuretools --force-reinstall
- name: Test optional primitives are not imported
run: python -c "import nlp_primitives; assert all(p not in dir(nlp_primitives) for p in ('Elmo', 'UniversalSentenceEncoder'))"
- name: Install complete requirements
run: make installdeps-complete
- if: ${{ matrix.featuretools_version == 'Main' }}
name: Install latest version of Featuretools from main branch
run: pip install git+https://github.com/alteryx/featuretools.git@main#egg=featuretools --force-reinstall
- name: Test optional primitives are imported
run: python -c "import nlp_primitives; assert all(p in dir(nlp_primitives) for p in ('Elmo', 'UniversalSentenceEncoder'))"
9 changes: 7 additions & 2 deletions nlp_primitives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import nltk.data

__version__ = '2.2.0'
from importlib.util import find_spec

import pkg_resources

from .count_string import CountString
Expand All @@ -15,12 +17,15 @@
from .polarity_score import PolarityScore
from .punctuation_count import PunctuationCount
from .stopword_count import StopwordCount
from .tensorflow.elmo import Elmo
from .tensorflow.universal_sentence_encoder import UniversalSentenceEncoder
from .title_word_count import TitleWordCount
from .total_word_length import TotalWordLength
from .upper_case_count import UpperCaseCount
from .whitespace_count import WhitespaceCount

if find_spec("tensorflow") and find_spec("tensorflow_hub"):
from .tensorflow.elmo import Elmo
from .tensorflow.universal_sentence_encoder import UniversalSentenceEncoder


nltk_data_path = pkg_resources.resource_filename('nlp_primitives', 'data/nltk-data/')
nltk.data.path.insert(0, nltk_data_path)
5 changes: 5 additions & 0 deletions nlp_primitives/tests/test_universal_sentence_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from woodwork.logical_types import NaturalLanguage

from ..tensorflow.universal_sentence_encoder import UniversalSentenceEncoder
from .test_utils import PRIMITIVES


def test_regular(universal_sentence_encoder):
Expand All @@ -27,6 +28,10 @@ def test_regular(universal_sentence_encoder):
np.testing.assert_array_almost_equal(a, b)


def test_name_in_primitive_list(universal_sentence_encoder):
assert PRIMITIVES.name.eq(universal_sentence_encoder.name).any()


@pytest.fixture()
def mock_remove_tensorflow():
# Simulate tensorflow being missing
Expand Down

0 comments on commit dfde88e

Please sign in to comment.