├── .gitignore ├── LICENSE.txt ├── README.md ├── anoms.py ├── breakout.py ├── requirements.txt ├── setup.py ├── src ├── edm-multi.cpp ├── edm-multi.h ├── edm-per.cpp ├── edm-per.h ├── edmTail.cpp ├── edmTail.h ├── edmx.cpp ├── edmx.h ├── helper.cpp ├── helper.h └── pywrapper.cpp └── tests ├── __init__.py ├── anom_breakout_data.txt ├── anoms_test.py ├── breakout_test.py ├── expected_both.txt ├── expected_longterm.txt ├── expected_longterm_onlylast.txt ├── expected_neg.txt ├── expected_onlylast.txt ├── expected_pos.txt ├── expected_threshold_med.txt ├── expected_threshold_p95.txt ├── expected_threshold_p99.txt ├── raw_data.txt └── scribe_data.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *~ 3 | *.iml 4 | build/ 5 | dist/ 6 | *.egg-info/ 7 | *.so 8 | *.swp 9 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AnomalyDetection and BreakoutDetection in python 2 | This is a python implementation of Twitter's AnomalyDetection and BreakoutDetection. 3 | 4 | ## Install 5 | The dependencies contain C++ and Fortran code, so that you need gcc installed. 6 | Checkout the code, enter the folder and run: 7 | ``` 8 | pip install -r requirements.txt 9 | ``` 10 | When use this as a library, please include the line for "pyloess" from "requirements.txt" in your "requirements.txt". 11 | 12 | ## Usage 13 | The parameters are the same as the AnomalyDetectionVec in Twitter's AnomalyDetection (except the plot related ones). 14 | You need to put your time series data into a list of float numbers: 15 | ``` 16 | from anoms import detect_anoms 17 | from breakout import detect_breakout 18 | 19 | x = list() 20 | 21 | \# put the data into x 22 | 23 | res = detect_anoms(x, max_anoms=0.02, alpha=0.01, direction='both') 24 | ``` 25 | `res` will be a list of int numbers, consists the index of detected anomalies in `x`. 26 | If `e_value=True` is set, `res` will be a tuple, 27 | whose first value is the list of index of detected anomalies 28 | and the second value is the list of expected values. 29 | ``` 30 | res = detect_breakout(x, min_size=24, method='multi', beta=0.001, degree=1) 31 | ``` 32 | `res` will be a list of int numbers, consists the index of detected breakout in `x`. 33 | -------------------------------------------------------------------------------- /anoms.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from pyloess import stl 3 | from scipy.stats import t 4 | from pandas import Series 5 | from math import floor 6 | from breakout import detect_breakout 7 | import logging 8 | 9 | logger = logging.getLogger('indeed.anoms') 10 | 11 | 12 | def detect_anoms(x, period, max_anoms=0.10, alpha=0.05, direction='both', longterm_period=None, only_last=None, 13 | threshold=None, e_value=False, breakout_kwargs=None): 14 | """ 15 | Anomaly Detection Using Seasonal Hybrid ESD Test. 16 | :param x: a list of floats, which consists of the observations. 17 | :param period: int, the number of observations in a single period. 18 | :param max_anoms: float in (0, 0.49]. Maximum number of anomalies that S-H-ESD will detect as a percentage of the 19 | data 20 | :param alpha: float. The level of statistical significance with which to accept or reject anomalies. 21 | :param direction: string. Directionality of the anomalies to be detected. Options are: 'pos', 'neg', 'both'. 22 | 'pos' only reports positive going anomalies, 'neg' only reports negative going anomalies, 23 | 'both' report anomalies on both direction, 24 | :param longterm_period: int. Split x into lists of given size, and perform anomaly detection on them 25 | individually. 26 | :param only_last: int. Find and report anomalies only within a length in the tail of the time series. 27 | :param threshold: string. Only report positive going anomalies above the threshold specified. 28 | Options are: None, 'med_max', 'p95' and 'p99'. 29 | :param e_value: boolean. Returns an additional list containing the expected value. 30 | :param breakout_kwargs: dict. If given, use it as parameter to call breakout detection to improve the trends. 31 | :return: a list of int, consists of the index of the anomalies in x. 32 | If e_value is set to True, a list of float is returned as the second return value, which consists the 33 | expected values of each detected anomaly. 34 | """ 35 | if max_anoms > 0.49 or max_anoms <= 0: 36 | raise ValueError("max_anoms must be >0 and <= 0.49") 37 | if alpha <= 0: 38 | raise ValueError("alpha must greater than 0.") 39 | if longterm_period is None: 40 | longterm_period = len(x) 41 | for v in x: 42 | if np.isnan(v): 43 | raise ValueError("data contains NaN value.") 44 | ret = set() 45 | e_values = None 46 | if e_value: 47 | e_values = [None] * len(x) # To keep the expected values when e_value is set. 48 | for window_start in xrange(0, len(x), longterm_period): 49 | # If the data is too long, split the data into smaller windows, and do the anomaly detection on each window. 50 | window_end = min(len(x), window_start + longterm_period) 51 | if logger.isEnabledFor(logging.DEBUG): 52 | logger.debug("Start to process window: window_start=%s, window_end=%s" % 53 | (window_start, window_end)) 54 | # If the window size doesn't divide the total size, the last window doesn't have enough data. 55 | # In this case, adjust the start position of last window to make it having same size as previous windows. 56 | if window_end - window_start < longterm_period: 57 | window_start = window_end - longterm_period 58 | if logger.isEnabledFor(logging.DEBUG): 59 | logger.debug("The last window doesn't contain enough length of data. " 60 | "Adjusted the starting index. window_start=%s, window_end=%s" % (window_start, window_end)) 61 | window_x = x[window_start:window_end] 62 | if len(window_x) < period * 2: 63 | raise ValueError("Anom detection needs at least 2 periods worth of data.") 64 | window_ret = _detect_anomaly_for_one_window(window_x, period, max_anoms, alpha, direction, e_values, 65 | window_start, breakout_kwargs) 66 | if threshold: 67 | window_ret = _post_processing_threshold(window_x, period, window_ret, threshold) 68 | ret = ret.union(window_ret) 69 | if only_last: 70 | ret = _post_processing_only_last(x, ret, only_last) 71 | ret = sorted(ret) 72 | if e_value: 73 | return ret, map(lambda i: e_values[i], ret) 74 | return ret 75 | 76 | 77 | def _get_trends_by_median(x): 78 | median = np.median(x) 79 | return [median] * len(x) 80 | 81 | 82 | def _get_trends_by_breakout_detection(x, kwargs): 83 | # divide the data into parts using breakout detection, using the median of each part as the trend. 84 | ret_list = detect_breakout(x, **kwargs) 85 | if logger.isEnabledFor(logging.DEBUG): 86 | logger.debug("detect_breakout result: %s" % ret_list) 87 | last_loc = len(x) 88 | if last_loc not in ret_list: 89 | ret_list.append(last_loc) 90 | prev_loc = 0 91 | trends = [] 92 | for loc in ret_list: 93 | median = np.median(x[prev_loc:loc]) 94 | trends.extend([median] * (loc - prev_loc)) 95 | prev_loc = loc 96 | if logger.isEnabledFor(logging.DEBUG): 97 | logger.debug("detect_breakout trends: %s length=%s" % (trends, len(trends))) 98 | return trends 99 | 100 | 101 | def _detect_anomaly_for_one_window(x, period, max_anoms, alpha, direction, e_values, window_start, breakout_kwargs): 102 | # The core part of anomaly detection: 103 | # 1. Use STL to perform seasonal decomposition. 104 | # parameters are copied from R's stl() 105 | stl_ret = stl(x, np=period, ns=len(x) * 10 + 1, isdeg=0, robust=True, ni=1, no=15) 106 | # 2. Calculate residuals using seasonal from STL result and median as the trends. 107 | seasons = stl_ret['seasonal'] 108 | if e_values: # store the expected values if e_value is set 109 | trends = stl_ret['trend'] 110 | for i in range(0, len(x)): 111 | if e_values[window_start + i] is None: 112 | e_values[window_start + i] = floor(seasons[i] + trends[i]) 113 | if breakout_kwargs: 114 | trends = _get_trends_by_breakout_detection(x, breakout_kwargs) 115 | else: 116 | trends = _get_trends_by_median(x) 117 | residuals = [x[i] - seasons[i] - trends[i] for i in range(0, len(x))] 118 | # 3. Use ESD to find out outliers from residuals. These outliers' corresponding values in x are the anomalies 119 | max_anom_num = max(1, int(len(x) * max_anoms)) 120 | anom_index = _esd(residuals, max_anom_num, alpha, direction=direction) 121 | ret = set() 122 | for anom_i in anom_index: 123 | ret.add(window_start + anom_i) # convert the index to the index in x 124 | return ret 125 | 126 | 127 | def _post_processing_threshold(x, period, ret, threshold): 128 | # The threshold is calculated from the max values of each window. 129 | period_maxs = [] 130 | for i in xrange(0, len(x), period): 131 | period_maxs.append(max(x[i: min(len(x), i + period)])) 132 | thresh = 0 133 | if threshold == 'med_max': 134 | thresh = np.median(period_maxs) 135 | elif threshold == 'p95': 136 | thresh = np.percentile(period_maxs, 95) 137 | elif threshold == 'p99': 138 | thresh = np.percentile(period_maxs, 99) 139 | if logger.isEnabledFor(logging.DEBUG): 140 | logger.debug("threshold is True. threshold=%s, thresh=%s" % (threshold, thresh)) 141 | ret = set(filter(lambda index: x[index] >= thresh, ret)) 142 | return ret 143 | 144 | 145 | def _post_processing_only_last(x, ret, only_last): 146 | last_period_start = len(x) - only_last 147 | if logger.isEnabledFor(logging.DEBUG): 148 | logger.debug("only_last is set. Will remove all anomalies before index %s." % last_period_start) 149 | ret = set(filter(lambda value: value >= last_period_start, ret)) 150 | return ret 151 | 152 | 153 | _MAD_CONSTANT = 1.4826 # a magic number copied from R's mad() function 154 | 155 | 156 | def _esd(x, max_outlier, alpha, direction): 157 | """ 158 | The ESD test using median and MAD in the calculation of the test statistic. 159 | """ 160 | x = Series(x) 161 | n = len(x) 162 | outlier_index = [] 163 | for i in range(1, max_outlier + 1): 164 | median = x.median() 165 | mad = np.median([abs(value - median) for value in x]) * _MAD_CONSTANT 166 | if mad == 0: 167 | break 168 | if direction == 'both': 169 | ares = x.map(lambda value: abs(value - median) / mad) 170 | elif direction == 'pos': 171 | ares = x.map(lambda value: (value - median) / mad) 172 | elif direction == 'neg': 173 | ares = x.map(lambda value: (median - value) / mad) 174 | r_idx = ares.idxmax() 175 | r = ares[r_idx] 176 | if direction == 'both': 177 | p = 1.0 - alpha / (2 * (n - i + 1)) 178 | else: 179 | p = 1.0 - alpha / (n - i + 1) 180 | crit = t.ppf(p, n-i-1) 181 | lam = (n-i)*crit / np.sqrt((n-i-1+crit**2) * (n-i+1)) 182 | if logger.isEnabledFor(logging.DEBUG): 183 | logger.debug("%s/%s outlier. median=%s, mad=%s, r_idx=%s, r=%s, crit=%s, lam=%s" % 184 | (i, max_outlier, median, mad, r_idx, r, crit, lam)) 185 | if r > lam: 186 | outlier_index.append(r_idx) 187 | x = x.drop(r_idx) 188 | else: 189 | # The r keeps decreasing while lam keeps increasing. Therefore, when r is less than lam for the first time, 190 | # we can stop. 191 | break 192 | return outlier_index 193 | -------------------------------------------------------------------------------- /breakout.py: -------------------------------------------------------------------------------- 1 | from edm import edm_multi, edm_x, edm_tail, edm_percent 2 | import numpy as np 3 | import logging 4 | 5 | logger = logging.getLogger("indeed.breakout") 6 | 7 | _EDM_TAIL_QUANT = 0.5 8 | 9 | 10 | def detect_breakout(z, min_size=30, method='amoc', alpha=2, exact=True, sig_level=0.05, nperm=0, 11 | degree=1, beta=None, percent=None): 12 | """ 13 | Breakout Detector: Energy Divisive with Medians 14 | A technique for robustly, i.e., in the presence of anomalies, detecting single or multiple change points in 15 | univariate time series. 16 | :param z: list of floats. The input time series. 17 | :param min_size: int. The minimum number of observations between change points. 18 | :param method: string. Method must be one of either 'amoc' (At Most One Change) or 19 | 'multi' (Multiple Changes). For 'amoc' at most one change point location will be returned. 20 | :param alpha: float in (0, 2]. For 'amoc' method. The alpha parameter used to weight the distance 21 | between observations. 22 | :param exact: boolean. For 'amoc' method. True to use truemedians, False to use approximate medians 23 | when determining change points. 24 | :param sig_level: float in (0, 1). For 'amoc' method. Once a change point is found its statistical significance is 25 | determined through a hypothesis test. This is the significance. 26 | :param nperm: int >= 0. For 'amoc' method. The number of permutations to perform in order to obtain an approximate 27 | p-value. If 0 then then permutation test is not performed. 28 | :param degree: int, can take the values 0, 1 or 2. For 'multi' method. The degree of the penalization polynomial. 29 | :param beta: float. For 'multi' method. Used to further control the amount of penalization. 30 | :param percent: float. For 'multi' method. This value specifies the minimum percent change in the goodness of fit 31 | statistic to consider adding an additional change point. 32 | :return: list of int, containing the index of change points. 33 | """ 34 | if not isinstance(min_size, int) or min_size < 2: 35 | raise ValueError("min_size must be an int >= 2.") 36 | if method == 'amoc': 37 | multi = False 38 | if alpha > 2 or alpha <= 0: 39 | raise ValueError("alpha must be in the interval (0, 2]") 40 | if sig_level <= 0 or sig_level >= 1: 41 | raise ValueError("sig_level must be in interval (0, 1)") 42 | if not isinstance(nperm, int) or nperm < 0: 43 | raise ValueError("nperm must be an int greater than 0.") 44 | elif method == 'multi': 45 | multi = True 46 | if degree not in [0, 1, 2]: 47 | raise ValueError("degree must be 0, 1 or 2.") 48 | if beta is None and percent is None: 49 | raise ValueError("beta and percent can not be both None.") 50 | else: 51 | raise ValueError("method must be 'amoc' or 'multi'") 52 | for value in z: 53 | if np.isnan(value): 54 | raise ValueError("data contains NaN.") 55 | if not z: 56 | return [] 57 | z_max = max(z) 58 | z_min = min(z) 59 | distance = z_max - z_min 60 | if distance == 0: 61 | return [] 62 | z = [float(value - z_min) / distance for value in z] 63 | if multi: 64 | return _detect_multiple_breakout(z, min_size, beta, percent, degree) 65 | else: 66 | ret = _detect_single_breakout(z, min_size, exact, alpha, nperm, sig_level) 67 | return [ret] if ret is not None else [] 68 | 69 | 70 | def _detect_multiple_breakout(z, min_size, beta, percent, degree): 71 | if beta is None: 72 | if logger.isEnabledFor(logging.DEBUG): 73 | logger.debug("calling edm_percent") 74 | return edm_percent(z, min_size, percent, degree) 75 | if logger.isEnabledFor(logging.DEBUG): 76 | logger.debug("calling edm_multi") 77 | return edm_multi(z, min_size, beta, degree) 78 | 79 | 80 | def _detect_single_breakout(z, min_size, exact, alpha, nperm, sig_level): 81 | if exact: 82 | if logger.isEnabledFor(logging.DEBUG): 83 | logger.debug("calling edm_x") 84 | ret, stat = edm_x(z, min_size, alpha) 85 | else: 86 | if logger.isEnabledFor(logging.DEBUG): 87 | logger.debug("calling edm_tail") 88 | ret, stat = edm_tail(z, min_size, alpha, _EDM_TAIL_QUANT) 89 | if nperm == 0: 90 | return ret 91 | p_val = _permutation_test(z, min_size, stat, exact, alpha, nperm) 92 | return ret if p_val <= sig_level else None 93 | 94 | 95 | def _permutation_test(z, min_size, stat, exact, alpha, nperm): 96 | over = 1 97 | for i in range(0, nperm): 98 | z_perm = list(np.random.permutation(z)) 99 | if exact: 100 | if logger.isEnabledFor(logging.DEBUG): 101 | logger.debug("calling edm_x for nperm %s" % i) 102 | _, stat_perm = edm_x(z_perm, min_size, alpha) 103 | else: 104 | if logger.isEnabledFor(logging.DEBUG): 105 | logger.debug("calling edm_tail for nperm %s" % i) 106 | _, stat_perm = edm_tail(z_perm, min_size, alpha, _EDM_TAIL_QUANT) 107 | if stat_perm > stat: 108 | over += 1 109 | if logger.isEnabledFor(logging.DEBUG): 110 | logger.debug("over=%s, stat_perm=%s, stat=%s" % (over, stat_perm, stat)) 111 | p_val = float(over) / (nperm + 1) 112 | if logger.isEnabledFor(logging.DEBUG): 113 | logger.debug("over=%s, p_val=%s" % (over, p_val)) 114 | return p_val 115 | 116 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | -e git+git://github.com/andreas-h/pyloess.git@7415090e00c3987eecc44be2efcfbdaf038656e0#egg=pyloess 2 | -e . 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import setup, Extension 3 | 4 | 5 | def read(fname): 6 | return open(os.path.join(os.path.dirname(__file__), fname)).read() 7 | 8 | # pyloess requires numpy already installed before its installation. 9 | try: 10 | import numpy 11 | except ImportError: 12 | import pip 13 | pip.main(['install', 'numpy>=1.9.2']) 14 | 15 | 16 | _CPP_PATH = './src/' 17 | sources = [] 18 | for filename in ['pywrapper.cpp', 'edm-multi.cpp', 'edm-per.cpp', 'edmTail.cpp', 'edmx.cpp', 'helper.cpp']: 19 | source_file = _CPP_PATH + filename 20 | sources.append(_CPP_PATH + filename) 21 | 22 | module = Extension('edm', sources=sources) 23 | 24 | setup( 25 | name="anomaly-detection", 26 | version="0.0.1", 27 | description="A python implementation of https://github.com/twitter/AnomalyDetection", 28 | py_modules=['anoms', 'breakout'], 29 | install_requires=[ 30 | 'pandas>=0.12.0', 31 | 'scipy>=0.15.1', 32 | 'numpy>=1.9.2', 33 | 'pyloess' 34 | ], 35 | long_description=read('README.md'), 36 | ext_modules=[module] 37 | ) 38 | -------------------------------------------------------------------------------- /src/edm-multi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include"helper.h" 6 | 7 | //Z: time series 8 | //min_size: minimum segment size 9 | //beta: penalization term for the addition of a change point 10 | 11 | // [[Rcpp::export]] 12 | extern "C" std::vector EDM_multi(const std::vector& Z, const int min_size=24, double beta=0, const int degree=0){ 13 | 14 | //identify which type of penalization to use 15 | double (*G)(double); 16 | switch(degree){ 17 | case 1: G=Linear; 18 | break; 19 | case 2: G=Quadratic; 20 | break; 21 | default: G=Const; 22 | break; 23 | } 24 | 25 | int n = Z.size(); 26 | if(beta < 0)//assume that beta is a positive number 27 | beta = -beta; 28 | std::vector prev(n+1,0);//store optimal location of previous change point 29 | std::vector number(n+1,0);//store the number of change points in optimal segmentation 30 | std::vector F(n+1,-3);//store optimal statistic value 31 | //F[s] is calculated using observations { Z[0], Z[1], ..., Z[s-1] } 32 | 33 | //trees used to store the "upper half" of the considered observations 34 | std::multiset right_min, left_min; 35 | //trees used to store the "lower half" of the considered observations 36 | std::multiset > right_max, left_max; 37 | 38 | //Iterate over possible locations for the last change 39 | for(int s=2*min_size; s prev[t-1]){ 59 | for(int i=prev[t-1]; i F[s]){ 73 | number[s] = number[t] + 1; 74 | F[s] = tmp; 75 | prev[s] = t; 76 | } 77 | } 78 | } 79 | 80 | //obtain list of optimal change point estimates 81 | std::vector ret; 82 | int at = n; 83 | while(at){ 84 | if(prev[at])//don't insert 0 as a change point estimate 85 | ret.push_back(prev[at]); 86 | at = prev[at]; 87 | } 88 | sort(ret.begin(),ret.end()); 89 | return ret; 90 | } 91 | -------------------------------------------------------------------------------- /src/edm-multi.h: -------------------------------------------------------------------------------- 1 | #ifndef _EDM_MULTI_ 2 | #define _EDM_MULTI_ 3 | #include 4 | 5 | 6 | extern "C" std::vector EDM_multi(const std::vector& Z, const int min_size, double beta, const int degree); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /src/edm-per.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Penalizes based on percent chagne in the statistic value. 3 | Linear penalty means that each new breakout must result in an at least X% increast 4 | Quadratic penalty means that each new brekaout must result in at least an (X*k)% increase for k breakouts 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include"helper.h" 12 | 13 | // [[Rcpp::export]] 14 | extern "C" std::vector EDM_percent(const std::vector& Z, const int min_size=24, const double percent=0, const int degree=0){ 15 | //Z: time series 16 | //min_size: minimum segment size 17 | //beta: penalization term for the addition of a change point 18 | 19 | //identify which type of penalization to use 20 | double (*G)(double); 21 | switch(degree){ 22 | case 1: G=Linear; 23 | break; 24 | case 2: G=Quadratic; 25 | break; 26 | default: G=Const; 27 | break; 28 | } 29 | 30 | int n = Z.size(); 31 | 32 | std::vector prev(n+1,0);//store optimal location of previous change point 33 | std::vector number(n+1,0);//store the number of change points in optimal segmentation 34 | std::vector F(n+1,0);//store optimal statistic value 35 | //F[s] is calculated using observations { Z[0], Z[1], ..., Z[s-1] } 36 | 37 | //trees used to store the "upper half" of the considered observations 38 | std::multiset right_min, left_min; 39 | //trees used to store the "lower half" of the considered observations 40 | std::multiset > right_max, left_max; 41 | 42 | //Iterate over possible locations for the last change 43 | for(int s=2*min_size; s prev[t-1]){ 63 | for(int i=prev[t-1]; i(s-prev[t]),2) ); 74 | double tmp = F[t] + normalize * std::pow(static_cast(left_median - right_median),2); 75 | //Find best location for change point. check % condition later 76 | if(tmp > F[s]){ 77 | number[s] = number[t] + 1; 78 | F[s] = tmp; 79 | prev[s] = t; 80 | } 81 | } 82 | //check to make sure we meet the percent change requirement 83 | if( prev[s]){ 84 | if(F[s] - F[prev[s]] < percent*G(number[prev[s]])*F[prev[s]]){ 85 | number[s] = number[prev[s]]; 86 | F[s] = F[prev[s]]; 87 | prev[s] = prev[prev[s]]; 88 | } 89 | } 90 | } 91 | 92 | //obtain list of optimal change point estimates 93 | std::vector ret; 94 | int at = n; 95 | while(at){ 96 | if(prev[at])//don't insert 0 as a change point estimate 97 | ret.push_back(prev[at]); 98 | at = prev[at]; 99 | } 100 | sort(ret.begin(),ret.end()); 101 | return ret; 102 | } 103 | -------------------------------------------------------------------------------- /src/edm-per.h: -------------------------------------------------------------------------------- 1 | #ifndef _EDM_PER_ 2 | #define _EDM_PER_ 3 | #include 4 | 5 | 6 | extern "C" std::vector EDM_percent(const std::vector& Z, const int min_size, const double percent, const int degree); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /src/edmTail.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This version calculates the between distance using the delta points around the change point estimate. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include"helper.h" 10 | 11 | //Class used to hold all the information about the 12 | //breakout location and the interval trees 13 | struct Information{ 14 | std::vector A, B, AB; 15 | double best_stat; 16 | int best_loc, best_t2; 17 | int min_size, b; 18 | 19 | Information(int, int); 20 | }; 21 | 22 | Information::Information(int bb, int m){ 23 | A = std::vector(1<<(bb+1)); 24 | B = std::vector(1<<(bb+1)); 25 | AB = std::vector(1<<(bb+1)); 26 | b = bb; 27 | best_stat = best_loc = best_t2 = -3; 28 | min_size = m; 29 | } 30 | 31 | /*void printInformation(Information& info){ 32 | std::cout<<"best_stat: "<& Z, Information& info, int& tau1, double quant, double alpha); 38 | void ForwardUpdate(std::vector& Z, Information& info, int& tau1, double quant, double alpha); 39 | 40 | int GetIndex(int B, double x){ 41 | //Get index of leaf node interval containing x 42 | return (int)std::ceil(std::abs(x) * (1<& x, double quant){ 46 | //Return approximate quantile based on the interval tree 47 | 48 | int N = x.size(); 49 | int k = std::ceil(x[1]*quant); 50 | double l=0, u=1; 51 | int i=1,j; 52 | while(i < N){ //Make sure that we do not go beyond the array bounds 53 | j = i<<1; 54 | if(j >= N) 55 | break; 56 | if(x[i] == k){//Exactly k elements in this node's subtree. So can terminate early 57 | //Return a weighted combination of the child node medians 58 | double lWeight = x[j]/(x[j]+x[j+1]); 59 | double rWeight = 1 - lWeight; 60 | double lu, rl; 61 | lu = (u+l)/2; 62 | rl = (u+lu)/2; 63 | return lWeight*(quant*(lu-l)+l) + rWeight*(quant*(u-rl)+rl); 64 | } 65 | else if(x[j] >= k){//More than k elements in node's left child's subtree, move to left child 66 | i = j; 67 | u = (l+u)/2; 68 | } 69 | else if(x[j] < k){//Not enough elements in node's left child's subtree, move to right child 70 | k -= x[j]; 71 | i = j+1; 72 | l = (l+u)/2; 73 | } 74 | } 75 | return quant*(u-l)+l; 76 | } 77 | 78 | std::vector AddToTree(int B, std::vector& x){ 79 | std::vector A(1<<(B+1)); 80 | std::vector::iterator i; 81 | for(i = x.begin(); i < x.end(); ++i){//Iterage over items we wish to add to the tree 82 | int index = GetIndex(B,*i); 83 | while(index){ 84 | ++A[index]; 85 | index /= 2; 86 | } 87 | } 88 | return A; 89 | } 90 | 91 | // [[Rcpp::export]] 92 | extern "C" EDMResult EDM_tail(std::vector& Z, const int min_size=24, const double alpha=2, const double quant=0.5){ 93 | 94 | int N = Z.size(); 95 | int eps = (int)std::ceil( std::log(N) ); 96 | eps = std::max( eps, 10 ); 97 | 98 | Information info(eps,min_size); 99 | 100 | int tau1 = info.min_size; 101 | int tau2 = tau1 * 2; 102 | 103 | 104 | 105 | //Populate trees and calculate statistic value for starting configuration of 106 | //2 min_size segments 107 | for(int i=0; i info.best_stat){ 167 | info.best_stat = stat; 168 | info.best_loc = tau1; 169 | info.best_t2 = tau2; 170 | } 171 | } 172 | 173 | bool forward_move = false; 174 | //Initial consideration of other possible locations for tau1 175 | while(tau1 < N-min_size){ 176 | //"warm start" to update tree and statistic value for other prefix series 177 | if(forward_move){ 178 | ForwardUpdate(Z, info, tau1, quant, alpha); 179 | } 180 | else{ 181 | BackwardUpdate(Z, info, tau1, quant, alpha); 182 | } 183 | forward_move = !forward_move; 184 | } 185 | return EDMResult(info.best_loc, info.best_stat); 186 | } 187 | 188 | void ForwardUpdate(std::vector& Z, Information& info, int& tau1, double quant, double alpha){ 189 | 190 | int min_size = info.min_size; 191 | int tau2 = tau1 + min_size; 192 | ++tau1; 193 | int N = Z.size(), index; 194 | //Update A tree 195 | for(int i=tau1-min_size; i info.best_stat){ 281 | info.best_stat = stat; 282 | info.best_loc = tau1; 283 | info.best_t2 = tau2; 284 | } 285 | } 286 | } 287 | 288 | void BackwardUpdate(std::vector& Z, Information& info, int& tau1, double quant, double alpha){ 289 | 290 | int min_size = info.min_size; 291 | int tau2 = tau1 + min_size; 292 | ++tau1; 293 | int N = Z.size(), index; 294 | //Update A tree 295 | for(int i=tau1-min_size; i=tau1+min_size; --tau2){ 373 | index = GetIndex(info.b,Z[tau2-1]-Z[tau2-2]); 374 | while(index){ 375 | --info.B[index]; 376 | index /= 2; 377 | } 378 | qb = std::pow( GetQuantile(info.B,quant), alpha); 379 | 380 | double stat = 2*qc - qa - qb; 381 | stat *= (double)(tau2-tau1)*tau1/tau2; 382 | 383 | if(stat > info.best_stat){ 384 | info.best_stat = stat; 385 | info.best_loc = tau1; 386 | info.best_t2 = tau2; 387 | } 388 | } 389 | } 390 | -------------------------------------------------------------------------------- /src/edmTail.h: -------------------------------------------------------------------------------- 1 | #ifndef _EDM_TAIL_ 2 | #define _EDM_TAIL_ 3 | #include 4 | #include"helper.h" 5 | 6 | 7 | extern "C" EDMResult EDM_tail(std::vector& Z, const int min_size, const double alpha, const double quant); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /src/edmx.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Robust estimation of 2[mean(X)-mean(Y)]^2 time normalization factor 3 | This is the E-Divisive E-statistic when alpha = 2 4 | Instead of calculating mean(X) we calculate median(X), and similarly for Y 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include"helper.h" 13 | 14 | 15 | void AddToHeaps(std::priority_queue, std::greater >& m, 16 | std::priority_queue& M, double x); 17 | 18 | double getMedian(const std::priority_queue, std::greater >& m, 19 | const std::priority_queue& M); 20 | 21 | double Median(const std::vector& Z, int a, int b){ 22 | //Calculate the median of the values in { Z[i] : a <= i < b } 23 | std::vector x(Z.begin()+a, Z.begin()+b); 24 | int n = x.size(), h=n/2; 25 | 26 | if( n&1){// n is odd 27 | std::nth_element(x.begin(),x.begin()+h,x.end()); 28 | return x[h]; 29 | } 30 | else{// n is even 31 | double y1,y2; 32 | std::nth_element(x.begin(),x.begin()+h,x.end()); 33 | y1 = x[h]; 34 | std::nth_element(x.begin(),x.begin()+h-1,x.end()); 35 | y2 = x[h-1]; 36 | return (y1+y2)/2; 37 | } 38 | } 39 | 40 | // [[Rcpp::export]] 41 | extern "C" EDMResult EDMX(const std::vector& Z, int min_size = 24, double alpha=2){ 42 | 43 | alpha = 2; //Not used, just here for uniform funciton signature 44 | 45 | std::priority_queue LeftMax; 46 | std::priority_queue, std::greater > LeftMin; 47 | 48 | double stat = -3, stat_best = -3, t1=0.0, t2; 49 | int tau1, tau2; 50 | int N = Z.size(); 51 | for(int i=0; i RightMax; 57 | std::priority_queue, std::greater > RightMin; 58 | double medL = getMedian(LeftMin, LeftMax); 59 | 60 | //Add first set of elements to the heaps for the right segment 61 | for(std::vector::const_iterator i=Z.begin()+tau1; i!=Z.begin()+tau1+min_size-1; ++i) 62 | AddToHeaps(RightMin, RightMax, *i); 63 | 64 | for(tau2=tau1+min_size; tau2 stat_best){ 72 | t1 = tau1; 73 | t2 = tau2; 74 | stat_best = stat; 75 | } 76 | } 77 | } 78 | return EDMResult(t1, stat_best); 79 | } 80 | 81 | // Use 2 heaps to keep track of the median (can also be adjusted for other quantiles). One heap 82 | // for the "larger" and one heap for the "smaller" observations. Simple to update for streaming 83 | // data ( O(log n) ) and find median ( O(1) ). 84 | 85 | double getMedian(const std::priority_queue, std::greater >& m, 86 | const std::priority_queue& M){ 87 | 88 | if(m.size() > M.size()) // There are an odd number of observations 89 | return m.top(); 90 | else if(M.size() > m.size()) // There are an odd number of observations 91 | return M.top(); 92 | else // There are an even number of obersations 93 | return (m.top()+M.top())/2; 94 | } 95 | 96 | void AddToHeaps(std::priority_queue, std::greater >& m, 97 | std::priority_queue& M, double x){ 98 | 99 | // decide on initial heap to place element into 100 | if(m.empty() || x < m.top()) 101 | M.push(x); 102 | else 103 | m.push(x); 104 | // make sure that heaps are balanced 105 | if(m.size() > M.size() + 1){ 106 | M.push( m.top() ); 107 | m.pop(); 108 | } 109 | else if(M.size() > m.size() + 1){ 110 | m.push( M.top() ); 111 | M.pop(); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/edmx.h: -------------------------------------------------------------------------------- 1 | #ifndef _EDM_X_ 2 | #define _EDM_X_ 3 | #include 4 | #include"helper.h" 5 | 6 | 7 | extern "C" EDMResult EDMX(const std::vector& Z, const int min_size, const double alpha); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /src/helper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include"helper.h" 5 | 6 | extern double Linear(const double x){ return 1;} 7 | extern double Const(const double x){ return 0;} 8 | extern double Quadratic(const double x){ return 2*x+1;} 9 | 10 | 11 | /* 12 | Use 2 multisets (red-black trees) to keep track of the median. One tree for the larger (m) and 13 | one for the smaller (M) observations. Insertion and deletion in O(log(n)) and find 14 | the median in O(1), additional memory use is O(n). 15 | */ 16 | 17 | //insert x into the appropriate tree 18 | extern void insert_element(std::multiset& m, std::multiset >& M, double x){ 19 | 20 | if(m.empty() || x < *(m.begin())) 21 | M.insert(x); 22 | else 23 | m.insert(x); 24 | if(m.size() > M.size() + 1){ 25 | std::multiset::iterator i; 26 | i = m.begin(); 27 | M.insert(*i); 28 | m.erase(m.begin()); 29 | } 30 | else if(M.size() > m.size() + 1){ 31 | std::multiset >::iterator i; 32 | i = M.begin(); 33 | m.insert(*i); 34 | M.erase(M.begin()); 35 | } 36 | } 37 | 38 | //given a pair of trees obtain the median 39 | extern double get_median(std::multiset& m, std::multiset >& M){ 40 | 41 | if(m.size() > M.size()) 42 | return *(m.begin()); 43 | else if(M.size() > m.size()) 44 | return *(M.begin()); 45 | else 46 | return ( *(M.begin()) + *(m.begin()) )/2; 47 | } 48 | 49 | //remove x from the tree, if multiple copies of x exist only remove 1 50 | //since this method is never called by the user directly it is assumed 51 | //that there is at least 1 copy of x 52 | extern void remove_element(std::multiset& m, std::multiset >& M, const double x){ 53 | 54 | if(x < *(m.begin())){ 55 | std::multiset >::iterator i = M.find(x); 56 | M.erase(i); 57 | } 58 | else{ 59 | std::multiset::iterator i = m.find(x); 60 | m.erase(i); 61 | } 62 | if(m.size() > M.size() + 1){ 63 | std::multiset::iterator i; 64 | i = m.begin(); 65 | M.insert(*i); 66 | m.erase(m.begin()); 67 | } 68 | else if(M.size() > m.size() + 1){ 69 | std::multiset >::iterator i; 70 | i = M.begin(); 71 | m.insert(*i); 72 | M.erase(M.begin()); 73 | } 74 | } 75 | 76 | EDMResult::EDMResult(const int best_loc, const double best_stat) { 77 | this->best_loc = best_loc; 78 | this->best_stat = best_stat; 79 | } 80 | -------------------------------------------------------------------------------- /src/helper.h: -------------------------------------------------------------------------------- 1 | #ifndef my_help_func 2 | #define my_help_func 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | double get_median(std::multiset&, std::multiset >&); 10 | void insert_element(std::multiset&, std::multiset >&, const double); 11 | void remove_element(std::multiset&, std::multiset >&, const double); 12 | 13 | extern double Linear(const double x); 14 | extern double Const(const double x); 15 | extern double Quadratic(const double x); 16 | 17 | class EDMResult { 18 | public: 19 | int best_loc; 20 | double best_stat; 21 | EDMResult(const int, const double); 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/pywrapper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"edm-multi.h" 4 | #include"edm-per.h" 5 | #include"edmTail.h" 6 | #include"edmx.h" 7 | 8 | 9 | std::vector to_vector(PyObject *pyList) { 10 | Py_ssize_t list_len = PyList_Size(pyList); 11 | std::vector valueVector; 12 | for(Py_ssize_t i=0; i &ret) { 21 | int ret_len = ret.size(); 22 | PyObject *pyRetList = PyList_New(ret_len); 23 | for(int i=0; i Z = to_vector(pyList); 44 | std::vector ret = EDM_multi(Z, min_size, beta, degree); 45 | return to_pylist(ret); 46 | } 47 | 48 | static PyObject* EDM_percent_wrapper(PyObject *self, PyObject *args) { 49 | PyObject *pyList; 50 | int min_size; 51 | double percent; 52 | int degree; 53 | PyArg_ParseTuple(args, "Oidi", &pyList, &min_size, &percent, °ree); 54 | std::vector Z = to_vector(pyList); 55 | std::vector ret = EDM_percent(Z, min_size, percent, degree); 56 | return to_pylist(ret); 57 | } 58 | 59 | static PyObject* EDM_tail_wrapper(PyObject *self, PyObject *args) { 60 | PyObject *pyList; 61 | int min_size; 62 | double alpha; 63 | double quant; 64 | PyArg_ParseTuple(args, "Oidd", &pyList, &min_size, &alpha, &quant); 65 | std::vector Z = to_vector(pyList); 66 | EDMResult ret = EDM_tail(Z, min_size, alpha, quant); 67 | return to_pytuple(ret.best_loc, ret.best_stat); 68 | } 69 | 70 | static PyObject* EDM_x_wrapper(PyObject *self, PyObject *args) { 71 | PyObject *pyList; 72 | int min_size; 73 | double alpha; 74 | PyArg_ParseTuple(args, "Oid", &pyList, &min_size, &alpha); 75 | std::vector Z = to_vector(pyList); 76 | EDMResult ret = EDMX(Z, min_size, alpha); 77 | return to_pytuple(ret.best_loc, ret.best_stat); 78 | } 79 | 80 | static PyMethodDef edmMethods[] = { 81 | {"edm_multi", EDM_multi_wrapper, METH_VARARGS, "EDM Multi"}, 82 | {"edm_percent", EDM_percent_wrapper, METH_VARARGS, "EDM Percent"}, 83 | {"edm_tail", EDM_tail_wrapper, METH_VARARGS, "EDM Tail"}, 84 | {"edm_x", EDM_x_wrapper, METH_VARARGS, "EDM X"}, 85 | {NULL, NULL, 0, NULL} /* Sentinel */ 86 | }; 87 | extern "C" void initedm(void) { 88 | (void) Py_InitModule("edm", edmMethods); 89 | } 90 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import sys 3 | 4 | logger = logging.getLogger('indeed') 5 | logger.setLevel(logging.DEBUG) 6 | logger.addHandler(logging.StreamHandler(sys.stdout)) 7 | -------------------------------------------------------------------------------- /tests/anom_breakout_data.txt: -------------------------------------------------------------------------------- 1 | 98661 2 | 90838 3 | 115577 4 | 156157 5 | 149326 6 | 137430 7 | 128477 8 | 101070 9 | 89014 10 | 111482 11 | 152514 12 | 140990 13 | 129535 14 | 117823 15 | 107921 16 | 99013 17 | 122146 18 | 157169 19 | 142685 20 | 136091 21 | 132255 22 | 105010 23 | 91416 24 | 109886 25 | 150593 26 | 151661 27 | 137599 28 | 117487 29 | 89129 30 | 75168 31 | 103702 32 | 162456 33 | 201787 34 | 209951 35 | 193875 36 | 169426 37 | 151683 38 | 195004 39 | 243620 40 | 247397 41 | 262154 42 | 246290 43 | 215785 44 | 195394 45 | 233457 46 | 296383 47 | 287097 48 | 287857 49 | 270806 50 | 237375 51 | 207232 52 | 236679 53 | 302098 54 | 286822 55 | 282890 56 | 278101 57 | 238554 58 | 214978 59 | 247097 60 | 308226 61 | 284886 62 | 257507 63 | 232215 64 | 215723 65 | 195825 66 | 257706 67 | 328275 68 | 311134 69 | 308784 70 | 272147 71 | 227808 72 | 201230 73 | 245762 74 | 304675 75 | -------------------------------------------------------------------------------- /tests/anoms_test.py: -------------------------------------------------------------------------------- 1 | import tests 2 | import unittest 3 | from anoms import detect_anoms 4 | import numpy as np 5 | 6 | 7 | # Test that we can get exactly the same result as Twitter's AnomalyDetection library. 8 | # The 'raw_data.txt' is containing the same data as 'raw_data.R'. 9 | # The expected_*.txt files are containing the same result from 'vec_anom_detection.R' using same parameters. 10 | def read_twitter_raw_data(filename): 11 | x = [] 12 | with open(filename, 'r') as f: 13 | for line in f.readlines(): 14 | x.append(float(line)) 15 | return x 16 | 17 | 18 | def _read_twitter_test_result(filename): 19 | index = [] 20 | e_values = [] 21 | with open(filename, 'r') as f: 22 | for line in f.readlines(): 23 | parts = line.split(' ') 24 | index.append(int(parts[0]) - 1) # The 'index' column. R's array index is from 1, not 0 25 | e_values.append(float(parts[2])) # The 'expected_value' column 26 | return index, e_values 27 | 28 | 29 | class TestDetectAnoms(unittest.TestCase): 30 | def test_seasonal_data(self): 31 | """ 32 | An example from real Indeed data. Numbers are the click count of one of Indeed pages. 33 | The last number is an anomaly caused by a holiday. 34 | """ 35 | x = [534592, 854369, 868702, 852728, 773757, 618216, 423549, 497898, 836237, 883591, 888337, 818443, 660449, 36 | 482778, 477392, 904671, 943225, 918105, 843145, 685644, 511239, 558484, 894195, 927928, 919406, 852359, 37 | 658974, 473478, 458006, 587811] 38 | anoms_index = detect_anoms(x, 7) 39 | self.assertEqual([29], anoms_index) 40 | 41 | def test_constants(self): 42 | ret = detect_anoms([1] * 1000, 14, direction='both') 43 | self.assertEqual([], ret) 44 | 45 | def test_twitter_data_both(self): 46 | """ 47 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 48 | Set the direction=both 49 | """ 50 | x = read_twitter_raw_data('tests/raw_data.txt') 51 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_both.txt') 52 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', e_value=True) 53 | self.assertListEqual(expected_index, index) 54 | self.assertListEqual(expected_e_values, e_values) 55 | 56 | def test_twitter_data_pos(self): 57 | """ 58 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 59 | Set the direction=pos 60 | """ 61 | x = read_twitter_raw_data('tests/raw_data.txt') 62 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_pos.txt') 63 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='pos', e_value=True) 64 | self.assertListEqual(expected_index, index) 65 | self.assertListEqual(expected_e_values, e_values) 66 | 67 | def test_twitter_data_neg(self): 68 | """ 69 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 70 | Set the direction=neg 71 | """ 72 | x = read_twitter_raw_data('tests/raw_data.txt') 73 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_neg.txt') 74 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='neg', e_value=True) 75 | self.assertListEqual(expected_index, index) 76 | self.assertListEqual(expected_e_values, e_values) 77 | 78 | def test_twitter_data_onlylast(self): 79 | """ 80 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 81 | Set the only_last=True 82 | """ 83 | x = read_twitter_raw_data('tests/raw_data.txt') 84 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_onlylast.txt') 85 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', only_last=1440, e_value=True) 86 | self.assertListEqual(expected_index, index) 87 | self.assertListEqual(expected_e_values, e_values) 88 | 89 | def test_twitter_data_threshold_med(self): 90 | """ 91 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 92 | Set the threshold=med_max 93 | """ 94 | x = read_twitter_raw_data('tests/raw_data.txt') 95 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_threshold_med.txt') 96 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', threshold='med_max', e_value=True) 97 | self.assertListEqual(expected_index, index) 98 | self.assertListEqual(expected_e_values, e_values) 99 | 100 | def test_twitter_data_threshold_p95(self): 101 | """ 102 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 103 | Set the threshold=p95 104 | """ 105 | x = read_twitter_raw_data('tests/raw_data.txt') 106 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_threshold_p95.txt') 107 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', threshold='p95', e_value=True) 108 | self.assertListEqual(expected_index, index) 109 | self.assertListEqual(expected_e_values, e_values) 110 | 111 | def test_twitter_data_threshold_p99(self): 112 | """ 113 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 114 | Set the threshold=p99 115 | """ 116 | x = read_twitter_raw_data('tests/raw_data.txt') 117 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_threshold_p99.txt') 118 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', threshold='p99', e_value=True) 119 | self.assertListEqual(expected_index, index) 120 | self.assertListEqual(expected_e_values, e_values) 121 | 122 | def test_twitter_data_longterm(self): 123 | """ 124 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 125 | Set the longterm_period to 1440 * 7 126 | """ 127 | x = read_twitter_raw_data('tests/raw_data.txt') 128 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_longterm.txt') 129 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', longterm_period=1440 * 7, 130 | e_value=True) 131 | self.assertListEqual(expected_index, index) 132 | self.assertListEqual(expected_e_values, e_values) 133 | 134 | def test_twitter_data_longterm_onlylast(self): 135 | """ 136 | Use the same test data from Twitter's library. The result will be exactly the same as Twitter's. 137 | Set the longterm_period to 1440 * 7 138 | """ 139 | x = read_twitter_raw_data('tests/raw_data.txt') 140 | expected_index, expected_e_values = _read_twitter_test_result('tests/expected_longterm_onlylast.txt') 141 | index, e_values = detect_anoms(x, 1440, max_anoms=0.02, direction='both', longterm_period=1440 * 7, 142 | only_last=1440, e_value=True) 143 | self.assertListEqual(expected_index, index) 144 | self.assertListEqual(expected_e_values, e_values) 145 | 146 | def test_illegal_data_and_parameters(self): 147 | self.assertRaises(ValueError, detect_anoms, [1] * 1000, 14, max_anoms=0.5) 148 | self.assertRaises(ValueError, detect_anoms, [1] * 1000, 14, max_anoms=0) 149 | self.assertRaises(ValueError, detect_anoms, [1] * 1000, 14, alpha=0) 150 | self.assertRaises(ValueError, detect_anoms, [1] * 27, 14) # time series' length is less than period * 2. 151 | x = [1] * 1000 152 | x[999] = np.nan 153 | self.assertRaises(ValueError, detect_anoms, x, 14) 154 | 155 | 156 | class TestAnomWithBreakout(unittest.TestCase): 157 | def setUp(self): 158 | # A breakout happened in the position 33: 159 | self.data = read_twitter_raw_data('tests/anom_breakout_data.txt') 160 | 161 | def test_anom_without_breakout(self): 162 | # when the breakout just happened, reports it as anomaly, which is correct. 163 | ret = detect_anoms(self.data[3:33], 7, max_anoms=0.01, only_last=1) 164 | self.assertEqual([29], ret) 165 | # after the time windows moved forward, still reports the last data is anomaly, which is bad. 166 | ret = detect_anoms(self.data[8:38], 7, max_anoms=0.01, only_last=1) 167 | self.assertEqual([29], ret) 168 | ret = detect_anoms(self.data[12:42], 7, max_anoms=0.01, only_last=1) 169 | self.assertEqual([29], ret) 170 | 171 | def test_anom_with_breakout(self): 172 | breakout_kwargs = {'min_size': 7, 'method': 'multi', 'beta': 0.008} 173 | # when the breakout just happened, reports it as anomaly, which is correct. 174 | ret = detect_anoms(self.data[3:33], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) 175 | self.assertEqual([29], ret) 176 | # after the time window moved forward, detects the breakout and stops reporting the last point as anomaly. 177 | ret = detect_anoms(self.data[8:38], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) 178 | self.assertEqual([], ret) 179 | ret = detect_anoms(self.data[12:42], 7, max_anoms=0.01, only_last=1, breakout_kwargs=breakout_kwargs) 180 | self.assertEqual([], ret) 181 | -------------------------------------------------------------------------------- /tests/breakout_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from tests.anoms_test import read_twitter_raw_data 3 | from breakout import detect_breakout 4 | import numpy as np 5 | 6 | 7 | class TestBreakoutDetection(unittest.TestCase): 8 | def setUp(self): 9 | self.data = read_twitter_raw_data('tests/scribe_data.txt') 10 | 11 | def test_edm_multi(self): 12 | ret_list = detect_breakout(self.data, min_size=24, method='multi', beta=0.001, degree=1) 13 | self.assertEqual([47, 87], ret_list) 14 | 15 | def test_edm_percent(self): 16 | ret_list = detect_breakout(self.data, min_size=24, method='multi', percent=0.1, degree=1) 17 | self.assertEqual([26, 51, 106], ret_list) 18 | 19 | def test_edm_x_no_nperm(self): 20 | ret_list = detect_breakout(self.data, min_size=24, method='amoc', exact=True, sig_level=0.05, nperm=0) 21 | self.assertEqual([95], ret_list) 22 | 23 | def test_edm_x_nperm_has_ret(self): 24 | ret_list = detect_breakout(self.data, min_size=24, method='amoc', exact=True, sig_level=0.9, nperm=10) 25 | self.assertEqual([95], ret_list) 26 | 27 | def test_edm_x_nperm_no_ret(self): 28 | ret_list = detect_breakout(self.data, min_size=24, method='amoc', exact=True, sig_level=0.001, nperm=10) 29 | self.assertEqual([], ret_list) 30 | 31 | def test_edm_tail_no_nperm(self): 32 | ret_list = detect_breakout(self.data, min_size=24, method='amoc', exact=False, sig_level=0.05, nperm=0) 33 | self.assertEqual([47], ret_list) 34 | 35 | def test_edm_tail_nperm_has_ret(self): 36 | ret_list = detect_breakout(self.data, min_size=24, method='amoc', exact=False, sig_level=0.9, nperm=10) 37 | self.assertEqual([47], ret_list) 38 | 39 | def test_edm_tail_nperm_no_ret(self): 40 | ret_list = detect_breakout(self.data, min_size=24, method='amoc', exact=False, sig_level=0.001, nperm=10) 41 | self.assertEqual([], ret_list) 42 | 43 | def test_invalid_parameters(self): 44 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=0, 45 | method='amoc', exact=False, sig_level=0.001, nperm=10) 46 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=1.5, 47 | method='amoc', exact=False, sig_level=0.001, nperm=10) 48 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 49 | method='amoc', exact=False, alpha=0, sig_level=0.001, nperm=10) 50 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 51 | method='amoc', exact=False, alpha=2.1, sig_level=0.001, nperm=10) 52 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 53 | method='amoc', exact=False, sig_level=0.0, nperm=10) 54 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 55 | method='amoc', exact=False, sig_level=1.0, nperm=10) 56 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 57 | method='amoc', exact=False, sig_level=0.001, nperm=-1) 58 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 59 | method='amoc', exact=False, sig_level=0.001, nperm=1.1) 60 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 61 | method='multi', beta=0.008, degree=3) 62 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 63 | method='multi', degree=0) 64 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 65 | method='wrong_method', exact=False, sig_level=0.001, nperm=10) 66 | 67 | def test_empty(self): 68 | ret_list = detect_breakout([], min_size=24, method='multi', beta=0.001, degree=1) 69 | self.assertEqual([], ret_list) 70 | 71 | def test_constant(self): 72 | ret_list = detect_breakout([10] * 100, min_size=24, method='multi', beta=0.001, degree=1) 73 | self.assertEqual([], ret_list) 74 | 75 | def test_nan(self): 76 | self.data[10] = np.nan 77 | self.assertRaises(ValueError, detect_breakout, self.data, min_size=30, 78 | method='amoc', exact=False, sig_level=0.001, nperm=10) 79 | 80 | def test_int_values(self): 81 | # make sure the code still works if the values are int. 82 | z = [int(value) for value in self.data] 83 | ret_list = detect_breakout(z, min_size=24, method='multi', beta=0.001, degree=1) 84 | self.assertEqual([47, 87], ret_list) 85 | -------------------------------------------------------------------------------- /tests/expected_both.txt: -------------------------------------------------------------------------------- 1 | 125 21.351 129 2 | 5320 193.1036 97 3 | 6224 148.174 96 4 | 7426 52.7478 117 5 | 7428 49.6582 107 6 | 7430 35.6067 104 7 | 7431 32.5045 112 8 | 7432 30.0555 110 9 | 7433 31.2614 107 10 | 7434 30.2551 102 11 | 7435 27.386 102 12 | 7436 28.9807 105 13 | 7437 29.0844 100 14 | 7438 26.9185 101 15 | 7439 26.4621 100 16 | 7440 27.218 99 17 | 7441 40.4268 122 18 | 7442 28.8811 110 19 | 7443 27.1294 109 20 | 7444 26.9913 106 21 | 7445 26.7741 105 22 | 7446 30.6972 111 23 | 7447 27.6085 105 24 | 7448 25.2841 104 25 | 7449 25.2264 104 26 | 7450 25.1927 102 27 | 7451 27.6501 112 28 | 7452 24.8858 106 29 | 7453 24.8099 106 30 | 7454 24.295 103 31 | 7455 24.5221 102 32 | 7456 26.7354 117 33 | 7457 24.7589 108 34 | 7458 25.63 106 35 | 7459 24.6949 100 36 | 7460 23.6518 98 37 | 7461 27.6977 107 38 | 7462 25.8687 104 39 | 7463 24.6654 104 40 | 7464 24.6694 100 41 | 7465 24.4351 98 42 | 7466 25.7723 103 43 | 7467 24.2502 99 44 | 7468 25.0965 96 45 | 7469 23.9362 93 46 | 7470 24.4127 93 47 | 7471 32.8182 114 48 | 7472 27.4586 106 49 | 7473 26.3163 99 50 | 7474 30.3965 99 51 | 7475 26.9516 97 52 | 7476 26.8908 100 53 | 7477 28.9461 97 54 | 7478 28.9742 95 55 | 7479 30.0406 94 56 | 7480 28.8507 93 57 | 7481 32.7811 101 58 | 7482 27.7468 96 59 | 7483 30.8174 101 60 | 7484 28.5658 96 61 | 7485 27.6677 92 62 | 7486 31.0409 102 63 | 7576 154.775 105 64 | 7577 148.862 96 65 | 7583 165.787 94 66 | 7584 167.585 92 67 | 7585 170.349 92 68 | 7586 180.899 93 69 | 7587 170.513 91 70 | 7588 174.678 90 71 | 7589 164.735 88 72 | 7590 178.822 89 73 | 7591 198.326 107 74 | 7592 203.901 100 75 | 7593 200.309 92 76 | 7594 178.491 90 77 | 7595 167.748 90 78 | 7596 183.018 94 79 | 7597 176.769 93 80 | 7598 186.823 91 81 | 7599 183.66 91 82 | 7600 179.276 91 83 | 7601 197.283 96 84 | 7602 191.097 94 85 | 7603 194.67 95 86 | 7604 177.325 91 87 | 7605 173.758 89 88 | 7606 200.816 98 89 | 7607 186.235 92 90 | 7608 185.421 91 91 | 7609 178.958 90 92 | 7610 171.75 88 93 | 7611 203.231 95 94 | 7612 181.354 92 95 | 7613 186.778 87 96 | 7614 175.582 88 97 | 7615 176.125 89 98 | 7616 181.514 90 99 | 7617 175.261 87 100 | 7618 164.719 86 101 | 7621 170.736 106 102 | 7622 151.549 99 103 | 7623 149.412 97 104 | 7624 150.554 94 105 | 7626 149.541 97 106 | 10640 188.2908 88 107 | 13632 56.4691 97 108 | 13633 54.9415 98 109 | 13634 52.0359 93 110 | 13635 47.7313 91 111 | 13636 50.5876 105 112 | 13637 48.2846 99 113 | 13638 44.6438 96 114 | 13639 42.3077 95 115 | 13640 38.8363 94 116 | 13641 41.0145 100 117 | 13642 39.5523 97 118 | 13643 38.9117 96 119 | 13644 37.3052 93 120 | 13645 36.1725 93 121 | 13646 37.515 97 122 | 13647 38.1387 94 123 | 13648 39.5351 91 124 | 13649 38.1834 90 125 | 13650 37.5988 88 126 | 13651 43.6522 105 127 | 13652 47.9571 102 128 | 14348 210 150 129 | 14358 40 154 130 | 14368 250 152 131 | 14378 40 156 132 | -------------------------------------------------------------------------------- /tests/expected_longterm.txt: -------------------------------------------------------------------------------- 1 | 125 21.351 132 2 | 613 86.6964 112 3 | 2044 138.737 102 4 | 2316 141.448 111 5 | 2973 188.033 157 6 | 5320 193.1036 95 7 | 5732 201.864 171 8 | 5736 198.21 170 9 | 6212 133.898 102 10 | 6218 131.314 94 11 | 6219 136.575 96 12 | 6220 131.547 95 13 | 6221 132.48 102 14 | 6222 135.261 98 15 | 6223 147.37 104 16 | 6224 148.174 98 17 | 6225 129.572 96 18 | 6226 137.275 107 19 | 6227 139.755 103 20 | 6228 131.521 101 21 | 6229 130.279 100 22 | 6231 139.284 108 23 | 6236 129.279 99 24 | 6238 135.028 101 25 | 6239 128.803 99 26 | 6241 153.37 121 27 | 6242 144.616 109 28 | 6243 145.695 110 29 | 6244 147.652 108 30 | 6246 145.458 113 31 | 6253 135.518 105 32 | 7420 72.0985 107 33 | 7421 79.1242 115 34 | 7422 72.3928 110 35 | 7423 77.0147 110 36 | 7424 61.9894 108 37 | 7425 48.589 104 38 | 7426 52.7478 121 39 | 7427 50.9668 108 40 | 7428 49.6582 105 41 | 7429 51.3551 104 42 | 7430 35.6067 102 43 | 7431 32.5045 111 44 | 7432 30.0555 108 45 | 7433 31.2614 107 46 | 7434 30.2551 102 47 | 7435 27.386 101 48 | 7436 28.9807 104 49 | 7437 29.0844 99 50 | 7438 26.9185 99 51 | 7439 26.4621 100 52 | 7440 27.218 97 53 | 7441 40.4268 122 54 | 7442 28.8811 110 55 | 7443 27.1294 109 56 | 7444 26.9913 103 57 | 7445 26.7741 101 58 | 7446 30.6972 106 59 | 7447 27.6085 100 60 | 7448 25.2841 102 61 | 7449 25.2264 100 62 | 7450 25.1927 99 63 | 7451 27.6501 111 64 | 7452 24.8858 104 65 | 7453 24.8099 104 66 | 7454 24.295 102 67 | 7455 24.5221 98 68 | 7456 26.7354 118 69 | 7457 24.7589 106 70 | 7458 25.63 103 71 | 7459 24.6949 98 72 | 7460 23.6518 94 73 | 7461 27.6977 105 74 | 7462 25.8687 101 75 | 7463 24.6654 101 76 | 7464 24.6694 96 77 | 7465 24.4351 96 78 | 7466 25.7723 100 79 | 7467 24.2502 97 80 | 7468 25.0965 93 81 | 7469 23.9362 91 82 | 7470 24.4127 91 83 | 7471 32.8182 112 84 | 7472 27.4586 103 85 | 7473 26.3163 97 86 | 7474 30.3965 96 87 | 7475 26.9516 97 88 | 7476 26.8908 98 89 | 7477 28.9461 96 90 | 7478 28.9742 93 91 | 7479 30.0406 93 92 | 7480 28.8507 91 93 | 7481 32.7811 99 94 | 7482 27.7468 95 95 | 7483 30.8174 100 96 | 7484 28.5658 96 97 | 7485 27.6677 92 98 | 7486 31.0409 102 99 | 7487 42.3687 93 100 | 7488 48.337 94 101 | 7489 47.0822 93 102 | 7490 45.234 93 103 | 7491 54.7834 99 104 | 7575 127.933 89 105 | 7576 154.775 105 106 | 7577 148.862 91 107 | 7578 142.214 91 108 | 7579 138.171 90 109 | 7580 129.121 86 110 | 7581 147.028 95 111 | 7582 143.874 96 112 | 7583 165.787 93 113 | 7584 167.585 89 114 | 7585 170.349 90 115 | 7586 180.899 92 116 | 7587 170.513 88 117 | 7588 174.678 88 118 | 7589 164.735 87 119 | 7590 178.822 87 120 | 7591 198.326 106 121 | 7592 203.901 100 122 | 7593 200.309 91 123 | 7594 178.491 88 124 | 7595 167.748 89 125 | 7596 183.018 93 126 | 7597 176.769 92 127 | 7598 186.823 90 128 | 7599 183.66 88 129 | 7600 179.276 88 130 | 7601 197.283 93 131 | 7602 191.097 92 132 | 7603 194.67 91 133 | 7604 177.325 90 134 | 7605 173.758 87 135 | 7606 200.816 97 136 | 7607 186.235 90 137 | 7608 185.421 90 138 | 7609 178.958 88 139 | 7610 171.75 86 140 | 7611 203.231 93 141 | 7612 181.354 91 142 | 7613 186.778 85 143 | 7614 175.582 86 144 | 7615 176.125 87 145 | 7616 181.514 88 146 | 7617 175.261 85 147 | 7618 164.719 84 148 | 7619 136.325 86 149 | 7620 132.139 85 150 | 7621 170.736 105 151 | 7622 151.549 98 152 | 7623 149.412 95 153 | 7624 150.554 91 154 | 7625 137.062 92 155 | 7626 149.541 96 156 | 8713 200.985 166 157 | 8715 189.45 154 158 | 8717 196.807 159 159 | 8718 193.794 159 160 | 8732 196.37 157 161 | 8734 182.382 146 162 | 8742 184.99 149 163 | 8746 196.799 156 164 | 8751 191.932 151 165 | 8752 190.837 145 166 | 8754 176.116 136 167 | 8755 178.072 137 168 | 8761 193.973 159 169 | 8762 188.091 144 170 | 8763 184.829 144 171 | 8764 179.169 140 172 | 8766 180.098 144 173 | 8767 177.839 141 174 | 8776 184.17 146 175 | 8777 177.707 136 176 | 8784 165.733 130 177 | 8786 174.447 132 178 | 8787 162.432 127 179 | 8791 182.925 142 180 | 8792 172.684 131 181 | 8795 155.527 119 182 | 8796 163.375 127 183 | 8802 154.887 120 184 | 8803 162.636 126 185 | 8811 169.439 128 186 | 8812 160.829 123 187 | 8818 148.581 111 188 | 8821 170.769 132 189 | 8822 156.979 117 190 | 8825 147.02 111 191 | 8827 149.202 112 192 | 8828 148.559 113 193 | 8839 154.461 111 194 | 8841 153.255 116 195 | 8853 143.226 105 196 | 9481 90.8554 118 197 | 10640 188.2908 89 198 | 13636 50.5876 106 199 | 13637 48.2846 102 200 | 13638 44.6438 96 201 | 13639 42.3077 96 202 | 13640 38.8363 94 203 | 13641 41.0145 100 204 | 13642 39.5523 97 205 | 13643 38.9117 97 206 | 13644 37.3052 93 207 | 13645 36.1725 94 208 | 13646 37.515 95 209 | 13647 38.1387 94 210 | 13648 39.5351 92 211 | 13649 38.1834 90 212 | 13651 43.6522 105 213 | 13652 47.9571 103 214 | 14358 40 152 215 | 14368 250 153 216 | 14378 40 153 217 | -------------------------------------------------------------------------------- /tests/expected_longterm_onlylast.txt: -------------------------------------------------------------------------------- 1 | 13636 50.5876 106 2 | 13637 48.2846 102 3 | 13638 44.6438 96 4 | 13639 42.3077 96 5 | 13640 38.8363 94 6 | 13641 41.0145 100 7 | 13642 39.5523 97 8 | 13643 38.9117 97 9 | 13644 37.3052 93 10 | 13645 36.1725 94 11 | 13646 37.515 95 12 | 13647 38.1387 94 13 | 13648 39.5351 92 14 | 13649 38.1834 90 15 | 13651 43.6522 105 16 | 13652 47.9571 103 17 | 14358 40 152 18 | 14368 250 153 19 | 14378 40 153 20 | -------------------------------------------------------------------------------- /tests/expected_neg.txt: -------------------------------------------------------------------------------- 1 | 125 21.351 129 2 | 7425 48.589 105 3 | 7426 52.7478 117 4 | 7427 50.9668 107 5 | 7428 49.6582 107 6 | 7430 35.6067 104 7 | 7431 32.5045 112 8 | 7432 30.0555 110 9 | 7433 31.2614 107 10 | 7434 30.2551 102 11 | 7435 27.386 102 12 | 7436 28.9807 105 13 | 7437 29.0844 100 14 | 7438 26.9185 101 15 | 7439 26.4621 100 16 | 7440 27.218 99 17 | 7441 40.4268 122 18 | 7442 28.8811 110 19 | 7443 27.1294 109 20 | 7444 26.9913 106 21 | 7445 26.7741 105 22 | 7446 30.6972 111 23 | 7447 27.6085 105 24 | 7448 25.2841 104 25 | 7449 25.2264 104 26 | 7450 25.1927 102 27 | 7451 27.6501 112 28 | 7452 24.8858 106 29 | 7453 24.8099 106 30 | 7454 24.295 103 31 | 7455 24.5221 102 32 | 7456 26.7354 117 33 | 7457 24.7589 108 34 | 7458 25.63 106 35 | 7459 24.6949 100 36 | 7460 23.6518 98 37 | 7461 27.6977 107 38 | 7462 25.8687 104 39 | 7463 24.6654 104 40 | 7464 24.6694 100 41 | 7465 24.4351 98 42 | 7466 25.7723 103 43 | 7467 24.2502 99 44 | 7468 25.0965 96 45 | 7469 23.9362 93 46 | 7470 24.4127 93 47 | 7471 32.8182 114 48 | 7472 27.4586 106 49 | 7473 26.3163 99 50 | 7474 30.3965 99 51 | 7475 26.9516 97 52 | 7476 26.8908 100 53 | 7477 28.9461 97 54 | 7478 28.9742 95 55 | 7479 30.0406 94 56 | 7480 28.8507 93 57 | 7481 32.7811 101 58 | 7482 27.7468 96 59 | 7483 30.8174 101 60 | 7484 28.5658 96 61 | 7485 27.6677 92 62 | 7486 31.0409 102 63 | 10221 90.3287 133 64 | 10222 87.9705 131 65 | 13632 56.4691 97 66 | 13633 54.9415 98 67 | 13634 52.0359 93 68 | 13635 47.7313 91 69 | 13636 50.5876 105 70 | 13637 48.2846 99 71 | 13638 44.6438 96 72 | 13639 42.3077 95 73 | 13640 38.8363 94 74 | 13641 41.0145 100 75 | 13642 39.5523 97 76 | 13643 38.9117 96 77 | 13644 37.3052 93 78 | 13645 36.1725 93 79 | 13646 37.515 97 80 | 13647 38.1387 94 81 | 13648 39.5351 91 82 | 13649 38.1834 90 83 | 13650 37.5988 88 84 | 13651 43.6522 105 85 | 13652 47.9571 102 86 | 14358 40 154 87 | 14378 40 156 88 | -------------------------------------------------------------------------------- /tests/expected_onlylast.txt: -------------------------------------------------------------------------------- 1 | 13632 56.4691 97 2 | 13633 54.9415 98 3 | 13634 52.0359 93 4 | 13635 47.7313 91 5 | 13636 50.5876 105 6 | 13637 48.2846 99 7 | 13638 44.6438 96 8 | 13639 42.3077 95 9 | 13640 38.8363 94 10 | 13641 41.0145 100 11 | 13642 39.5523 97 12 | 13643 38.9117 96 13 | 13644 37.3052 93 14 | 13645 36.1725 93 15 | 13646 37.515 97 16 | 13647 38.1387 94 17 | 13648 39.5351 91 18 | 13649 38.1834 90 19 | 13650 37.5988 88 20 | 13651 43.6522 105 21 | 13652 47.9571 102 22 | 14348 210 150 23 | 14358 40 154 24 | 14368 250 152 25 | 14378 40 156 26 | -------------------------------------------------------------------------------- /tests/expected_pos.txt: -------------------------------------------------------------------------------- 1 | 5320 193.1036 97 2 | 6224 148.174 96 3 | 7576 154.775 105 4 | 7577 148.862 96 5 | 7578 142.214 94 6 | 7581 147.028 97 7 | 7583 165.787 94 8 | 7584 167.585 92 9 | 7585 170.349 92 10 | 7586 180.899 93 11 | 7587 170.513 91 12 | 7588 174.678 90 13 | 7589 164.735 88 14 | 7590 178.822 89 15 | 7591 198.326 107 16 | 7592 203.901 100 17 | 7593 200.309 92 18 | 7594 178.491 90 19 | 7595 167.748 90 20 | 7596 183.018 94 21 | 7597 176.769 93 22 | 7598 186.823 91 23 | 7599 183.66 91 24 | 7600 179.276 91 25 | 7601 197.283 96 26 | 7602 191.097 94 27 | 7603 194.67 95 28 | 7604 177.325 91 29 | 7605 173.758 89 30 | 7606 200.816 98 31 | 7607 186.235 92 32 | 7608 185.421 91 33 | 7609 178.958 90 34 | 7610 171.75 88 35 | 7611 203.231 95 36 | 7612 181.354 92 37 | 7613 186.778 87 38 | 7614 175.582 88 39 | 7615 176.125 89 40 | 7616 181.514 90 41 | 7617 175.261 87 42 | 7618 164.719 86 43 | 7619 136.325 87 44 | 7621 170.736 106 45 | 7622 151.549 99 46 | 7623 149.412 97 47 | 7624 150.554 94 48 | 7626 149.541 97 49 | 10640 188.2908 88 50 | 14348 210 150 51 | 14368 250 152 52 | -------------------------------------------------------------------------------- /tests/expected_threshold_med.txt: -------------------------------------------------------------------------------- 1 | 7592 203.901 100 2 | 7593 200.309 92 3 | 7606 200.816 98 4 | 7611 203.231 95 5 | 14348 210 150 6 | 14368 250 152 7 | -------------------------------------------------------------------------------- /tests/expected_threshold_p95.txt: -------------------------------------------------------------------------------- 1 | 14368 250 152 2 | -------------------------------------------------------------------------------- /tests/expected_threshold_p99.txt: -------------------------------------------------------------------------------- 1 | 14368 250 152 2 | -------------------------------------------------------------------------------- /tests/scribe_data.txt: -------------------------------------------------------------------------------- 1 | 105.083333333333 2 | 90.9 3 | 763.9 4 | 83.3666666666667 5 | 78.3666666666667 6 | 80.5833333333333 7 | 76.3666666666667 8 | 210.983333333333 9 | 78 10 | 77.5166666666667 11 | 83.0166666666667 12 | 89.2333333333333 13 | 84.8666666666667 14 | 653.166666666667 15 | 70.9166666666667 16 | 72.8333333333333 17 | 75.9166666666667 18 | 73.5333333333333 19 | 548.866666666667 20 | 66.2333333333333 21 | 73.45 22 | 66.9666666666667 23 | 71.1166666666667 24 | 68.3166666666667 25 | 285.383333333333 26 | 317.2 27 | 63.2833333333333 28 | 64.0833333333333 29 | 60.5 30 | 550.883333333333 31 | 399.683333333333 32 | 75.9 33 | 115.35 34 | 78.9333333333333 35 | 88.6833333333333 36 | 475.533333333333 37 | 30.1166666666667 38 | 31.5166666666667 39 | 34.0833333333333 40 | 39.55 41 | 47.5166666666667 42 | 423.633333333333 43 | 52.55 44 | 50.2166666666667 45 | 61.4166666666667 46 | 56.6166666666667 47 | 64.4166666666667 48 | 742.3 49 | 165.85 50 | 122.883333333333 51 | 122.216666666667 52 | 114.666666666667 53 | 565.966666666667 54 | 134.7 55 | 141.166666666667 56 | 160.783333333333 57 | 168.483333333333 58 | 458.65 59 | 513.283333333333 60 | 154.366666666667 61 | 130.666666666667 62 | 125.933333333333 63 | 127.25 64 | 615.583333333333 65 | 122.9 66 | 97.45 67 | 122.766666666667 68 | 115.1 69 | 111.95 70 | 442.783333333333 71 | 113.833333333333 72 | 116.116666666667 73 | 128.7 74 | 135.033333333333 75 | 138.75 76 | 153.383333333333 77 | 143.583333333333 78 | 161.5 79 | 168.116666666667 80 | 152.25 81 | 147.116666666667 82 | 163.916666666667 83 | 161.1 84 | 146.95 85 | 132.65 86 | 127.283333333333 87 | 116.1 88 | 92.2833333333333 89 | 54.8833333333333 90 | 111.35 91 | 114.983333333333 92 | 110.983333333333 93 | 1015.35 94 | 774.583333333333 95 | 232.65 96 | 134.616666666667 97 | 130.25 98 | 98.6666666666667 99 | 102.4 100 | 184.866666666667 101 | 258.766666666667 102 | 70.3333333333333 103 | 81.3833333333333 104 | 81.1 105 | 89.2166666666667 106 | 536.966666666667 107 | 85.8333333333333 108 | 95.6333333333333 109 | 76.1 110 | 94.3833333333333 111 | 73.25 112 | 346.7 113 | 65.3833333333333 114 | 84.7333333333333 115 | 140.566666666667 116 | 120.6 117 | 121.383333333333 118 | 359.233333333333 119 | 55.2833333333333 120 | 54.55 121 | 52.1833333333333 122 | 56.2 123 | 112.116666666667 124 | 208.533333333333 125 | 49.4 126 | 49.0666666666667 127 | 56.0666666666667 128 | 54.0166666666667 129 | 63.5166666666667 130 | 344.416666666667 131 | 42.0666666666667 132 | 55.3666666666667 133 | 55.9666666666667 134 | 55.85 135 | 56.3 136 | 46.5666666666667 137 | 49.25 138 | 43.9 139 | 357.616666666667 140 | 44.1 141 | 44.6833333333333 142 | 43.1333333333333 143 | 40.55 144 | 452.2 145 | 47.0666666666667 146 | 40 147 | 42.35 148 | 48.3666666666667 149 | 44.8666666666667 150 | 48.5166666666667 151 | 244.016666666667 152 | 50.1666666666667 153 | 48.7333333333333 154 | 47.9166666666667 155 | 51.9666666666667 156 | 343.333333333333 157 | 35.25 158 | 45.3333333333333 159 | 46.8666666666667 160 | 48.7833333333333 161 | --------------------------------------------------------------------------------