Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activity schedule by location Mosaiq helper #1713

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

mchamberland
Copy link
Collaborator

No description provided.

Copy link
Member

@SimonBiggs SimonBiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

@SimonBiggs
Copy link
Member

We now have a means to have these helper functions be tested within. The PyMedPhys testing suite. See the following:

moe_patient_name = helpers.get_patient_name(connection, "MR8002")

Would you be able to include a test with this helper function?

@mchamberland
Copy link
Collaborator Author

@SimonBiggs Sure thing, I'll give it a shot.

@mchamberland
Copy link
Collaborator Author

mchamberland commented Aug 3, 2022

@SimonBiggs I will need some guidance, if you please.

It looks like the mimic Mosaiq database does not have a Schedule table, so I will need to add it with some dummy data for the test to work, right? How did you generate those CSV files with the dummy data for the tests?

@SimonBiggs
Copy link
Member

SimonBiggs commented Aug 3, 2022

@SimonBiggs I will need some guidance, if you please.

Of course. I believe I queried the relevant table filtered for a dummy physics patient to get a pandas data frame, and then saved the data frame to CSV using pandas.Dataframe.to_csv. I also had to pull out the types and put them into the TOML file. I'm now kicking myself that I didn't make a simple "Mosaiq to CSV and TOML" function.

If you're able to give it a shot with that info, let me know. Otherwise I can try and see what I can dig up. Unfortunately, I no longer have access to a Mosaiq instance, so I can't trial and error myself...

@mchamberland
Copy link
Collaborator Author

@SimonBiggs Thanks, that should help me get started. I'll report back in a couple of days once I've had time to test the waters.

@mchamberland
Copy link
Collaborator Author

@SimonBiggs I've added the types for the Schedule table to the types_map.toml, but every time I save the modified file in VS Code, VS Code goes around sorting the entries alphabetically and moves them around. I have no idea what's triggering this behavior. Any way to stop this?

@SimonBiggs
Copy link
Member

I have no idea what's triggering this behavior. Any way to stop this?

When I edit and save that toml file in VSCode mine doesn't auto-sort. I suspect it may be an extension you have on your machine? When I run code --list-extensions (link) on my machine I get the following list:

alygin.vscode-tlaplus
BazelBuild.vscode-bazel
bungcip.better-toml
CodeStream.codestream
esbenp.prettier-vscode
ExecutableBookProject.myst-highlight
GitHub.vscode-codeql
GitHub.vscode-pull-request-github
GrapeCity.gc-excelviewer
GraphQL.vscode-graphql
GraphQL.vscode-graphql-syntax
Gruntfuggly.todo-tree
hbenl.vscode-test-explorer
jakearl.search-editor-apply-changes
jasonnutter.vscode-codeowners
mhutchie.git-graph
micnil.vscode-checkpoints
ms-azuretools.vscode-docker
ms-dotnettools.csharp
ms-python.python
ms-python.vscode-pylance
ms-toolsai.jupyter
ms-toolsai.jupyter-keymap
ms-vscode-remote.remote-containers
ms-vscode-remote.remote-ssh
ms-vscode-remote.remote-ssh-edit
ms-vscode-remote.remote-wsl
ms-vscode.makefile-tools
ms-vscode.powershell
ms-vscode.test-adapter-converter
ms-vsliveshare.vsliveshare
nhoizey.gremlins
njpwerner.autodocstring
redhat.java
robocorp.robotframework-lsp
stkb.rewrap
streetsidesoftware.code-spell-checker
tomoki1207.pdf
trond-snekvik.simple-rst
Tyriar.sort-lines
VisualStudioExptTeam.vscodeintellicode
vscjava.vscode-java-debug
vscjava.vscode-java-dependency
vscjava.vscode-java-pack
vscjava.vscode-java-test
vscjava.vscode-maven
yzane.markdown-pdf
zxh404.vscode-proto3

In saying that though, as long as the sorting is staying constant within the toml objects, the column order isn't important. The main downside of the sorting would be the changelog being polluted during PR review.

@mchamberland
Copy link
Collaborator Author

It was worse than just sorting alphabetically within tables: it removed duplicate column names between tables.

Thanks for the tip: I disabled the only TOML extension I had (be5invis.toml) and installed the one you're using. Seems to behave better!

@SimonBiggs
Copy link
Member

it removed duplicate column names between tables.

Ewww, gross :(

@SimonBiggs
Copy link
Member

SimonBiggs commented Aug 5, 2022

Also, for that schedule table were you able to automatically extract that list from the SQL database? It might be worth adding a comment to the top of the file detailing the steps to extract new types. (Learning from my mistake last time, of not leaving enough breadcrumbs...)

@mchamberland
Copy link
Collaborator Author

@SimonBiggs unfortunately, no, I just used my data dictionary. I don’t know SQL to do fancy queries like that! But I just did a quick search online and found how to do it. I will double-check what I wrote against that and leave a comment.

I see your tables all end with RowVers in the TOML type map. That column isn’t in my data dictionary, but I added to my commit anyway. Not sure if it’s needed or not.

@sjswerdloff
Copy link
Collaborator

'RowVers' is a column that was incorporated for use by an ORM adopted by MOSAIQ.
The data dictionary probably doesn't mention it because it does not have semantics relevant to anything other than that ORM.
But to accurately match the schema... that's always the last column

@mchamberland mchamberland marked this pull request as draft August 11, 2022 17:58
@mchamberland
Copy link
Collaborator Author

Still working on this in my downtime.

@mchamberland
Copy link
Collaborator Author

@SimonBiggs I wrote a little get_column_data_types() method and placed it in helpers.py... but I'm not entirely sure if that's the best spot for it. Thoughts?

Also, a simple mosaiq_table_to_toml_types_map() could look something like:

def mosaiq_table_to_toml_types_map(connection, table_name, path):

    column_types = get_column_data_types(connection, table_name)

    column_types_dict = {
        table_name: pd.Series(column_types['data type'].values, index=column_types['column name']).to_dict()
    }

    with open(path, 'w') as f:
        toml.dump(column_types_dict, f)

Feedback? Where would you place this?

@SimonBiggs
Copy link
Member

SimonBiggs commented Aug 11, 2022 via email

@mchamberland
Copy link
Collaborator Author

Just an update to say this is still on my to do list. I've been busy with other stuff, but plan on finishing up those tests in the coming weeks.

@SimonBiggs
Copy link
Member

Hi @mchamberland,

Just wanting to check how things were going on your end with this. 🙂

@mchamberland
Copy link
Collaborator Author

@SimonBiggs still planning on coming back to this!

@SimonBiggs
Copy link
Member

Awesome :)

@SimonBiggs
Copy link
Member

Hi @mchamberland,

Just wanting to check in. Where do you think we might be up to on this PR?

@mchamberland
Copy link
Collaborator Author

@SimonBiggs I am sitting down and looking at where I left off the tests as I write this. Thank you for your patience! 😅

@SimonBiggs
Copy link
Member

Thank you for your patience!

Completely okay 🙂. Make sure the first thing you do is merge the current version of pymedphys/main. It's likely changed quite a bit since this began.

@mchamberland
Copy link
Collaborator Author

@SimonBiggs I wrote a test... but none of the test_mimicked_db.py tests pass. It seems like it's failing to setup the test database. Any suggestions?

(pymedphys) C:\Users\XXXXX\Documents\dev\pymedphys\lib\pymedphys\tests\mosaiq>pytest test_mimicked_db.py --mosaiqdb
Test session starts (platform: win32, Python 3.8.5, pytest 6.2.1, pytest-sugar 0.9.4)
rootdir: C:\Users\XXXXX\Documents\dev\pymedphys\lib\pymedphys\tests\mosaiq
plugins: hypothesis-5.49.0, rerunfailures-9.1.1, sugar-0.9.4
collecting ... 

―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ERROR at setup of test_get_patient_name ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― 

>   ???

src\pymssql.pyx:636:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

>   ???

src\_mssql.pyx:1964:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

>   ???

src\_mssql.pyx:682:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

>   ???
E   _mssql.MSSQLDatabaseException: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost)\nNet-Lib error during Unknown error (10061)\nDB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost)\nNet-Lib error during Unknown error (10061)\n')

src\_mssql.pyx:1690: MSSQLDatabaseException

During handling of the above exception, another exception occurred:

    @pytest.fixture(name="connection")
    def connection_base():
        """will create the test database, if it does not already exist on the instance"""
>       mimics.create_db_with_tables()

test_mimicked_db.py:44:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  
data\mimics.py:113: in create_db_with_tables
    mocks.check_create_test_db(database=DATABASE)
data\mocks.py:88: in check_create_test_db
    with pymssql.connect(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

>   ???
E   pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost)\nNet-Lib error during Unknown error (10061)\nDB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost)\nNet-Lib error during Unknown error (10061)\n')

src\pymssql.pyx:642: OperationalError
                                                                                                                                                                                                         11% █▎        


――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ERROR at setup of test_get_patient_fields ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― 

>   ???

src\pymssql.pyx:636:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

>   ???

src\_mssql.pyx:1964:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

>   ???

src\_mssql.pyx:682:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

>   ???
E   _mssql.MSSQLDatabaseException: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost)\nNet-Lib error during Unknown error (10061)\nDB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost)\nNet-Lib error during Unknown error (10061)\n')

src\_mssql.pyx:1690: MSSQLDatabaseException

During handling of the above exception, another exception occurred:

    @pytest.fixture(name="connection")
    def connection_base():
        """will create the test database, if it does not already exist on the instance"""
>       mimics.create_db_with_tables()

test_mimicked_db.py:44:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  
data\mimics.py:113: in create_db_with_tables
    mocks.check_create_test_db(database=DATABASE)
data\mocks.py:88: in check_create_test_db
    with pymssql.connect(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

>   ???
E   pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost)\nNet-Lib error during Unknown error (10061)\nDB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost)\nNet-Lib error during Unknown error (10061)\n')

src\pymssql.pyx:642: OperationalError

@mchamberland
Copy link
Collaborator Author

mchamberland commented Jul 26, 2023

@SimonBiggs Oh, do I need to install MySQL or something similar? I assume that's what's going on... Anything I should know on how to configure it (I'm not even sure I'll be allowed to install it on my work laptop...)

EDIT: Probably not it. Maybe I just need to try it on a computer where I have more rights...

@mchamberland
Copy link
Collaborator Author

Well... if I push my changes, the tests run on GitHub, but you probably don't want to see how many times it will take me to sort things out!

@mchamberland
Copy link
Collaborator Author

@SimonBiggs OK, some progress, but testing the column types against the types_map.toml fails because of the type casting workaround here.

I guess my test function should handle those specific edge cases...

@mchamberland
Copy link
Collaborator Author

I spent all morning unsuccessfully trying to get pymssql working on my M2 Mac. I give up.

I will rely on pushing to my branch to have GitHub run the tests for me. We can just squash those commits together when the PR is ready.

@sjswerdloff
Copy link
Collaborator

I spent all morning unsuccessfully trying to get pymssql working on my M2 Mac. I give up.

I will rely on pushing to my branch to have GitHub run the tests for me. We can just squash those commits together when the PR is ready.

What version of python are you using?
I have an M1 Mac, I use brew to install most of the tools one would typically need (you may need to get Xcode installed or updated).
I used pyenv to install 3.10.8, and then used pyenv virtualenv to create a clean workspace for pymedphys and fetched/pulled to latest main. I did a
poetry install -E all
and I got pymssql 2.2.7.
So... "get pymssql working" must be more than just getting it installed (when the M1 first came out, there weren't wheels for it so just getting a package installed was painful).

Is the problem connecting to your instance of SQL Server?
It is possible to get an instance of SQL Server running on Windows 11 ARM (in a VM on your Mac).
It's amazing how much one can get running on Windows 11 ARM in a VM on your Mac (Win11 ARM has it's own x86 equivalent to Rosetta2). Much easier than trying to use QEMU (don't ask).
There's a bit of hackery involved, but there's an alternative installation script on GitHub for installing SQL Server 2022 on Win11 ARM.
I haven't tried to use use pymssql in pymedphys, and to avoid conflict of interest issues, I'm not going to dive in to doing that with MOSAIQ.
But if you just want to work out the basic connection issues we can try to side-bar.
Typical issues are routing (easy if you have a VM on the same host), firewall (less anxiety provoking to bypass on an internal VM network), credentials in SQL Server (you can't use the Windows Authentication).

@sjswerdloff
Copy link
Collaborator

I spent all morning unsuccessfully trying to get pymssql working on my M2 Mac. I give up.
I will rely on pushing to my branch to have GitHub run the tests for me. We can just squash those commits together when the PR is ready.

Ah. I see:
poetry run pymedphys dev tests --run-only-mosaiqdb
raises a number of errors.
I'll take a look at building a wheel for pymssql and running it's self tests.

@sjswerdloff
Copy link
Collaborator

I spent all morning unsuccessfully trying to get pymssql working on my M2 Mac. I give up.
I will rely on pushing to my branch to have GitHub run the tests for me. We can just squash those commits together when the PR is ready.

Ah. I see: poetry run pymedphys dev tests --run-only-mosaiqdb raises a number of errors. I'll take a look at building a wheel for pymssql and running it's self tests.

Things broke in the last few weeks...
pymssql/pymssql#826

Previously, the solution was:
pymssql/pymssql#727 (comment)

And I think that's what was working for me at the time.
While I have occasionally dug in deep to make things work, this one is deeper than I'm prepared to attempt.
I imagine it will get fixed in the next few weeks, as it appears to also affect Docker containers and not just Apple Silicon.

@mchamberland
Copy link
Collaborator Author

mchamberland commented Aug 29, 2023

@SimonBiggs the RowVers type is originally timestamp. According to the TYPE_CASTING in mimics.py, this should become largebinary. But when I test against the mimicked Mosaiq DB, the type of RowVers somehow is varbinary.

What gives?

I had to put in an embarrassing workaround in my test. 🙈

@SimonBiggs
Copy link
Member

Hi @mchamberland,

I remember having quite a bit of pain here. But frustratingly I didn't leave enough breadcrumbs in the code detailing the choices.

If mimics is changed to have it be varbinary does that cause issues with the tests?

If not, then we should have it be like that.

@mchamberland
Copy link
Collaborator Author

@SimonBiggs Good idea. Somehow, I thought there were other columns with type timestamp, but it turns out RowVers is the only one. I will change mimics.py and see if the tests still run properly. Thanks!

@SimonBiggs
Copy link
Member

SimonBiggs commented Aug 29, 2023

Thanks @mchamberland :) 🙏

@mchamberland
Copy link
Collaborator Author

@SimonBiggs well, I tried, and it broke a number of things in ways that were very obscure to me. I reverted back to my workaround.

Will keep working on adding a few more tests for this new feature. Whew!

@SimonBiggs
Copy link
Member

Thanks 🙏 @mchamberland.

Could you leave a few more comments scattered through the code that help people (future us?) get a feel for a bit of the weirdness on this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants