3

I have data in a rectangular structure – something like matrix={{a,b,c},{d,e,f},{g,h,i}}. The problem is that some of the columns consist of uncompressed data and some not. Now I'm looking for an elegant way to uncompress the whole matrix. Unfortunately Uncompress[matrix] doesn't work.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
RMMA
  • 2,710
  • 2
  • 18
  • 33

2 Answers2

4

Here is a way to only uncompress elements that have been compressed. Note that using Mathematica's Compress returns a string starting with <number>:, so we need to Uncompress only those elements that are strings and start with that.

Clear@uncompress
uncompress[s_String] /; StringMatchQ[s, DigitCharacter ~~ ":" ~~ ___] := Uncompress@s
uncompress[x_] := x
rm -rf
  • 88,781
  • 21
  • 293
  • 472
  • The characters "1:e" only appear at the beginning for version 1 compression. Version 2 seems to have "2:G". I would tend to consider only the version number and colon to be part of the header proper since I don't know where the "e" or "G" come from... – Oleksandr R. Sep 04 '12 at 14:08
  • Is the : documented anywhere? – Dr. belisarius Sep 04 '12 at 14:11
  • @OleksandrR. There is a version 2 of Compress? I've only ever seen 1:e, but I've changed it to DigitCharacter ~~ ":" since you say you've seen 2:G – rm -rf Sep 04 '12 at 14:12
  • @Verde Trying to find it, but not sure if it is – rm -rf Sep 04 '12 at 14:14
  • @Verde it's not documented, but only versions 1 and 2 currently exist, and they both have DigitCharacter ~~ ":", so... (To compress something using the version 2 method, use Compress[expr, Method -> {"Version" -> 2}].) – Oleksandr R. Sep 04 '12 at 14:51
  • @OleksandrR. If it is not documented, I prefer to stay away of it. Sometimes messing with undocumented features is convenient, but this is not the case – Dr. belisarius Sep 04 '12 at 15:03
  • 1
    @Verde the fact that Uncompress emits a message when passed something other than a string representing compressed data isn't documented either, but your answer relies on it. By requiring the parameter to be a string of a certain form rather than passing everything indiscriminately, you reduce the chances of encountering side effects or passing some garbage that Uncompress may choke on. Of course the optimum solution is avoid creating data structures that require heuristics to reinterpret in the first place... – Oleksandr R. Sep 04 '12 at 15:15
  • @OleksandrR. If you look at the first comment I posted under the question, you will understand that I am completely aware of those issues. Anyway, my answer doesn't rely on Uncompress[] issuing a message, but in the original list not having an uncompressed element that matches a thing that could be uncompressed. The fact that Compress[] and Uncompress[] are not bijective worsens the thing a little (details in the comments here) – Dr. belisarius Sep 04 '12 at 15:32
  • @Verde I wasn't claiming that you weren't aware of the potential problems, but just that there are difficulties with both approaches that are not easily avoided by relying only on what is documented. Your answer depends on Check, which is tied to the existence of messages. If you don't want to assume that, SameQ could be used, but one must take care not to allow evaluation to proceed too far before doing the comparison. Something like this would be needed as a wrapper. – Oleksandr R. Sep 04 '12 at 15:51
  • @OleksandrR. Yeap. But seems an overkill for a problem that should be solved by good design :) – Dr. belisarius Sep 04 '12 at 15:53
2

Create a matrix, and compress a column

a = RandomInteger[10, {3, 3}]
b = Transpose@{a[[1]], Compress /@ a[[2]], a[[3]]}

Uncompress it:

Quiet@Map[Check[Uncompress[#], #] &, a, {2}]

Edit

Instead of Quiet[] you could use Off[Uncompress::string] so other error messages are not hindered

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • Better to use ReplaceAll with a pattern str_String /; StringMatchQ[str, StartOfString ~~ DigitCharacter ~~ ":" ~~ __] :> Check[Uncompress[str], str, Uncompress::corrupt]? – Oleksandr R. Sep 04 '12 at 14:02
  • it seems as if it doesnt work in my case. Maybe because the compressed part can be again a list. In that case I would like to write only the 7th element into the uncompressed matrix. – RMMA Sep 04 '12 at 14:06
  • 3
    @rainer Why don't you then tell us what your "case" is? Your question is vague and this is the most that can be said without divining your thoughts – rm -rf Sep 04 '12 at 14:08
  • @OleksandrR. I am not aware that the : is documented. If it is, you are right – Dr. belisarius Sep 04 '12 at 14:14
  • ok, now it works. Thx! – RMMA Sep 04 '12 at 14:42