@@ -163,8 +163,8 @@ class BaseRecord:
163163 extensions. If the argument contains any path delimiter
164164 characters, the argument will be interpreted as PATH/BASE_RECORD.
165165 Both relative and absolute paths are accepted. If `pn_dir` is set,
166- `record_name` should contain just the base record name , and the
167- files will be searched for remotely on PhysioNet . Otherwise,
166+ the files are read from PhysioNet , and folders in the database may be
167+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
168168 files are searched for locally, or when `record_name` is a
169169 supported cloud URI, they are read directly from cloud storage
170170 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -696,8 +696,8 @@ class Record(BaseRecord, _header.HeaderMixin, _signal.SignalMixin):
696696 extensions. If the argument contains any path delimiter
697697 characters, the argument will be interpreted as PATH/BASE_RECORD.
698698 Both relative and absolute paths are accepted. If `pn_dir` is set,
699- `record_name` should contain just the base record name , and the
700- files will be searched for remotely on PhysioNet . Otherwise,
699+ the files are read from PhysioNet , and folders in the database may be
700+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
701701 files are searched for locally, or when `record_name` is a
702702 supported cloud URI, they are read directly from cloud storage
703703 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -1083,8 +1083,8 @@ class MultiRecord(BaseRecord, _header.MultiHeaderMixin):
10831083 extensions. If the argument contains any path delimiter
10841084 characters, the argument will be interpreted as PATH/BASE_RECORD.
10851085 Both relative and absolute paths are accepted. If `pn_dir` is set,
1086- `record_name` should contain just the base record name , and the
1087- files will be searched for remotely on PhysioNet . Otherwise,
1086+ the files are read from PhysioNet , and folders in the database may be
1087+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
10881088 files are searched for locally, or when `record_name` is a
10891089 supported cloud URI, they are read directly from cloud storage
10901090 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -1830,6 +1830,44 @@ def check_np_array(item, field_name, ndim, parent_class, channel_num=None):
18301830# ------------------------- Reading Records --------------------------- #
18311831
18321832
1833+ def _resolve_pn_dir (record_name , pn_dir ):
1834+ """Find the PhysioNet directory and name of a record to read.
1835+
1836+ Adds the latest version of the database to `pn_dir` if it has no version,
1837+ and moves any folders in `record_name` to the end of `pn_dir`, so that
1838+ reading "files/p1/100" with pn_dir="demo" reads "100" with
1839+ pn_dir="demo/<latest version>/files/p1".
1840+
1841+ Parameters
1842+ ----------
1843+ record_name : str
1844+ The name of the record, which may start with folders in the database.
1845+ pn_dir : str
1846+ The PhysioNet database, with an optional version and folders.
1847+
1848+ Returns
1849+ -------
1850+ record_name : str
1851+ The name of the record, without folders.
1852+ pn_dir : str
1853+ The PhysioNet directory that holds the record.
1854+
1855+ """
1856+ if any (pn_dir .startswith (proto ) for proto in CLOUD_PROTOCOLS ):
1857+ raise ValueError (
1858+ "Cloud paths should be passed under record_name, not under pn_dir"
1859+ )
1860+ if "." not in pn_dir :
1861+ dir_list = pn_dir .split ("/" )
1862+ pn_dir = posixpath .join (
1863+ dir_list [0 ], download .get_version (dir_list [0 ]), * dir_list [1 :]
1864+ )
1865+ folders , record_name = posixpath .split (record_name )
1866+ if folders :
1867+ pn_dir = posixpath .join (pn_dir , folders )
1868+ return record_name , pn_dir
1869+
1870+
18331871def rdheader (record_name , pn_dir = None , rd_segments = False ):
18341872 """Read a WFDB header file and return a `Record` or `MultiRecord`.
18351873
@@ -1842,8 +1880,8 @@ def rdheader(record_name, pn_dir=None, rd_segments=False):
18421880 extensions. If the argument contains any path delimiter
18431881 characters, the argument will be interpreted as PATH/BASE_RECORD.
18441882 Both relative and absolute paths are accepted. If `pn_dir` is set,
1845- `record_name` should contain just the base record name , and the
1846- files will be searched for remotely on PhysioNet . Otherwise,
1883+ the files are read from PhysioNet , and folders in the database may be
1884+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
18471885 files are searched for locally, or when `record_name` is a
18481886 supported cloud URI, they are read directly from cloud storage
18491887 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -1893,18 +1931,8 @@ def rdheader(record_name, pn_dir=None, rd_segments=False):
18931931 # If the PhysioNet database path is provided, construct the download path using the
18941932 # database version
18951933 elif pn_dir is not None :
1896- # check to make sure a cloud path isn't being passed under pn_dir
1897- if any (pn_dir .startswith (proto ) for proto in CLOUD_PROTOCOLS ):
1898- raise ValueError (
1899- "Cloud paths should be passed under record_name, not under pn_dir"
1900- )
1901-
1902- if "." not in pn_dir :
1903- dir_list = pn_dir .split ("/" )
1904- pn_dir = posixpath .join (
1905- dir_list [0 ], download .get_version (dir_list [0 ]), * dir_list [1 :]
1906- )
1907-
1934+ base_record_name , pn_dir = _resolve_pn_dir (record_name , pn_dir )
1935+ file_name = f"{ base_record_name } .hea"
19081936 header_content = download ._stream_header (file_name , pn_dir )
19091937
19101938 # If it isn't a cloud path or a PhysioNet path, we treat as a local file
@@ -1953,6 +1981,8 @@ def _read_record_header(record_name, dir_name, pn_dir, wfdb_archive):
19531981 """
19541982 if wfdb_archive is not None :
19551983 return _parse_header_content (wfdb_archive .read_header (record_name ))
1984+ if pn_dir is not None :
1985+ return rdheader (record_name , pn_dir = pn_dir )
19561986 return rdheader (_join_path (dir_name , record_name ), pn_dir = pn_dir )
19571987
19581988
@@ -2075,8 +2105,8 @@ def rdrecord(
20752105 extensions. If the argument contains any path delimiter
20762106 characters, the argument will be interpreted as PATH/BASE_RECORD.
20772107 Both relative and absolute paths are accepted. If `pn_dir` is set,
2078- `record_name` should contain just the base record name , and the
2079- files will be searched for remotely on PhysioNet . Otherwise,
2108+ the files are read from PhysioNet , and folders in the database may be
2109+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
20802110 files are searched for locally, or when `record_name` is a
20812111 supported cloud URI, they are read directly from cloud storage
20822112 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -2222,16 +2252,7 @@ def _rdrecord(
22222252
22232253 # Read the header fields
22242254 if pn_dir is not None :
2225- # check to make sure a cloud path isn't being passed under pn_dir
2226- if any (pn_dir .startswith (proto ) for proto in CLOUD_PROTOCOLS ):
2227- raise ValueError (
2228- "Cloud paths should be passed under record_name, not under pn_dir"
2229- )
2230- if "." not in pn_dir :
2231- dir_list = pn_dir .split ("/" )
2232- pn_dir = posixpath .join (
2233- dir_list [0 ], download .get_version (dir_list [0 ]), * dir_list [1 :]
2234- )
2255+ record_name , pn_dir = _resolve_pn_dir (record_name , pn_dir )
22352256
22362257 if wfdb_archive is not None :
22372258 segment_name = None if is_archive_path (record_name ) else record_name
@@ -2422,8 +2443,12 @@ def _rdrecord(
24222443 wfdb_archive = wfdb_archive ,
24232444 )
24242445 else :
2446+ # On PhysioNet, the segments are in pn_dir, not in dir_name
2447+ segment_name = record .seg_name [seg_num ]
2448+ if pn_dir is None :
2449+ segment_name = _join_path (dir_name , segment_name )
24252450 record .segments [seg_num ] = rdrecord (
2426- _join_path ( dir_name , record . seg_name [ seg_num ]) ,
2451+ segment_name ,
24272452 sampfrom = seg_ranges [i ][0 ],
24282453 sampto = seg_ranges [i ][1 ],
24292454 channels = seg_channels [i ],
@@ -2479,8 +2504,8 @@ def rdsamp(
24792504 extensions. If the argument contains any path delimiter
24802505 characters, the argument will be interpreted as PATH/BASE_RECORD.
24812506 Both relative and absolute paths are accepted. If `pn_dir` is set,
2482- `record_name` should contain just the base record name , and the
2483- files will be searched for remotely on PhysioNet . Otherwise,
2507+ the files are read from PhysioNet , and folders in the database may be
2508+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
24842509 files are searched for locally, or when `record_name` is a
24852510 supported cloud URI, they are read directly from cloud storage
24862511 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -2601,8 +2626,8 @@ def sampfreq(record_name, pn_dir=None):
26012626 extensions. If the argument contains any path delimiter
26022627 characters, the argument will be interpreted as PATH/BASE_RECORD.
26032628 Both relative and absolute paths are accepted. If `pn_dir` is set,
2604- `record_name` should contain just the base record name , and the
2605- files will be searched for remotely on PhysioNet . Otherwise,
2629+ the files are read from PhysioNet , and folders in the database may be
2630+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
26062631 files are searched for locally, or when `record_name` is a
26072632 supported cloud URI, they are read directly from cloud storage
26082633 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -2653,8 +2678,8 @@ def signame(record_name, pn_dir=None, sig_nums=[]):
26532678 extensions. If the argument contains any path delimiter
26542679 characters, the argument will be interpreted as PATH/BASE_RECORD.
26552680 Both relative and absolute paths are accepted. If `pn_dir` is set,
2656- `record_name` should contain just the base record name , and the
2657- files will be searched for remotely on PhysioNet . Otherwise,
2681+ the files are read from PhysioNet , and folders in the database may be
2682+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
26582683 files are searched for locally, or when `record_name` is a
26592684 supported cloud URI, they are read directly from cloud storage
26602685 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -2714,8 +2739,8 @@ def wfdbdesc(record_name, pn_dir=None):
27142739 extensions. If the argument contains any path delimiter
27152740 characters, the argument will be interpreted as PATH/BASE_RECORD.
27162741 Both relative and absolute paths are accepted. If `pn_dir` is set,
2717- `record_name` should contain just the base record name , and the
2718- files will be searched for remotely on PhysioNet . Otherwise,
2742+ the files are read from PhysioNet , and folders in the database may be
2743+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
27192744 files are searched for locally, or when `record_name` is a
27202745 supported cloud URI, they are read directly from cloud storage
27212746 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
@@ -2850,8 +2875,8 @@ def wfdbtime(record_name, input_times, pn_dir=None):
28502875 extensions. If the argument contains any path delimiter
28512876 characters, the argument will be interpreted as PATH/BASE_RECORD.
28522877 Both relative and absolute paths are accepted. If `pn_dir` is set,
2853- `record_name` should contain just the base record name , and the
2854- files will be searched for remotely on PhysioNet . Otherwise,
2878+ the files are read from PhysioNet , and folders in the database may be
2879+ given in `record_name`, in `pn_dir`, or split between them . Otherwise,
28552880 files are searched for locally, or when `record_name` is a
28562881 supported cloud URI, they are read directly from cloud storage
28572882 via `fsspec`. Supported URIs: `s3://`, `gs://`, `az://`,
0 commit comments