#4 WIP: created blogs folder ,added Python标准库之os.md file and modified medpy_third_lib.ipynb

Merged
AlbertDarren merged 2 commits from dev into master 1 year ago
  1. +433
    -0
      blogs/Python标准库之os.md
  2. +274
    -40
      medpy_third_lib.ipynb

+ 433
- 0
blogs/Python标准库之os.md View File

@@ -0,0 +1,433 @@
# 1. OS标准库简介

顾名思义,OS表示Operating System,即操作系统。OS标准库是一个操作系统接口模块,提供一些方便使用操作系统相关功能的函数,具体安装位置可通过导入os模块查看`os.__file__`属性得到。当需要在Python代码中调用OS相关功能实现业务逻辑或者无法直接使用命令行工具时,我们就需要考虑导入此模块,因此有必要进行深入学习。

# 2. OS标准库常用函数和属性

## 2.1 文件和目录

### 2.1.1 `os.getcwd()`

返回表示当前工作目录的字符串

```python
print("当前工作目录为:{}".format(os.getcwd())) # 返回当前工作目录
```

![](Python标准库之os.assets/os.getcwd.JPG)

### 2.1.2 `os.mkdir(path, mode=0o777, *, dir_fd=None)`

以指定数字表示的权限模式mode创建一个名为path的目录。某些系统会忽略 mode,如果没有忽略,那么Linux系统来说,新建文件夹的权限=指定数字表示的权限模式mode-当前系统用户的umask默认权限,如下所示

```python
"""
Linux操作系统可通过umask命令获得4个八进制数表示的默认权限,root用户默认是0022,普通用户默认是 0002
第1位数代表文件所具有的特殊权限(SetUID、SetGID、Sticky BIT),后3位数表示表示umask权限值
分别对应所有者、用户组、其他人的权限值,权限与数字对应关系为:r->4,w->2,x->1
"""
exit_code=os.system("umask")
```

![](Python标准库之os.assets/umask.JPG)

```python
"""
文件夹模式mode赋值为十进制511,等价于八进制0o777
"""
set_mode=511
os.mkdir(path="./cyr",mode=set_mode) # 在当前目录创建名为cyr的文件夹
```

```python
# 长格式查看新创建的文件夹cyr可知其权限字符串为rwxr-xr-x,等价于转换后的数字权限111101101
!ls -l | grep cyr
```

![](Python标准库之os.assets/新建文件夹cyr权限.JPG)

```python
umask_value=0o0022 # 当前系统用户八进制表示umask默认权限
new_dir_mode=set_mode-umask_value
print("新建文件夹的权限为:{:b}".format(new_dir_mode))
```

![](Python标准库之os.assets/新建文件夹cyr权限验证.JPG)

+ `os.rmdir(path, *, dir_fd=None)`

移除(删除)目录 *path*。如果目录不存在或不为空,则会分别抛出 [`FileNotFoundError`](https://docs.python.org/zh-cn/3.7/library/exceptions.html#FileNotFoundError) 或 [`OSError`](https://docs.python.org/zh-cn/3.7/library/exceptions.html#OSError) 异常。

```python
os.rmdir("./cyr") # 删除空文件夹成功,无法查到cyr目录
!ls | grep cyr
```

```python
os.rmdir("./why") # 删除不存在的文件夹FileNotFoundError报错
```

![](Python标准库之os.assets/删除不存在文件夹报错.JPG)

```python
os.rmdir("./nnunet/") # 删除不为空文件夹OSError报错
```

![](Python标准库之os.assets/删除不为空文件夹OSError报错.JPG)

+ `os.chdir(path)`

将当前工作目录更改为 path

```python
print("切换前的当前工作目录为:{}".format(os.getcwd())) # 返回当前工作目录
dst_path="/root" # 目标文件夹
os.chdir(dst_path) # 将当前工作目录切换为/root
print("切换后的当前工作目录为:{}".format(os.getcwd())) # 返回当前工作目录
```

![](Python标准库之os.assets/os.chdir.JPG)

+ `os.listdir(path='.')`

返回一个包含指定path下所有文件和目录名称的按任意顺序排列的列表,特殊条目'.'和'..'除外

```python
dst_path="/code/" # 目标目录
dirs_ls=os.listdir(path=dst_path) # 获得指定目录下全部文件和文件夹名称列表
print(dirs_ls)
```

![](Python标准库之os.assets/os.listdir.JPG)


## 2.2 os.path常见路径操作

### 2.2.1 `os.path.abspath(path)`

返回路径path 的绝对路径(标准化的),相当于字符串拼接,路径path不存在也不会报错

```python
relative_path="tests/test_steps_for_sliding_window_prediction.py" # 路径path存在
print("{}对应的绝对路径为{}".format(relative_path,os.path.abspath(relative_path)))
```

![](Python标准库之os.assets/os.path.abspath.JPG)

```python
no_path="tests/none.py" # 路径path不存在
print("{}对应的绝对路径为{}".format(relative_path,os.path.abspath(no_path)))
```

![](Python标准库之os.assets/os.path.abspathno.JPG)

### 2.2.2 `os.path.basename(path)`

返回路径 path 的基本名称

```python
full_pathname="/proc/bus/pci/3a/08.0" # 路径path存在
print("全路径名称对应的文件名为{}".format(os.path.basename(full_pathname)))
```

![](Python标准库之os.assets/os.path.basename.JPG)

```python
no_full_pathname="/demo/none.cpp" # 路径path不存在
print("全路径名称对应的文件名为{}".format(os.path.basename(no_full_pathname)))
```

![image-20221126180414744](Python标准库之os.assets/image-20221126180414744.png)

### 2.2.3 `os.path.dirname(path)`

返回路径 path 的目录名称

```python
full_pathname="/proc/bus/pci/3a/08.0" # 路径path存在
print("全路径名称对应的目录名称为{}".format(os.path.dirname(full_pathname)))
```

![](Python标准库之os.assets/os.path.dirname.JPG)

```python
no_full_pathname="/demo/none.cpp" # 路径path不存在
print("全路径名称对应的目录名称为{}".format(os.path.dirname(no_full_pathname)))
```

![](Python标准库之os.assets/os.path.dirnameno.JPG)

### 2.2.4 `os.path.exists(path)`

判断path是否指向一个已存在路径或已打开的文件描述符,存在返回True,不存在返回False

```python
full_pathname="/proc/bus/pci/3a/08.0" # 路径path存在
print("全路径名称对应的目录是否存在?{}".format(os.path.exists(full_pathname)))
```

![](Python标准库之os.assets/os.path.exists.JPG)

```python
no_full_pathname="/demo/none.cpp" # 路径path不存在
print("全路径名称对应的目录是否存在?{}".format(os.path.exists(no_full_pathname)))
```

![](Python标准库之os.assets/os.path.existsno.JPG)

### 2.2.5 `os.path.isabs(path)`

判断path是否为一个绝对路径,是则返回True,不是或不存在则返回False。在 Unix 上,它就是以斜杠开头,而在 Windows 上,它可以是去掉驱动器号后以斜杠(或反斜杠)开头。

```python
abs_pathname="/proc/bus/pci/3a/08.0" # 路径path存在
print("全路径名称对应的目录是否为绝对路径?{}".format(os.path.isabs(abs_pathname)))
```
![image-20221126193214609](Python标准库之os.assets/image-20221126193214609.png)
```python
rel_pathname="./nnunet/__init__.py" # 路径path是相对路径
print("全路径名称对应的目录是否绝对路径?{}".format(os.path.isabs(rel_pathname)))
```

![image-20221126193232255](Python标准库之os.assets/image-20221126193232255.png)

```python
no_pathname="./nnunet/none.py" # 路径path是不存在
print("全路径名称对应的目录是否绝对路径?{}".format(os.path.isabs(no_pathname)))
```

![image-20221126193244714](Python标准库之os.assets/image-20221126193244714.png)

### 2.2.6 `os.path.isfile(path)`

若path为指向一个已存在文件的符号链接或一个已存在文件路径,返回True。若path为一个文件夹路径或不存在路径,返回False。

```shell
ls -li /opt/conda/bin/python* # 带inode节点信息并长格式查看python开头的文件和文件夹
```

![](Python标准库之os.assets/ls_li.JPG)

由上图可发现/opt/conda/bin/python为一个符号链接(软链接)指向一个已存在文件路径/opt/conda/bin/python3.7

```python
abs_pathname="/opt/conda/bin/python3.7" # path为一个已存在文件路径
print("全路径名称对应的文件是否存在?{}".format(os.path.isfile(abs_pathname)))
```

![](Python标准库之os.assets/os.path.isfiletrue.JPG)

```python
symbolic_link="/opt/conda/bin/python" # path为指向一个已存在文件/opt/conda/bin/python3.7的符号链接
print("全路径名称对应的文件是否存在?{}".format(os.path.isfile(symbolic_link)))
```

![](Python标准库之os.assets/os.path.isfiletrue-16695128425931.JPG)

```python
abs_path="/opt/conda/bin/" # 文件夹路径
print("全路径名称对应的文件是否存在?{}".format(os.path.isfile(abs_path)))
```

![image-20221127093432253](Python标准库之os.assets/image-20221127093432253.png)

```python
no_full_pathname="/demo/none.cpp" # 路径path不存在
print("全路径名称对应的文件是否存在?{}".format(os.path.isfile(no_full_pathname)))
```

![image-20221127093502677](Python标准库之os.assets/image-20221127093502677.png)

### 2.2.7 `os.path.isdir(path)`

若path为指向一个已存在文件夹的符号链接或一个已存在文件夹路径,返回True。若path为一个文件路径或不存在路径,返回False。

```shell
ls /code/nnunet/ # 查看已存在文件夹路径/code/nnunet/
```
![image-20221127110800609](Python标准库之os.assets/image-20221127110800609.png)

```shell
ln -s /code/nnunet/ ./symlink2codennunet # 当前目录即root下创建一个软链接指向一个已存在文件夹路径/code/nnunet/
```

```shell
ls -l /root/
```

![image-20221127110142399](Python标准库之os.assets/image-20221127110142399.png)

由上图可知root用户主目录下存在一个软链接symlink2codennunet指向一个已存在文件夹路径

```python
exist_dir_path="/code/nnunet/"# path为一个已存在文件夹路径
print("全路径名称对应的文件夹是否存在?{}".format(os.path.isdir(exist_dir_path)))
```

![image-20221127111107995](Python标准库之os.assets/image-20221127111107995.png)

```python
exist_dir_symlink="/root/symlink2codennunet/"# path为指向一个已存在文件夹的符号链接
print("全路径名称对应的文件夹是否存在?{}".format(os.path.isdir(exist_dir_symlink)))
```

![image-20221127111131025](Python标准库之os.assets/image-20221127111131025.png)

```python
exist_file_path="/opt/conda/bin/python3.7"# path为一个已存在文件路径
print("全路径名称对应的文件夹是否存在?{}".format(os.path.isdir(exist_file_path)))
```

![image-20221127111201285](Python标准库之os.assets/image-20221127111201285.png)

```python
no_path="/demo/none.cpp" # 路径path不存在
print("全路径名称对应的文件夹是否存在?{}".format(os.path.isdir(no_path)))
```

![image-20221127111218903](Python标准库之os.assets/image-20221127111218903.png)

### 2.2.8 `os.path.islink(path)`

若path代表一个已存在的符号链接,则返回True,反之则返回False。如果 Python 运行时不支持符号链接,则总是返回 False

```python
exist_symbolic_link="/opt/conda/bin/python" # path为指向一个已存在的符号链接
print("全路径名称对应的符号链接是否存在?{}".format(os.path.islink(exist_symbolic_link)))
```

![image-20221127135410586](Python标准库之os.assets/image-20221127135410586.png)

```python
no_symbolic_link="/demo/no_link" # path为指向一个不存在的符号链接
print("全路径名称对应的符号链接是否存在?{}".format(os.path.islink(no_symbolic_link)))
```

![image-20221127135440128](Python标准库之os.assets/image-20221127135440128.png)

```python
exist_file_path="/opt/conda/bin/python3.7"# path为一个已存在文件路径
print("全路径名称对应的符号链接是否存在?{}".format(os.path.islink(exist_file_path)))
```

![image-20221127135526214](Python标准库之os.assets/image-20221127135526214.png)

```python
exist_dir_path="/root/"# path为一个已存在文件夹路径
print("全路径名称对应的符号链接是否存在?{}".format(os.path.islink(exist_dir_path)))
```

![image-20221127135543707](Python标准库之os.assets/image-20221127135543707.png)

### 2.2.9 `os.path.join(path, *paths)`

拼接两个或多个路径部分,按需要插入`/`。如果参数中某个部分是绝对路径,则绝对路径前的路径都将被丢弃,并从绝对路径部分开始连接。如果最后一部分为空,则结果将以分隔符结尾。

```python
previous_path,abs_dirname,basename,empty_part="model","/code","demo.py",""
```

```python
print("参数中某个部分是绝对路径拼接后为{}".format(os.path.join(previous_path,abs_dirname,basename)))
```

![image-20221127141227036](Python标准库之os.assets/image-20221127141227036.png)

```python
print("拼接两个或多个路径部分,按需要插入'/'拼接后为{}".format(os.path.join(previous_path,basename)))
```

![image-20221127141254552](Python标准库之os.assets/image-20221127141254552.png)

```python
print("最后一部分为空以分隔符结尾{}".format(os.path.join(previous_path,basename,empty_part)))
```

![image-20221127141326629](Python标准库之os.assets/image-20221127141326629.png)

### 2.2.10 `os.path.normcase(path)`

规范路径名称的大小写。 在 Windows 上,将路径名称中的所有字符转为小写,并将正斜杠转为反斜杠。 在其他操作系统上,将路径不加修改地返回。

Linux操作系统

```python
print("当前操作系统模块名为:{}".format(os.name))
windows_style_path=r"C:/Users\defaultuser0/APPData"
print("Windows路径规范化后为{}".format(os.path.normcase(windows_style_path)))
```

![image-20221127142558417](Python标准库之os.assets/image-20221127142558417.png)

Windows操作系统

![image-20221127143000287](Python标准库之os.assets/image-20221127143000287.png)

### 2.2.11 `os.path.split(path)`

将路径 path 拆分为一对,即 (head, tail),其中,tail 是路径的最后一部分,而 head 里是除最后部分外的所有内容。tail 部分不会包含斜杠,如果 path 以斜杠结尾,则 tail 将为空。如果 path 中没有斜杠,head 将为空。如果 path 为空,则 head 和 tail 均为空。head 末尾的斜杠会被去掉,除非它是根目录(即它仅包含一个或多个斜杠)。

```python
norm_path="/nnunet/configuration.py" # 一般路径
ends_with_slash_path="/code/nnunet/" # 以斜杠结尾的路径
no_slash_path="HIP_Logo.png" # 没有斜杠的路径
empty_path="" # 空路径
root_path="/" # 根目录
print("一般路径head={},tail={}".format(*os.path.split(norm_path)))
print("以斜杠结尾的路径head={},tail={}".format(*os.path.split(ends_with_slash_path)))
print("没有斜杠的路径head={},tail={}".format(*os.path.split(no_slash_path)))
print("空路径head={},tail={}".format(*os.path.split(empty_path)))
print("根目录head={},tail={}".format(*os.path.split(root_path)))
```

![image-20221127152957405](Python标准库之os.assets/image-20221127152957405.png)

### 2.2.12 `os.path.splitext(path)`

将路径 path 拆分为一对,即 (root, ext),使 root + ext == path,其中 ext 为空或以英文句点开头,且最多包含一个句点。路径前的句点将被忽略,例如 splitext('.cshrc') 返回 ('.cshrc', '')。

```python
dir_path="/code/nnunet/" # 文件夹路径
multi_dot_file_path="/code/i.thy.py" # 包含多个句点的文件路径
single_dot_file_path="/code/we.py" # 包含单个句点的文件路径
starts_with_dot_file_path=".bashrc" # 以句点开头的路径
print("文件夹路径root={},ext={}".format(*os.path.splitext(dir_path)))
print("包含多个句点的文件路径root={},ext={}".format(*os.path.splitext(multi_dot_file_path)))
print("包含单个句点的文件路径root={},ext={}".format(*os.path.splitext(single_dot_file_path)))
print("以句点开头的路径root={},ext={}".format(*os.path.splitext(starts_with_dot_file_path)))
```

![image-20221127153824033](Python标准库之os.assets/image-20221127153824033.png)


## 2.3 其他常用命令

### 2.3.1 `os.name`

导入的依赖特定操作系统的模块的名称,返回'posix'表示Linux,'nt'表示Windows,'java'表示Java虚拟机

```python
print("当前操作系统平台名称为{}".format(os.name))
```

![](../img/os.posix.JPG)

### 2.3.2 `os.__file__`

以字符串形式返回os模块安装的绝对路径

```python
import os
print("os模块安装绝对路径是{}".format(os.__file__))
```

​ ![](Python标准库之os.assets/os.__file__演示.JPG)


# 3. 参考文献

+ [os --- 操作系统接口模块 — Python 3.7.13 文档](https://docs.python.org/zh-cn/3.7/library/os.html)
+ [os.path --- 常见路径操作 — Python 3.7.13 文档](https://docs.python.org/zh-cn/3.7/library/os.path.html)
+ [Linux umask详解:令新建文件和目录拥有默认权限 (biancheng.net)](http://c.biancheng.net/view/764.html)
+ [Linux chmod命令:修改文件或目录的权限 (biancheng.net)](http://c.biancheng.net/view/755.html)

+ 274
- 40
medpy_third_lib.ipynb View File

@@ -3,8 +3,13 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "c69a7351",
"metadata": {},
"id": "5432afd4",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": [
"# !pip install medpy matplotlib #-i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com"
@@ -12,10 +17,56 @@
},
{
"cell_type": "code",
"execution_count": 4,
"id": "164df929",
"metadata": {},
"outputs": [],
"execution_count": 1,
"id": "9ba6598a",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Looking in indexes: https://mirrors.bfsu.edu.cn/pypi/web/simple/\n",
"\u001b[33mWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f3beef45450>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution')': /pypi/web/simple/medpy/\u001b[0m\n",
"Collecting medpy\n",
" Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/3b/70/c1fd5dd60242eee81774696ea7ba4caafac2bad8f028bba94b1af83777d7/MedPy-0.4.0.tar.gz (151 kB)\n",
"\u001b[K |████████████████████████████████| 151 kB 980 kB/s eta 0:00:01\n",
"\u001b[?25hCollecting matplotlib\n",
" Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/ad/62/7b662284352867a86acfb636761ba351723fc3a235efd8397578d903413d/matplotlib-3.5.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.2 MB)\n",
"\u001b[K |████████████████████████████████| 11.2 MB 5.7 MB/s eta 0:00:01\n",
"\u001b[?25hRequirement already satisfied: numpy>=1.17 in /opt/conda/lib/python3.7/site-packages (from matplotlib) (1.21.2)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /opt/conda/lib/python3.7/site-packages (from matplotlib) (3.0.8)\n",
"Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.7/site-packages (from matplotlib) (21.3)\n",
"Requirement already satisfied: pillow>=6.2.0 in /opt/conda/lib/python3.7/site-packages (from matplotlib) (8.4.0)\n",
"Collecting cycler>=0.10\n",
" Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/5c/f9/695d6bedebd747e5eb0fe8fad57b72fdf25411273a39791cde838d5a8f51/cycler-0.11.0-py3-none-any.whl (6.4 kB)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/lib/python3.7/site-packages (from matplotlib) (2.8.2)\n",
"Collecting fonttools>=4.22.0\n",
" Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/e3/d9/e9bae85e84737e76ebbcbea13607236da0c0699baed0ae4f1151b728a608/fonttools-4.38.0-py3-none-any.whl (965 kB)\n",
"\u001b[K |████████████████████████████████| 965 kB 66.1 MB/s eta 0:00:01\n",
"\u001b[?25hCollecting kiwisolver>=1.0.1\n",
" Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/ab/8f/8dbe2d4efc4c0b08ec67d6efb7cc31fbfd688c80afad85f65980633b0d37/kiwisolver-1.4.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.1 MB)\n",
"\u001b[K |████████████████████████████████| 1.1 MB 46.1 MB/s eta 0:00:01\n",
"\u001b[?25hRequirement already satisfied: typing-extensions in /opt/conda/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib) (3.10.0.2)\n",
"Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.7/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n",
"Requirement already satisfied: scipy>=1.1.0 in /opt/conda/lib/python3.7/site-packages (from medpy) (1.7.3)\n",
"Collecting SimpleITK>=1.1.0\n",
" Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/72/aa/aa10190bed1ee3f3b237b52cac7761c713f92d10aedaced13d7ad457b9de/SimpleITK-2.2.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (52.8 MB)\n",
"\u001b[K |████████████████████████████████| 52.8 MB 9.4 MB/s eta 0:00:011 |███████████████▏ | 25.0 MB 5.1 MB/s eta 0:00:06 |███████████████▌ | 25.6 MB 5.1 MB/s eta 0:00:06 |███████████████████▏ | 31.7 MB 6.7 MB/s eta 0:00:04\n",
"\u001b[?25hBuilding wheels for collected packages: medpy\n",
" Building wheel for medpy (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Created wheel for medpy: filename=MedPy-0.4.0-py3-none-any.whl size=214963 sha256=5a0acd8c11ca7a35a43bf6f8c56f1e0286798819c6e17dd5d6c91b3fa0c8102d\n",
" Stored in directory: /root/.cache/pip/wheels/e0/8e/5b/45329e6ddb95c2aff4dcafebddbf6f22243425fbe383475c23\n",
"Successfully built medpy\n",
"Installing collected packages: SimpleITK, kiwisolver, fonttools, cycler, medpy, matplotlib\n",
"Successfully installed SimpleITK-2.2.0 cycler-0.11.0 fonttools-4.38.0 kiwisolver-1.4.4 matplotlib-3.5.3 medpy-0.4.0\n"
]
}
],
"source": [
"# !pip install medpy matplotlib -i https://mirrors.bfsu.edu.cn/pypi/web/simple/"
]
@@ -23,8 +74,13 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "53d8184b",
"metadata": {},
"id": "9357f424",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": [
"import os\n",
@@ -35,8 +91,13 @@
{
"cell_type": "code",
"execution_count": 6,
"id": "7cb13c83",
"metadata": {},
"id": "1e5107e6",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": [
"# 文件夹路径和文件名\n",
@@ -53,8 +114,13 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "1f5f1dcd",
"metadata": {},
"id": "2022b7f8",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -73,8 +139,13 @@
{
"cell_type": "code",
"execution_count": 6,
"id": "01d66613",
"metadata": {},
"id": "b2f08842",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"data": {
@@ -107,8 +178,13 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "76fefbc2",
"metadata": {},
"id": "59d3ffae",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"data": {
@@ -144,8 +220,13 @@
{
"cell_type": "code",
"execution_count": 8,
"id": "aab13d4b",
"metadata": {},
"id": "c07f3c80",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": [
"# 设置数组形状为10000*10000的元组\n",
@@ -155,8 +236,13 @@
{
"cell_type": "code",
"execution_count": 9,
"id": "f32f2ab3",
"metadata": {},
"id": "66571f08",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -191,8 +277,13 @@
{
"cell_type": "code",
"execution_count": 10,
"id": "ef4058cf",
"metadata": {},
"id": "23cf3200",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -229,8 +320,13 @@
{
"cell_type": "code",
"execution_count": 11,
"id": "4fa3d6d7",
"metadata": {},
"id": "728772d9",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -267,8 +363,13 @@
{
"cell_type": "code",
"execution_count": 12,
"id": "1ced0ce4",
"metadata": {},
"id": "e9fbe52d",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -306,8 +407,13 @@
{
"cell_type": "code",
"execution_count": 13,
"id": "9eab87fc",
"metadata": {},
"id": "b4b4b8be",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -344,8 +450,13 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "11363f8f",
"metadata": {},
"id": "1e086f36",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": [
"%%timeit\n",
@@ -366,8 +477,13 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "219c7293",
"metadata": {},
"id": "9d3fa972",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -404,8 +520,13 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "6dac370a",
"metadata": {},
"id": "aee7d9b7",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -442,8 +563,13 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "86ec96bb",
"metadata": {},
"id": "a9bce405",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -480,8 +606,13 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "d359c642",
"metadata": {},
"id": "1ecd14f6",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -518,8 +649,13 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "dc54ca8a",
"metadata": {},
"id": "4244b8a2",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stdout",
@@ -552,6 +688,104 @@
"specifity=true_negative_rate(predict,ground_truth)\n",
"print(\"特异度/真阴性率为{}\".format(specifity))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53e473f5",
"metadata": {},
"outputs": [],
"source": [
"%%timeit\n",
"from medpy.metric.binary import true_positive_rate\n",
"import jax.numpy as numpy\n",
"from jax import random\n",
"# 设置伪随机数种子\n",
"rng1=random.PRNGKey(1)\n",
"rng2=random.PRNGKey(2)\n",
"# 定义预测结果和真实标记数组\n",
"predict=random.randint(key=rng1,shape=(50,50),minval=0,maxval=4)\n",
"ground_truth=random.randint(key=rng2,shape=(50,50),minval=0,maxval=4)\n",
"# 计算特异度/真阴性率\n",
"specifity=true_negative_rate(predict,ground_truth)\n",
"print(\"特异度/真阴性率为{}\".format(specifity))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d29984ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"4.24 ms ± 1.01 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"from medpy.metric.binary import precision\n",
"import jax.numpy as numpy\n",
"from jax import random\n",
"# 设置伪随机数种子\n",
"rng1=random.PRNGKey(1)\n",
"rng2=random.PRNGKey(2)\n",
"# 定义预测结果和真实标记数组\n",
"predict=random.randint(key=rng1,shape=(50,50),minval=0,maxval=4)\n",
"ground_truth=random.randint(key=rng2,shape=(50,50),minval=0,maxval=4)\n",
"# 计算精确度/阳性预测值\n",
"Precision=precision(predict,ground_truth)\n",
"print(\"精确度/阳性预测值为{}\".format(Precision))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3c2c4d4a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"精确度/阳性预测值为0.7548283261802575\n",
"2.97 ms ± 255 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"from medpy.metric.binary import positive_predictive_value\n",
"import jax.numpy as numpy\n",
"from jax import random\n",
"# 设置伪随机数种子\n",
"rng1=random.PRNGKey(1)\n",
"rng2=random.PRNGKey(2)\n",
"# 定义预测结果和真实标记数组\n",
"predict=random.randint(key=rng1,shape=(50,50),minval=0,maxval=4)\n",
"ground_truth=random.randint(key=rng2,shape=(50,50),minval=0,maxval=4)\n",
"# 计算精确度/阳性预测值\n",
"ppv=positive_predictive_value(predict,ground_truth)\n",
"print(\"精确度/阳性预测值为{}\".format(ppv))"
]
}
],
"metadata": {


Loading…
Cancel
Save