Spaces:
Sleeping
Sleeping
Download test.py from tomaszki/PythonFileCompressor: direct link, hf CLI and curl.
- Browser
- Download file 758 Bytes
-
https://huggingface.co/spaces/tomaszki/PythonFileCompressor/resolve/0b6ba53eb03d1ab1c88763bf9508cc5927d349a9/test.py
- Command line
-
hf download hf://spaces/tomaszki/PythonFileCompressor@0b6ba53eb03d1ab1c88763bf9508cc5927d349a9/test.py
-
curl -L -o test.py https://huggingface.co/spaces/tomaszki/PythonFileCompressor/resolve/0b6ba53eb03d1ab1c88763bf9508cc5927d349a9/test.py
758 Bytes
| import numpyAc | |
| import numpy as np | |
| # Generate random symbols and pdf. | |
| dim = 128 | |
| symsNum = 2000 | |
| pdf = np.random.rand(symsNum,dim) | |
| pdf = pdf / (np.sum(pdf,1,keepdims=True)) | |
| sym = np.random.randint(0,dim,symsNum,dtype=np.int16) | |
| output_pdf = pdf | |
| # Encode to bytestream. | |
| codec = numpyAc.arithmeticCoding() | |
| byte_stream,real_bits = codec.encode(pdf, sym,'out.b') | |
| # Number of bits taken by the stream. | |
| print('real_bits',real_bits) | |
| # Theoretical bits number | |
| print('shannon entropy',-int(np.log2(pdf[range(0,symsNum),sym]).sum())) | |
| # Decode from bytestream. | |
| decodec = numpyAc.arithmeticDeCoding(None,symsNum,dim,'out.b') | |
| # Autoregressive decoding and output will be equal to the input. | |
| for i,s in enumerate(sym): | |
| assert decodec.decode(output_pdf[i:i+1,:]) == s |