-
Notifications
You must be signed in to change notification settings - Fork 9
Add more compression formats #90
Conversation
Codecov Report
@@ Coverage Diff @@
## master #90 +/- ##
==========================================
- Coverage 95.92% 95.88% -0.04%
==========================================
Files 8 9 +1
Lines 1275 1289 +14
==========================================
+ Hits 1223 1236 +13
- Misses 52 53 +1
Continue to review full report at Codecov.
|
|
Instead of the enum's for compression, I wonder if a better approach is to define an abstract type so that people can add different compression schemes without having to depend on a whole lot of different packages. abstract type AbstractCompressionScheme end
struct NoCompression <: AbstractCompressionScheme end
function compressed_open(
f::Function, filename::String, mode::String, ::NoCompression
)
return open(f, filename, mode)
end
struct Gzip <: AbstractCompressionScheme end
function open(
f::Function, filename::String, mode::String, ::Gzip
)
if mode == "w"
open(f, CodecZlib.GzipCompressorStream, filename, "w")
else
...
end
end |
|
I'm trying to implement your approach, with types. However, when looping over the compression formats (detect the format from the extension), I don't see any clean way of implementing it with types. I find that Here is what I did right now: 698b8cc |
|
Just force the user to provide the compression scheme. Define function read_from_file(filename; compression_scheme = nothing)
if compression_scheme === nothing
compression_scheme = endswith(filename, ".gz") ? Gzip() : NoCompression()
end
compressed_open(filename, "r", compression_scheme) do
read_from_file(...)
end
end |
|
Better this way? I still define three "default" compression schemes, supported everywhere. The latest commit also includes a little bit of documentation. Should the new |
src/MathOptFormat.jl
Outdated
| return _file_formats[_filename_to_format(filename)][2]() | ||
| end | ||
|
|
||
| function gzip_open(f::Function, filename::String, mode::String; compression::AbstractCompressionScheme=AutomaticCompressionDetection()) |
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.
Remove this function.
| abstract type AbstractCompressionScheme end | ||
|
|
||
| struct AutomaticCompressionDetection <: AbstractCompressionScheme end | ||
| # No open() implementation, this would not make sense (flag to indicate that _filename_to_compression should be called). |
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.
function _compressed_open(
f::Function,
filename::String,
mode::String,
::AutomaticCompression
)
compression = _filename_to_compression(filename)
return _compressed_open(f, filename, mode, compression)
end
src/compression.jl
Outdated
| # No open() implementation, this would not make sense (flag to indicate that _filename_to_compression should be called). | ||
|
|
||
| struct NoCompression <: AbstractCompressionScheme end | ||
| function open( |
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.
_compressed_open
src/compression.jl
Outdated
| end | ||
|
|
||
| struct Gzip <: AbstractCompressionScheme end | ||
| function open( |
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.
_compressed_open
src/compression.jl
Outdated
| end | ||
|
|
||
| struct Bzip2 <: AbstractCompressionScheme end | ||
| function open( |
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.
_compressed_open
src/compression.jl
Outdated
| end | ||
|
|
||
| struct Xz <: AbstractCompressionScheme end | ||
| function open( |
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.
_compressed_open
src/compression.jl
Outdated
| """ | ||
| abstract type AbstractCompressionScheme end | ||
|
|
||
| struct AutomaticCompressionDetection <: AbstractCompressionScheme end |
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.
Just AutomaticCompression
src/MathOptFormat.jl
Outdated
| function MOI.write_to_file(model::MATH_OPT_FORMATS, filename::String) | ||
| gzip_open(filename, "w") do io | ||
| function MOI.write_to_file(model::MATH_OPT_FORMATS, filename::String; compression::AbstractCompressionScheme=AutomaticCompressionDetection()) | ||
| gzip_open(filename, "w", compression=compression) do io |
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.
_compressed_open(filename, "w", compression) do io
src/MathOptFormat.jl
Outdated
| function MOI.read_from_file(model::MATH_OPT_FORMATS, filename::String) | ||
| gzip_open(filename, "r") do io | ||
| function MOI.read_from_file(model::MATH_OPT_FORMATS, filename::String; compression::AbstractCompressionScheme=AutomaticCompressionDetection()) | ||
| gzip_open(filename, "r", compression=compression) do io |
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.
_compressed_open(filename, "r", compression) do io
| else | ||
| error("File-type of $(filename) not supported by MathOptFormat.jl.") | ||
| end | ||
| model = _filename_to_model(filename) |
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.
function read_from_file(
filename::String;
compression::AbstractCompressionScheme = AutomaticCompression(),
file_format::FileFormat = AUTOMATIC_FILE_FORMAT,
)
if file_format == AUTOMATIC_FILE_FORMAT
for (format, (ext, _)) in _FILE_FORMATS
if endswith(filename, ext) || occursin("$(ext).", filename)
file_format = format
break
end
end
error(
"Unable to detect automatically format of $(filename). Use the " *
"`file_format` keyword to specify the file format."
)
end
model = _FILE_FORMATS[file_format]()
MOI.read_from_file(model, filename; compression = compression)
return model
endYou might want to do something similar with AutomaticCompression so that we don't try NoCompression if the user passes in a weird filename.
|
Better this way? |
|
I've added a few tests. One of them fails, I registered an issue for that (JuliaIO/CodecXz.jl#16). |
|
Just remove Xz for now |
|
Shall I remove the code or comment it out in this PR?
…On Sun, 3 Nov 2019, 15:19 Oscar Dowson, ***@***.***> wrote:
Just remove Xz for now
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#90?email_source=notifications&email_token=AACFY6QR7TF6D3UMK5QJU6DQR3FXHA5CNFSM4JGR5CK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC5TUNQ#issuecomment-549141046>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACFY6UYUMEGL3HAJ2PHGILQR3FXHANCNFSM4JGR5CKQ>
.
|
|
I've commented out this code so it can easily be restored once the package is fixed. |
|
All tests pass; could this be merged? |
|
Sorry for moving slowly on this. Thanks! |
|
No problem, thanks for merging :)! |
Following jump-dev/JuMP.jl#1982, here is the implementation of two other compression formats (BZIP2 and XZ). While I was at it, I also refactored a bit the common functions (in the hope that adding new compression formats and new export formats will be easier, the changes to be done being centralised).
For now, I did not add tests, as I'm not sure which ones I should add (currently, it seems compression is handled within each file format, but I think just testing one of them should be enough -- e.g., just LP, as nothing is really dependent on the file format).