pysus 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
PySUS-0.1.1/PKG-INFO ADDED
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 1.0
2
+ Name: PySUS
3
+ Version: 0.1.1
4
+ Summary: Tools for dealing with Brazil's Public health data
5
+ Home-page: UNKNOWN
6
+ Author: Flavio Codeco Coelho
7
+ Author-email: fccoelho@gmail.com
8
+ License: gpl-v3
9
+ Description: # PySUS
10
+
11
+ This package collects a set of utilities for handling with public databases published by Brazil's DATASUS
12
+
13
+ ## Features
14
+
15
+ - Decode encoded patient age to any time unit (years, months, etc)
16
+ - Convert `.dbc` files to DBF databases or read them into pandas dataframes. DBC files are basically DBFs compressed by a proprietary algorithm.
17
+
18
+ ## Instalation
19
+
20
+ `
21
+ $ sudo pip install PySUS`
22
+
23
+ Platform: UNKNOWN
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 1.0
2
+ Name: PySUS
3
+ Version: 0.1.1
4
+ Summary: Tools for dealing with Brazil's Public health data
5
+ Home-page: UNKNOWN
6
+ Author: Flavio Codeco Coelho
7
+ Author-email: fccoelho@gmail.com
8
+ License: gpl-v3
9
+ Description: # PySUS
10
+
11
+ This package collects a set of utilities for handling with public databases published by Brazil's DATASUS
12
+
13
+ ## Features
14
+
15
+ - Decode encoded patient age to any time unit (years, months, etc)
16
+ - Convert `.dbc` files to DBF databases or read them into pandas dataframes. DBC files are basically DBFs compressed by a proprietary algorithm.
17
+
18
+ ## Instalation
19
+
20
+ `
21
+ $ sudo pip install PySUS`
22
+
23
+ Platform: UNKNOWN
@@ -0,0 +1,18 @@
1
+ setup.py
2
+ PySUS.egg-info/PKG-INFO
3
+ PySUS.egg-info/SOURCES.txt
4
+ PySUS.egg-info/dependency_links.txt
5
+ PySUS.egg-info/not-zip-safe
6
+ PySUS.egg-info/requires.txt
7
+ PySUS.egg-info/top_level.txt
8
+ pysus/__init__.py
9
+ pysus/preprocessing/__init__.py
10
+ pysus/preprocessing/decoders.py
11
+ pysus/tests/__init__.py
12
+ pysus/tests/test_decoders.py
13
+ pysus/tests/test_utilities.py
14
+ pysus/utilities/__init__.py
15
+ pysus/utilities/_build_readdbc.py
16
+ pysus/utilities/_readdbc.cpython-35m-x86_64-linux-gnu.so
17
+ pysus/utilities/readdbc.py
18
+ pysus/utilities/c-src/blast.c
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,3 @@
1
+ pandas
2
+ dbfread
3
+ cffi>=1.0.0
@@ -0,0 +1 @@
1
+ pysus
File without changes
@@ -0,0 +1,8 @@
1
+ #-*- coding:utf-8 -*-
2
+ u"""
3
+ Created on 19/07/16
4
+ by fccoelho
5
+ license: GPL V3 or Later
6
+ """
7
+
8
+ __docformat__ = 'restructuredtext en'
@@ -0,0 +1,36 @@
1
+ #-*- coding:utf-8 -*-
2
+ u"""
3
+ This module contains a set of functions to decode
4
+ commonly encoded variables
5
+ Created on 19/07/16
6
+ by fccoelho
7
+ license: GPL V3 or Later
8
+ """
9
+
10
+ __docformat__ = 'restructuredtext en'
11
+ import numpy as np
12
+
13
+ @np.vectorize
14
+ def decodifica_idade(idade, unidade='Y'):
15
+ """
16
+ Em tabelas do SINAN frequentemente a idade é representada como um inteiro que precisa ser parseado
17
+ para retornar a idade em uma unidade cronológica padrão.
18
+ :param unidade: unidade da idade: 'Y': anos, 'M' meses, 'D': dias, 'H': horas
19
+ :param idade: inteiro ou sequencia de inteiros codificados.
20
+ :return:
21
+ """
22
+ fator = {'Y': 1, 'M': 12, 'D': 365, 'H': 365*24}
23
+ if idade >= 4000: #idade em anos
24
+ idade_anos = idade - 4000
25
+ elif idade >= 3000 and idade < 4000: #idade em meses
26
+ idade_anos = (idade-3000)/12
27
+ elif idade >= 2000 and idade < 3000: #idade em dias
28
+ idade_anos = (idade-2000)/365
29
+ elif idade >= 1000 and idade < 2000: # idade em horas
30
+ idade_anos = (idade-1000)/(365*24)
31
+ else:
32
+ #print(idade)
33
+ idade_anos = np.nan
34
+ #raise ValueError("Idade inválida")
35
+ idade_dec = idade_anos/fator[unidade]
36
+ return idade_dec
@@ -0,0 +1,8 @@
1
+ #-*- coding:utf-8 -*-
2
+ u"""
3
+ Created on 19/07/16
4
+ by fccoelho
5
+ license: GPL V3 or Later
6
+ """
7
+
8
+ __docformat__ = 'restructuredtext en'
@@ -0,0 +1,35 @@
1
+ # -*- coding:utf-8 -*-
2
+ u"""
3
+ Created on 19/07/16
4
+ by fccoelho
5
+ license: GPL V3 or Later
6
+ """
7
+
8
+ import unittest
9
+
10
+ import numpy as np
11
+ from numpy.testing import *
12
+
13
+ from pysus.preprocessing import decoders
14
+
15
+
16
+ class TestDecoder(unittest.TestCase):
17
+ def test_decodifica_idade_retorna_em_anos(self):
18
+ res = decoders.decodifica_idade(4010, unidade='Y')
19
+ self.assertEqual(res, 10)
20
+ res = decoders.decodifica_idade(3120, unidade='Y')
21
+ self.assertEqual(res, 10)
22
+ res = decoders.decodifica_idade(2365, unidade='Y')
23
+ self.assertAlmostEqual(res, 1, places=4)
24
+ res = decoders.decodifica_idade(1480, unidade='Y')
25
+ self.assertAlmostEqual(res, 0.0547, places=3)
26
+
27
+ def test_decodifica_lista_idades_retorna_em_anos(self):
28
+ res = decoders.decodifica_idade([4010] * 3, unidade='Y')
29
+ assert_array_equal(res, np.array([10] * 3))
30
+ res = decoders.decodifica_idade([3120] * 4, unidade='Y')
31
+ assert_array_equal(res, np.array([10] * 4))
32
+ res = decoders.decodifica_idade([2365] * 2, unidade='Y')
33
+ assert_array_equal(res, np.array([1, 1]))
34
+ res = decoders.decodifica_idade([1480] * 5, unidade='Y')
35
+ assert_array_almost_equal(res, np.array([0.0547] * 5), decimal=3)
@@ -0,0 +1,18 @@
1
+ u"""
2
+ Created on 17/08/16
3
+ by fccoelho
4
+ license: GPL V3 or Later
5
+ """
6
+
7
+ import unittest
8
+
9
+ import pandas as pd
10
+
11
+ from pysus.utilities.readdbc import read_dbc
12
+
13
+
14
+ class TestReadDbc(unittest.TestCase):
15
+ def test_read_dbc(self):
16
+ df = read_dbc(b'test_data/sids.dbc')
17
+ self.assertIsInstance(df, pd.DataFrame)
18
+ self.assertGreater(df.size, 0)
@@ -0,0 +1,6 @@
1
+ u"""
2
+ Created on 16/08/16
3
+ by fccoelho
4
+ license: GPL V3 or Later
5
+ """
6
+
@@ -0,0 +1,32 @@
1
+ u"""
2
+ Created on 16/08/16
3
+ by fccoelho
4
+ license: GPL V3 or Later
5
+ """
6
+ import os
7
+ from cffi import FFI
8
+
9
+ ffibuilder = FFI()
10
+
11
+ PATH = os.path.dirname(__file__)
12
+
13
+ with open(os.path.join(PATH, 'c-src/dbc2dbf.c'),'r') as f:
14
+ ffibuilder.set_source("pysus.utilities._readdbc",
15
+ f.read(),
16
+ libraries=["c"],
17
+ sources=[os.path.join(PATH, "c-src/blast.c")],
18
+ include_dirs=[os.path.join(PATH, "c-src/")]
19
+ )
20
+ ffibuilder.cdef(
21
+ """
22
+ static unsigned inf(void *how, unsigned char **buf);
23
+ static int outf(void *how, unsigned char *buf, unsigned len);
24
+ void dbc2dbf(char** input_file, char** output_file);
25
+ """
26
+ )
27
+
28
+ with open(os.path.join(PATH, "c-src/blast.h")) as f:
29
+ ffibuilder.cdef(f.read(), override=True)
30
+
31
+ if __name__ == "__main__":
32
+ ffibuilder.compile(verbose=True)
@@ -0,0 +1,406 @@
1
+ /* blast.c
2
+ * Copyright (C) 2003, 2012 Mark Adler
3
+ * For conditions of distribution and use, see copyright notice in blast.h
4
+ * version 1.2, 24 Oct 2012
5
+ *
6
+ * blast.c decompresses data compressed by the PKWare Compression Library.
7
+ * This function provides functionality similar to the explode() function of
8
+ * the PKWare library, hence the name "blast".
9
+ *
10
+ * This decompressor is based on the excellent format description provided by
11
+ * Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the
12
+ * example Ben provided in the post is incorrect. The distance 110001 should
13
+ * instead be 111000. When corrected, the example byte stream becomes:
14
+ *
15
+ * 00 04 82 24 25 8f 80 7f
16
+ *
17
+ * which decompresses to "AIAIAIAIAIAIA" (without the quotes).
18
+ */
19
+
20
+ /*
21
+ * Change history:
22
+ *
23
+ * 1.0 12 Feb 2003 - First version
24
+ * 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
25
+ * 1.2 24 Oct 2012 - Add note about using binary mode in stdio
26
+ * - Fix comparisons of differently signed integers
27
+ */
28
+
29
+ #include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
30
+ #include "blast.h" /* prototype for blast() */
31
+
32
+ #define MAXBITS 13 /* maximum code length */
33
+ #define MAXWIN 4096 /* maximum window size */
34
+
35
+ /* input and output state */
36
+ struct state {
37
+ /* input state */
38
+ blast_in infun; /* input function provided by user */
39
+ void *inhow; /* opaque information passed to infun() */
40
+ unsigned char *in; /* next input location */
41
+ unsigned left; /* available input at in */
42
+ int bitbuf; /* bit buffer */
43
+ int bitcnt; /* number of bits in bit buffer */
44
+
45
+ /* input limit error return state for bits() and decode() */
46
+ jmp_buf env;
47
+
48
+ /* output state */
49
+ blast_out outfun; /* output function provided by user */
50
+ void *outhow; /* opaque information passed to outfun() */
51
+ unsigned next; /* index of next write location in out[] */
52
+ int first; /* true to check distances (for first 4K) */
53
+ unsigned char out[MAXWIN]; /* output buffer and sliding window */
54
+ };
55
+
56
+ /*
57
+ * Return need bits from the input stream. This always leaves less than
58
+ * eight bits in the buffer. bits() works properly for need == 0.
59
+ *
60
+ * Format notes:
61
+ *
62
+ * - Bits are stored in bytes from the least significant bit to the most
63
+ * significant bit. Therefore bits are dropped from the bottom of the bit
64
+ * buffer, using shift right, and new bytes are appended to the top of the
65
+ * bit buffer, using shift left.
66
+ */
67
+ static int bits(struct state *s, int need)
68
+ {
69
+ int val; /* bit accumulator */
70
+
71
+ /* load at least need bits into val */
72
+ val = s->bitbuf;
73
+ while (s->bitcnt < need) {
74
+ if (s->left == 0) {
75
+ s->left = s->infun(s->inhow, &(s->in));
76
+ if (s->left == 0) longjmp(s->env, 1); /* out of input */
77
+ }
78
+ val |= (int)(*(s->in)++) << s->bitcnt; /* load eight bits */
79
+ s->left--;
80
+ s->bitcnt += 8;
81
+ }
82
+
83
+ /* drop need bits and update buffer, always zero to seven bits left */
84
+ s->bitbuf = val >> need;
85
+ s->bitcnt -= need;
86
+
87
+ /* return need bits, zeroing the bits above that */
88
+ return val & ((1 << need) - 1);
89
+ }
90
+
91
+ /*
92
+ * Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
93
+ * each length, which for a canonical code are stepped through in order.
94
+ * symbol[] are the symbol values in canonical order, where the number of
95
+ * entries is the sum of the counts in count[]. The decoding process can be
96
+ * seen in the function decode() below.
97
+ */
98
+ struct huffman {
99
+ short *count; /* number of symbols of each length */
100
+ short *symbol; /* canonically ordered symbols */
101
+ };
102
+
103
+ /*
104
+ * Decode a code from the stream s using huffman table h. Return the symbol or
105
+ * a negative value if there is an error. If all of the lengths are zero, i.e.
106
+ * an empty code, or if the code is incomplete and an invalid code is received,
107
+ * then -9 is returned after reading MAXBITS bits.
108
+ *
109
+ * Format notes:
110
+ *
111
+ * - The codes as stored in the compressed data are bit-reversed relative to
112
+ * a simple integer ordering of codes of the same lengths. Hence below the
113
+ * bits are pulled from the compressed data one at a time and used to
114
+ * build the code value reversed from what is in the stream in order to
115
+ * permit simple integer comparisons for decoding.
116
+ *
117
+ * - The first code for the shortest length is all ones. Subsequent codes of
118
+ * the same length are simply integer decrements of the previous code. When
119
+ * moving up a length, a one bit is appended to the code. For a complete
120
+ * code, the last code of the longest length will be all zeros. To support
121
+ * this ordering, the bits pulled during decoding are inverted to apply the
122
+ * more "natural" ordering starting with all zeros and incrementing.
123
+ */
124
+ static int decode(struct state *s, struct huffman *h)
125
+ {
126
+ int len; /* current number of bits in code */
127
+ int code; /* len bits being decoded */
128
+ int first; /* first code of length len */
129
+ int count; /* number of codes of length len */
130
+ int index; /* index of first code of length len in symbol table */
131
+ int bitbuf; /* bits from stream */
132
+ int left; /* bits left in next or left to process */
133
+ short *next; /* next number of codes */
134
+
135
+ bitbuf = s->bitbuf;
136
+ left = s->bitcnt;
137
+ code = first = index = 0;
138
+ len = 1;
139
+ next = h->count + 1;
140
+ while (1) {
141
+ while (left--) {
142
+ code |= (bitbuf & 1) ^ 1; /* invert code */
143
+ bitbuf >>= 1;
144
+ count = *next++;
145
+ if (code < first + count) { /* if length len, return symbol */
146
+ s->bitbuf = bitbuf;
147
+ s->bitcnt = (s->bitcnt - len) & 7;
148
+ return h->symbol[index + (code - first)];
149
+ }
150
+ index += count; /* else update for next length */
151
+ first += count;
152
+ first <<= 1;
153
+ code <<= 1;
154
+ len++;
155
+ }
156
+ left = (MAXBITS+1) - len;
157
+ if (left == 0) break;
158
+ if (s->left == 0) {
159
+ s->left = s->infun(s->inhow, &(s->in));
160
+ if (s->left == 0) longjmp(s->env, 1); /* out of input */
161
+ }
162
+ bitbuf = *(s->in)++;
163
+ s->left--;
164
+ if (left > 8) left = 8;
165
+ }
166
+ return -9; /* ran out of codes */
167
+ }
168
+
169
+ /*
170
+ * Given a list of repeated code lengths rep[0..n-1], where each byte is a
171
+ * count (high four bits + 1) and a code length (low four bits), generate the
172
+ * list of code lengths. This compaction reduces the size of the object code.
173
+ * Then given the list of code lengths length[0..n-1] representing a canonical
174
+ * Huffman code for n symbols, construct the tables required to decode those
175
+ * codes. Those tables are the number of codes of each length, and the symbols
176
+ * sorted by length, retaining their original order within each length. The
177
+ * return value is zero for a complete code set, negative for an over-
178
+ * subscribed code set, and positive for an incomplete code set. The tables
179
+ * can be used if the return value is zero or positive, but they cannot be used
180
+ * if the return value is negative. If the return value is zero, it is not
181
+ * possible for decode() using that table to return an error--any stream of
182
+ * enough bits will resolve to a symbol. If the return value is positive, then
183
+ * it is possible for decode() using that table to return an error for received
184
+ * codes past the end of the incomplete lengths.
185
+ */
186
+ static int construct(struct huffman *h, const unsigned char *rep, int n)
187
+ {
188
+ int symbol; /* current symbol when stepping through length[] */
189
+ int len; /* current length when stepping through h->count[] */
190
+ int left; /* number of possible codes left of current length */
191
+ short offs[MAXBITS+1]; /* offsets in symbol table for each length */
192
+ short length[256]; /* code lengths */
193
+
194
+ /* convert compact repeat counts into symbol bit length list */
195
+ symbol = 0;
196
+ do {
197
+ len = *rep++;
198
+ left = (len >> 4) + 1;
199
+ len &= 15;
200
+ do {
201
+ length[symbol++] = len;
202
+ } while (--left);
203
+ } while (--n);
204
+ n = symbol;
205
+
206
+ /* count number of codes of each length */
207
+ for (len = 0; len <= MAXBITS; len++)
208
+ h->count[len] = 0;
209
+ for (symbol = 0; symbol < n; symbol++)
210
+ (h->count[length[symbol]])++; /* assumes lengths are within bounds */
211
+ if (h->count[0] == n) /* no codes! */
212
+ return 0; /* complete, but decode() will fail */
213
+
214
+ /* check for an over-subscribed or incomplete set of lengths */
215
+ left = 1; /* one possible code of zero length */
216
+ for (len = 1; len <= MAXBITS; len++) {
217
+ left <<= 1; /* one more bit, double codes left */
218
+ left -= h->count[len]; /* deduct count from possible codes */
219
+ if (left < 0) return left; /* over-subscribed--return negative */
220
+ } /* left > 0 means incomplete */
221
+
222
+ /* generate offsets into symbol table for each length for sorting */
223
+ offs[1] = 0;
224
+ for (len = 1; len < MAXBITS; len++)
225
+ offs[len + 1] = offs[len] + h->count[len];
226
+
227
+ /*
228
+ * put symbols in table sorted by length, by symbol order within each
229
+ * length
230
+ */
231
+ for (symbol = 0; symbol < n; symbol++)
232
+ if (length[symbol] != 0)
233
+ h->symbol[offs[length[symbol]]++] = symbol;
234
+
235
+ /* return zero for complete set, positive for incomplete set */
236
+ return left;
237
+ }
238
+
239
+ /*
240
+ * Decode PKWare Compression Library stream.
241
+ *
242
+ * Format notes:
243
+ *
244
+ * - First byte is 0 if literals are uncoded or 1 if they are coded. Second
245
+ * byte is 4, 5, or 6 for the number of extra bits in the distance code.
246
+ * This is the base-2 logarithm of the dictionary size minus six.
247
+ *
248
+ * - Compressed data is a combination of literals and length/distance pairs
249
+ * terminated by an end code. Literals are either Huffman coded or
250
+ * uncoded bytes. A length/distance pair is a coded length followed by a
251
+ * coded distance to represent a string that occurs earlier in the
252
+ * uncompressed data that occurs again at the current location.
253
+ *
254
+ * - A bit preceding a literal or length/distance pair indicates which comes
255
+ * next, 0 for literals, 1 for length/distance.
256
+ *
257
+ * - If literals are uncoded, then the next eight bits are the literal, in the
258
+ * normal bit order in th stream, i.e. no bit-reversal is needed. Similarly,
259
+ * no bit reversal is needed for either the length extra bits or the distance
260
+ * extra bits.
261
+ *
262
+ * - Literal bytes are simply written to the output. A length/distance pair is
263
+ * an instruction to copy previously uncompressed bytes to the output. The
264
+ * copy is from distance bytes back in the output stream, copying for length
265
+ * bytes.
266
+ *
267
+ * - Distances pointing before the beginning of the output data are not
268
+ * permitted.
269
+ *
270
+ * - Overlapped copies, where the length is greater than the distance, are
271
+ * allowed and common. For example, a distance of one and a length of 518
272
+ * simply copies the last byte 518 times. A distance of four and a length of
273
+ * twelve copies the last four bytes three times. A simple forward copy
274
+ * ignoring whether the length is greater than the distance or not implements
275
+ * this correctly.
276
+ */
277
+ static int decomp(struct state *s)
278
+ {
279
+ int lit; /* true if literals are coded */
280
+ int dict; /* log2(dictionary size) - 6 */
281
+ int symbol; /* decoded symbol, extra bits for distance */
282
+ int len; /* length for copy */
283
+ unsigned dist; /* distance for copy */
284
+ int copy; /* copy counter */
285
+ unsigned char *from, *to; /* copy pointers */
286
+ static int virgin = 1; /* build tables once */
287
+ static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */
288
+ static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */
289
+ static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */
290
+ static struct huffman litcode = {litcnt, litsym}; /* length code */
291
+ static struct huffman lencode = {lencnt, lensym}; /* length code */
292
+ static struct huffman distcode = {distcnt, distsym};/* distance code */
293
+ /* bit lengths of literal codes */
294
+ static const unsigned char litlen[] = {
295
+ 11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
296
+ 9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
297
+ 7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
298
+ 8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
299
+ 44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
300
+ 44, 173};
301
+ /* bit lengths of length codes 0..15 */
302
+ static const unsigned char lenlen[] = {2, 35, 36, 53, 38, 23};
303
+ /* bit lengths of distance codes 0..63 */
304
+ static const unsigned char distlen[] = {2, 20, 53, 230, 247, 151, 248};
305
+ static const short base[16] = { /* base for length codes */
306
+ 3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264};
307
+ static const char extra[16] = { /* extra bits for length codes */
308
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8};
309
+
310
+ /* set up decoding tables (once--might not be thread-safe) */
311
+ if (virgin) {
312
+ construct(&litcode, litlen, sizeof(litlen));
313
+ construct(&lencode, lenlen, sizeof(lenlen));
314
+ construct(&distcode, distlen, sizeof(distlen));
315
+ virgin = 0;
316
+ }
317
+
318
+ /* read header */
319
+ lit = bits(s, 8);
320
+ if (lit > 1) return -1;
321
+ dict = bits(s, 8);
322
+ if (dict < 4 || dict > 6) return -2;
323
+
324
+ /* decode literals and length/distance pairs */
325
+ do {
326
+ if (bits(s, 1)) {
327
+ /* get length */
328
+ symbol = decode(s, &lencode);
329
+ len = base[symbol] + bits(s, extra[symbol]);
330
+ if (len == 519) break; /* end code */
331
+
332
+ /* get distance */
333
+ symbol = len == 2 ? 2 : dict;
334
+ dist = decode(s, &distcode) << symbol;
335
+ dist += bits(s, symbol);
336
+ dist++;
337
+ if (s->first && dist > s->next)
338
+ return -3; /* distance too far back */
339
+
340
+ /* copy length bytes from distance bytes back */
341
+ do {
342
+ to = s->out + s->next;
343
+ from = to - dist;
344
+ copy = MAXWIN;
345
+ if (s->next < dist) {
346
+ from += copy;
347
+ copy = dist;
348
+ }
349
+ copy -= s->next;
350
+ if (copy > len) copy = len;
351
+ len -= copy;
352
+ s->next += copy;
353
+ do {
354
+ *to++ = *from++;
355
+ } while (--copy);
356
+ if (s->next == MAXWIN) {
357
+ if (s->outfun(s->outhow, s->out, s->next)) return 1;
358
+ s->next = 0;
359
+ s->first = 0;
360
+ }
361
+ } while (len != 0);
362
+ }
363
+ else {
364
+ /* get literal and write it */
365
+ symbol = lit ? decode(s, &litcode) : bits(s, 8);
366
+ s->out[s->next++] = symbol;
367
+ if (s->next == MAXWIN) {
368
+ if (s->outfun(s->outhow, s->out, s->next)) return 1;
369
+ s->next = 0;
370
+ s->first = 0;
371
+ }
372
+ }
373
+ } while (1);
374
+ return 0;
375
+ }
376
+
377
+ /* See comments in blast.h */
378
+ int blast(blast_in infun, void *inhow, blast_out outfun, void *outhow)
379
+ {
380
+ struct state s; /* input/output state */
381
+ int err; /* return value */
382
+
383
+ /* initialize input state */
384
+ s.infun = infun;
385
+ s.inhow = inhow;
386
+ s.left = 0;
387
+ s.bitbuf = 0;
388
+ s.bitcnt = 0;
389
+
390
+ /* initialize output state */
391
+ s.outfun = outfun;
392
+ s.outhow = outhow;
393
+ s.next = 0;
394
+ s.first = 1;
395
+
396
+ /* return if bits() or decode() tries to read past available input */
397
+ if (setjmp(s.env) != 0) /* if came back here via longjmp(), */
398
+ err = 2; /* then skip decomp(), return error */
399
+ else
400
+ err = decomp(&s); /* decompress */
401
+
402
+ /* write any leftover output and update the error code if needed */
403
+ if (err != 1 && s.next && s.outfun(s.outhow, s.out, s.next) && err == 0)
404
+ err = 1;
405
+ return err;
406
+ }
@@ -0,0 +1,45 @@
1
+ u"""
2
+ Created on 16/08/16
3
+ by fccoelho
4
+ license: GPL V3 or Later
5
+ """
6
+ import os
7
+ from tempfile import NamedTemporaryFile
8
+
9
+ import pandas as pd
10
+ from dbfread import DBF
11
+
12
+ from pysus.utilities._readdbc import ffi, lib
13
+
14
+
15
+ def read_dbc(filename, encoding='utf-8'):
16
+ """
17
+ Opens a DATASUS .dbc file and return its contents as a pandas
18
+ Dataframe.
19
+ :param filename: .dbc filename
20
+ :param encoding: encoding of the data
21
+ :return: Pandas Dataframe.
22
+ """
23
+ with NamedTemporaryFile() as tf:
24
+ dbc2dbf(filename, tf.name.encode())
25
+ dbf = DBF(tf.name, encoding=encoding)
26
+ df = pd.DataFrame(list(dbf))
27
+ return df
28
+
29
+
30
+ def dbc2dbf(infile, outfile):
31
+ """
32
+ Converts a DATASUS dbc file to a DBF database.
33
+ :param infile: .dbc file name
34
+ :param outfile: name of the .dbf file to be created.
35
+ """
36
+ p = ffi.new('char[]', os.path.abspath(infile))
37
+ q = ffi.new('char[]', os.path.abspath(outfile))
38
+
39
+ lib.dbc2dbf([p], [q])
40
+
41
+ print(os.path.exists(outfile))
42
+
43
+ if __name__ == "__main__":
44
+ dbc2dbf(b'/tmp/DNRJ2014.dbc', b'/tmp/output.dbf')
45
+
PySUS-0.1.1/setup.cfg ADDED
@@ -0,0 +1,5 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+ tag_svn_revision = 0
5
+
PySUS-0.1.1/setup.py ADDED
@@ -0,0 +1,23 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ with open('README.md') as f:
4
+ ld = f.read()
5
+
6
+ setup(
7
+ name='PySUS',
8
+ version='0.1.1',
9
+ packages=find_packages(),
10
+ package_data={
11
+ '': ['*.c', '*.h', '*.o', '*.so']
12
+ },
13
+ zip_safe=False,
14
+ url='',
15
+ license='gpl-v3',
16
+ author='Flavio Codeco Coelho',
17
+ author_email='fccoelho@gmail.com',
18
+ description="Tools for dealing with Brazil's Public health data",
19
+ long_description=ld,
20
+ setup_requires=['cffi>=1.0.0'],
21
+ cffi_modules=["pysus/utilities/_build_readdbc.py:ffibuilder"],
22
+ install_requires=['pandas', 'dbfread', 'cffi>=1.0.0']
23
+ )