-
Notifications
You must be signed in to change notification settings - Fork 235
Allow passing in pandas dataframes to x2sys_cross #591
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
Changes from 18 commits
1d9e72a
e8cb8b6
a64f47f
9437850
c32870c
ef98ab5
01c5ee5
4132ff5
2c41ca4
aa3a521
fcd6cfe
17a2f3e
1446429
a2b08be
8efb415
1b658c7
8430dd7
b93a7aa
fb52df4
5338f37
bb22363
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |
| GMT supplementary X2SYS module for crossover analysis. | ||
| """ | ||
| import contextlib | ||
| import os | ||
| import re | ||
| from pathlib import Path | ||
|
|
||
| import pandas as pd | ||
|
|
||
|
|
@@ -14,10 +17,30 @@ | |
| dummy_context, | ||
| fmt_docstring, | ||
| kwargs_to_strings, | ||
| unique_name, | ||
| use_alias, | ||
| ) | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def tempfile_from_dftrack(track, suffix): | ||
| """ | ||
| Saves a pandas.DataFrame track table to a temporary tab-separated ASCII | ||
| text file with a suffix (e.g. 'xyz'). | ||
| """ | ||
| try: | ||
| tmpfilename = f"track-{unique_name()[:7]}.{suffix}" | ||
| track.to_csv( | ||
| path_or_buf=tmpfilename, | ||
| sep="\t", | ||
| index=False, | ||
| date_format="%Y-%m-%dT%H:%M:%S.%fZ", | ||
| ) | ||
| yield tmpfilename | ||
| finally: | ||
| os.remove(tmpfilename) | ||
|
|
||
|
|
||
| @fmt_docstring | ||
| @use_alias( | ||
| D="fmtfile", | ||
|
|
@@ -263,8 +286,23 @@ def x2sys_cross(tracks=None, outfile=None, **kwargs): | |
| if kind == "file": | ||
| file_contexts.append(dummy_context(track)) | ||
| elif kind == "matrix": | ||
| raise NotImplementedError(f"{type(track)} inputs are not supported yet") | ||
| # file_contexts.append(lib.virtualfile_from_matrix(track.values)) | ||
| # find suffix (-E) of trackfiles used (e.g. xyz, csv, etc) from | ||
| # $X2SYS_HOME/TAGNAME/TAGNAME.tag file using regex search | ||
| lastline = ( | ||
| Path(os.environ["X2SYS_HOME"], kwargs["T"], f"{kwargs['T']}.tag") | ||
| .read_text() | ||
| .strip() | ||
| .split("\n")[-1] | ||
| ) # e.g. "-Dxyz -Etsv -I1/1" | ||
| try: | ||
| # 1st try to match file extension after -E | ||
| suffix = re.search(pattern=r"-E(\S*)", string=lastline).group(1) | ||
| except AttributeError: # 'NoneType' object has no attribute 'group' | ||
| # 2nd try to match file extension after -D | ||
| suffix = re.search(pattern=r"-D(\S*)", string=lastline).group(1) | ||
|
||
|
|
||
| # Save pandas.DataFrame track data to temporary file | ||
| file_contexts.append(tempfile_from_dftrack(track=track, suffix=suffix)) | ||
| else: | ||
| raise GMTInvalidInput(f"Unrecognized data type: {type(track)}") | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original implementation using GMTTempFile/NamedTemporaryFile didn't work because of some permissions issues (on macOS/Windows), which is why this try-finally block is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code quality looks good. As you're the one who develops and uses these functions, we have to trust you. 😄
Just one suggestion, add the comment to the codes, explaining why you use
unique_namehere.That's the first question when I read your codes before I see your comment here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's all Paul's work done a decade ago, I'm just wrapping it in Python so more people can use it easily 😃 You won't believe how many 'crossover analysis' tools have been written again and again, but that's another story.
Ok, will do.