Skip to content

Commit

Permalink
Merge branch 'release/4.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dermatologist committed Nov 19, 2023
2 parents 1e26dd6 + 16203bf commit 9f45470
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 62 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Changelog

## [Unreleased](https://github.com/dermatologist/pyomop/tree/HEAD)
## [4.0.0](https://github.com/dermatologist/pyomop/tree/4.0.0) (2023-11-19)

[Full Changelog](https://github.com/dermatologist/pyomop/compare/3.2.0...HEAD)
[Full Changelog](https://github.com/dermatologist/pyomop/compare/3.2.0...4.0.0)

**Closed issues:**

Expand Down
107 changes: 55 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,67 @@ pip install pyomop
pip install -e .
```

## Usage >= 4.0.0 (Async)
## Usage >= 4.0.0 (Async) Example
```
from pyomop import CdmEngineFactory, CdmVocabulary, CdmVector, Cohort, Vocabulary, metadata
from sqlalchemy.future import select
import datetime
import asyncio
cdm = CdmEngineFactory() # Creates SQLite database by default
# Postgres example (db='mysql' also supported)
# cdm = CdmEngineFactory(db='pgsql', host='', port=5432,
# user='', pw='',
# name='', schema='cdm6')
engine = cdm.engine
# Create Tables if required
asyncio.run(cdm.init_models(metadata))
# Create vocabulary if required
vocab = CdmVocabulary(cdm)
# vocab.create_vocab('/path/to/csv/files') # Uncomment to load vocabulary csv files
# Add a cohort
async with cdm.session() as session:
async with session.begin():
session.add(Cohort(cohort_definition_id=2, subject_id=100,
cohort_end_date=datetime.datetime.now(),
cohort_start_date=datetime.datetime.now()))
await session.commit()
# Query the cohort
stmt = select(Cohort).where(Cohort.subject_id == 100)
result = await session.execute(stmt)
for row in result.scalars():
print(row)
assert row.subject_id == 100
# Query the cohort pattern 2
cohort = await session.get(Cohort, 1)
print(cohort)
assert cohort.subject_id == 100
# Close session
await session.close()
await engine.dispose()
# Convert result to a pandas dataframe
vec = CdmVector()
vec.result = result
print(vec.df.dtypes)
# Execute SQL statetements
result = await vec.sql_df(cdm, 'TEST') # TEST is defined in sqldict.py
for row in result:
print(row)
result = await vec.sql_df(cdm, query='SELECT * from cohort')
for row in result:
print(row)
async def main():
cdm = CdmEngineFactory() # Creates SQLite database by default
# Postgres example (db='mysql' also supported)
# cdm = CdmEngineFactory(db='pgsql', host='', port=5432,
# user='', pw='',
# name='', schema='cdm6')
engine = cdm.engine
# Create Tables if required
await cdm.init_models(metadata)
# Create vocabulary if required
vocab = CdmVocabulary(cdm)
# vocab.create_vocab('/path/to/csv/files') # Uncomment to load vocabulary csv files
# Add a cohort
async with cdm.session() as session:
async with session.begin():
session.add(Cohort(cohort_definition_id=2, subject_id=100,
cohort_end_date=datetime.datetime.now(),
cohort_start_date=datetime.datetime.now()))
await session.commit()
# Query the cohort
stmt = select(Cohort).where(Cohort.subject_id == 100)
result = await session.execute(stmt)
for row in result.scalars():
print(row)
assert row.subject_id == 100
# Query the cohort pattern 2
cohort = await session.get(Cohort, 1)
print(cohort)
assert cohort.subject_id == 100
# Convert result to a pandas dataframe
vec = CdmVector()
vec.result = result
print(vec.df.dtypes)
result = await vec.sql_df(cdm, 'TEST') # TEST is defined in sqldict.py
for row in result:
print(row)
result = await vec.sql_df(cdm, query='SELECT * from cohort')
for row in result:
print(row)
# Close session
await session.close()
await engine.dispose()
# Run the main function
asyncio.run(main())
```

## Usage <=3.2.0
Expand Down
8 changes: 4 additions & 4 deletions src/pyomop/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import click
import asyncio
from . import CdmEngineFactory
from . import CdmVocabulary
from . import metadata
Expand Down Expand Up @@ -29,12 +30,11 @@ def cli(verbose, create, dbtype, host, port, user, pw, name, schema, vocab):
print("verbose")
if create:
cdm = CdmEngineFactory(dbtype, host, port, user, pw, name, schema)
engine = cdm.engine
metadata.create_all(engine)
asyncio.run(cdm.init_models(metadata))
if vocab is not '':
cdm = CdmEngineFactory(dbtype, host, port, user, pw, name, schema)
vocab = CdmVocabulary(cdm)
vocab.create_vocab(vocab)
_vocab = CdmVocabulary(cdm)
_vocab.create_vocab(vocab)
print("Done")

def main_routine():
Expand Down
8 changes: 4 additions & 4 deletions t_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ async def main():
print(cohort)
assert cohort.subject_id == 100

# Close session
await session.close()
await engine.dispose()

# Convert result to a pandas dataframe
vec = CdmVector()
vec.result = result
Expand All @@ -55,5 +51,9 @@ async def main():
print(row)


# Close session
await session.close()
await engine.dispose()

# Run the main function
asyncio.run(main())

0 comments on commit 9f45470

Please sign in to comment.