├── LICENSE
├── README.md
├── __init__.py
├── py_fcm
├── __init__.py
├── fcm.py
├── fcm_estimator.py
├── learning
│ ├── __init__.py
│ ├── association.py
│ ├── discretization
│ │ ├── __init__.py
│ │ ├── clusters_estimation.py
│ │ ├── fuzzy_cmeans.py
│ │ └── rl_fuzzy_cmeans.py
│ └── utils.py
├── loader.py
└── utils
│ ├── __const.py
│ ├── __init__.py
│ └── functions.py
├── setup.py
└── tests
├── __init__.py
├── test_estimator.py
├── test_fcm.py
├── test_functions.py
├── test_generator.py
└── test_public_library_functions.py
/LICENSE:
--------------------------------------------------------------------------------
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 |
635 | Copyright (C)
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 | Copyright (C)
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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PyFCM
2 | Fuzzy cognitive maps python library. Also, supports the topology generation from data to solve classification problems.
3 | The details associated to the generation process are described in [this paper](https://link.springer.com/chapter/10.1007/978-3-030-89691-1_25).
4 | ### Installation
5 |
6 | #### From source:
7 |
8 | 1. Clone repository:
9 | ```
10 | $ git clone https://github.com/J41R0/PyFCM.git
11 | $ cd PyFCM
12 | ```
13 | 2. Install setup tools and package:
14 | ```
15 | $ pip install setuptools
16 | $ python setup.py install
17 | ```
18 | #### From PyPi:
19 | 1. Install package using pip:
20 | ```
21 | $ pip install py-fcm
22 | ```
23 |
24 | ### Example usage
25 |
26 | #### Inference:
27 | ```
28 | from py_fcm import from_json
29 |
30 | fcm_json = """{
31 | "max_iter": 500,
32 | "decision_function": "LAST",
33 | "activation_function": "sigmoid",
34 | "memory_influence": False,
35 | "stability_diff": 0.001,
36 | "stop_at_stabilize": True,
37 | "extra_steps": 5,
38 | "weight": 1,
39 | "concepts":
40 | [
41 | {
42 | "id": "concept_1",
43 | "is_active": True,
44 | "type": "SIMPLE",
45 | "activation": 0.5
46 | },
47 | {
48 | "id": "concept_2", "is_active": True,
49 | "type": "DECISION", "activation": 0.0,
50 | "custom_function": "gceq",
51 | "custom_function_args": {"weight": 0.3}
52 | },
53 | {
54 | "id": "concept_3",
55 | "is_active": True,
56 | "type": "SIMPLE",
57 | "activation": 0.0,
58 | "use_memory": True
59 | },
60 | {
61 | "id": "concept_4",
62 | "is_active": True,
63 | "type": "SIMPLE",
64 | "activation": 0.3,
65 | "custom_function": "saturation"
66 | }
67 | ],
68 | "relations":
69 | [
70 | {"origin": "concept_4", "destiny": "concept_2", "weight": -0.1},
71 | {"origin": "concept_1", "destiny": "concept_3", "weight": 0.59},
72 | {"origin": "concept_3", "destiny": "concept_2", "weight": 0.8911}
73 | ],
74 | 'activation_function_args': {'lambda_val': 1},
75 | """
76 | my_fcm = from_json(fcm_json)
77 | my_fcm.run_inference()
78 | result = my_fcm.get_final_state(concept_type='any')
79 | print(result)
80 | ```
81 |
82 | #### Generation:
83 | ```
84 | import pandas
85 | from py_fcm import FcmEstimator
86 |
87 | data_dict = {
88 | 'F1': ['x', 'x', 'y', 'y'],
89 | 'F2': [9.8, 7.3, 1.1, 3.6],
90 | 'class': ['a', 'a', 'r', 'r']
91 | }
92 |
93 | train = pandas.DataFrame(data_dict)
94 | x_train = train.loc[:, train.columns != 'class']
95 | y_train = train.loc[:, 'class']
96 |
97 | estimator = FcmEstimator()
98 | estimator.fit(x_train, y_train)
99 | print(estimator.predict(x_train))
100 | print("Accuracy: ",estimator.score(x_train, y_train))
101 | print(estimator.get_fcm().to_json())
102 |
103 | ```
--------------------------------------------------------------------------------
/__init__.py:
--------------------------------------------------------------------------------
1 | __version__ = '0.3.0'
2 |
--------------------------------------------------------------------------------
/py_fcm/__init__.py:
--------------------------------------------------------------------------------
1 | from py_fcm.utils.functions import Relation
2 | from py_fcm.fcm_estimator import FcmEstimator
3 | from py_fcm.loader import from_json, join_maps, FuzzyCognitiveMap
4 | from py_fcm.utils.__const import TYPE_SIMPLE, TYPE_DECISION, TYPE_FUZZY
5 |
6 |
7 | def load_csv_dataset(dataset_dir, ds_name, factorize=False):
8 | import pandas
9 | from collections import OrderedDict
10 | data_dict = OrderedDict()
11 | with open(dataset_dir + "/" + ds_name) as file:
12 | content = []
13 | for line in file:
14 | content.append(line.strip())
15 | # first line describe the atributes names
16 | attributes = str(content[0]).split(',')
17 | for current_att in attributes:
18 | data_dict[current_att] = []
19 |
20 | # print(data_dict)
21 | for line in range(1, len(content)):
22 | data_line = str(content[line]).split(',')
23 | # avoid rows with different attributes length
24 | if len(data_line) == len(attributes):
25 | # the missing data must be identified
26 | for data in range(0, len(data_line)):
27 | # reusing for value type inference
28 | current_att = data_line[data]
29 | data_dict[attributes[data]].append(current_att)
30 | else:
31 | # Handle errors in dataset matrix
32 | print("Errors in line: ", line, len(data_line), len(attributes))
33 | # adding data set
34 | print("\n===> Dataset for test: ", ds_name)
35 | try:
36 | dataset_frame = pandas.DataFrame(data_dict).drop_duplicates()
37 | except Exception as err:
38 | for key, value in data_dict.items():
39 | print(key, value)
40 | pass
41 | raise Exception(ds_name + " " + str(err))
42 | # Transform the columns disc values in 0..N values
43 | if factorize:
44 | # for current_col in dataset_frame.
45 | dataset_frame = dataset_frame.apply(lambda x: pandas.factorize(x)[0])
46 | return dataset_frame
47 |
--------------------------------------------------------------------------------
/py_fcm/fcm_estimator.py:
--------------------------------------------------------------------------------
1 | from collections import defaultdict
2 |
3 | import numpy as np
4 | from pandas import DataFrame, Series
5 |
6 | from py_fcm.utils.functions import Relation
7 | from py_fcm.learning.association import AssociationBasedFCM, FuzzyCognitiveMap
8 |
9 |
10 | class FcmEstimator:
11 | def __init__(self, concept_str_separator="___", concept_exclusion_val=-1, fit_inclination=True,
12 | double_relation=True, features_relation=True, infer_concept_type=True, discretization="cmeans-cfe",
13 | causal_eval_function=Relation.supp, causal_threshold=0.0, causality_function=Relation.pos_inf,
14 | causal_sign_function=None, causal_sign_threshold=0, fcm_mem_influence=True, fcm_max_it=250,
15 | fcm_extra_steps=5, fcm_stability_diff=0.001, fcm_decision_function="MEAN",
16 | fcm_excitation_function='KOSKO', fcm_activation_function="sigmoid_hip", vectorized_run=True, **kwargs):
17 | self.__is_fcm_generated = False
18 | # init FCM
19 | self.__fcm = FuzzyCognitiveMap(max_it=fcm_max_it,
20 | extra_steps=fcm_extra_steps,
21 | stability_diff=fcm_stability_diff,
22 | decision_function=fcm_decision_function,
23 | mem_influence=fcm_mem_influence,
24 | activation_function=fcm_activation_function,
25 | **kwargs)
26 | self.__fcm.set_default_concept_properties(excitation_function=fcm_excitation_function)
27 | self.__fcm.debug = not vectorized_run
28 | # init generator
29 | self.__generator = AssociationBasedFCM(str_separator=concept_str_separator,
30 | discretization=discretization,
31 | fit_inclination=fit_inclination,
32 | double_relation=double_relation,
33 | features_relation=features_relation,
34 | exclusion_val=concept_exclusion_val,
35 | causality_function=causality_function,
36 | causal_eval_function=causal_eval_function,
37 | causal_threshold=causal_threshold,
38 | sign_function=causal_sign_function,
39 | sign_threshold=causal_sign_threshold)
40 | self.__generator.infer_concept_type = infer_concept_type
41 |
42 | def __to_data_frame(self, x, start=0):
43 | if type(x) == dict:
44 | return DataFrame(x)
45 | if type(x) == list:
46 | x = np.array(x)
47 | if type(x) == np.ndarray:
48 | col_names = []
49 | if len(x.shape) == 1:
50 | col_names.append(str(start))
51 | else:
52 | col_names = [str(i + start) for i in range(x.shape[1])]
53 | return DataFrame(x, columns=col_names)
54 | if type(x) == Series:
55 | return x.to_frame()
56 | return x
57 |
58 | def __validate_dual_input(self, x, y):
59 | if type(x) == list:
60 | x = np.array(x)
61 | max_col = x.shape[1]
62 | x = self.__to_data_frame(x)
63 | y = self.__to_data_frame(y, start=max_col)
64 | if type(x) != DataFrame:
65 | raise Exception("Cannot load provided x structure, please use a pandas DataFrame like object.")
66 | if type(y) != DataFrame:
67 | raise Exception("Cannot load provided y structure, please use a pandas DataFrame like object.")
68 | return x, y
69 |
70 | def fit(self, x, y, plot=False, plot_dir='.'):
71 | # expect a dataframe or a matrix shaped in a list of rows
72 | x, y = self.__validate_dual_input(x, y)
73 | target_feat = []
74 | for col in y.columns:
75 | target_feat.append(col)
76 | input_data = x.join(y, rsuffix='_class')
77 | self.__generator.reset()
78 | self.__generator.build_fcm(input_data, target_features=target_feat, fcm=self.__fcm,
79 | plot=plot, plot_dir=plot_dir)
80 | self.__is_fcm_generated = True
81 |
82 | def predict(self, x: DataFrame, plot=False, plot_dir='.'):
83 | if type(x) != DataFrame:
84 | x = self.__to_data_frame(x)
85 | if type(x) != DataFrame:
86 | raise Exception("Cannot load provided x structure, please use a pandas DataFrame like object.")
87 | result = defaultdict(list)
88 | if self.__is_fcm_generated:
89 | for index, row in x.iterrows():
90 | self.__fcm.clear_execution()
91 | for feat_name in x.columns:
92 | self.__generator.init_concept_by_feature_data(feat_name, row[feat_name])
93 |
94 | prediction = self.__generator.get_inference_result(plot=plot, plot_dir=plot_dir)
95 | for feat_name in prediction:
96 | result[feat_name].append(prediction[feat_name])
97 | else:
98 | raise Exception("There is no FCM generated, the fit method mus be called first")
99 | return DataFrame(result)
100 |
101 | def score(self, x, y):
102 | # TODO: Add other scoring functions besides accuracy
103 | if self.__is_fcm_generated:
104 | x, y = self.__validate_dual_input(x, y)
105 | predicted_result = self.predict(x)
106 | right_predicted = 0
107 | total = 0
108 | for feat_name in y.columns:
109 | pos = 0
110 | for val in y[feat_name]:
111 | total += 1
112 | if val == predicted_result[feat_name][pos]:
113 | right_predicted += 1
114 | pos += 1
115 | return right_predicted / total
116 | else:
117 | raise Exception("There is no FCM generated, the fit method mus be called first")
118 |
119 | def get_fcm(self):
120 | return self.__fcm
121 |
--------------------------------------------------------------------------------
/py_fcm/learning/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/J41R0/PyFCM/01c4630acc9784d94a27232b2b9a0cdac7ba6f4c/py_fcm/learning/__init__.py
--------------------------------------------------------------------------------
/py_fcm/learning/association.py:
--------------------------------------------------------------------------------
1 | import warnings
2 | from collections import defaultdict
3 |
4 | from pandas import DataFrame
5 |
6 | from py_fcm.utils.__const import *
7 | from py_fcm.learning.utils import *
8 | from py_fcm.fcm import FuzzyCognitiveMap
9 | from py_fcm.utils.functions import Relation, fuzzy_set
10 | from py_fcm.learning.discretization import fuzzy_feature_discretization
11 |
12 | # USED CONST DEFINITION
13 | # features
14 | NP_ARRAY_DATA = "np array data"
15 | CONCEPT_DESC = "feature concepts description"
16 | CONCEPT_NAMES = "feature concept names"
17 | TYPE = "concept type"
18 | FEATURE_CONCEPTS = 0
19 | FEATURE_DESC = 1
20 |
21 | # np unique output consts
22 | UNIQUE_ARRAY = 0
23 |
24 | # coefficient calc output
25 | P_Q_COEFF = 0
26 | Q_P_COEFF = 1
27 |
28 |
29 | class AssociationBasedFCM:
30 | def __init__(self, str_separator="___", discretization="cmeans-gap", exclusion_val=-1, fit_inclination=True,
31 | double_relation=True, features_relation=True, causality_function=Relation.conf,
32 | causal_eval_function=Relation.supp, causal_threshold=0.0, sign_function=None,
33 | sign_threshold=0):
34 | self.__features = {}
35 | self.__processed_features = set()
36 |
37 | # vars
38 | self.__fcm = FuzzyCognitiveMap()
39 | self.__exclusion_val = exclusion_val
40 | self.__causality_value_function = causality_function
41 | self.__causality_evaluation_function = causal_eval_function
42 | self.__causal_threshold = causal_threshold
43 | self.__sign_function = sign_function
44 | self.__sign_function_cut_val = sign_threshold
45 | self.__str_separator = str_separator
46 | self.__double_relation = double_relation
47 | self.__features_relation = features_relation
48 | self.__discretization_method = discretization
49 |
50 | if fit_inclination:
51 | self.__fcm.fit_inclination = 0.75
52 | self.infer_concept_type = True
53 |
54 | def __name_concept(self, feat_name, value):
55 | return str(value) + str(self.__str_separator) + str(feat_name)
56 |
57 | def __gen_continuous_concepts(self, feat_name, target_feats, plot, plot_dir):
58 | if feat_name in target_feats:
59 | self.__features[feat_name][TYPE] = TYPE_REGRESOR
60 | else:
61 | self.__features[feat_name][TYPE] = TYPE_FUZZY
62 |
63 | n_clusters, val_list, memberships = fuzzy_feature_discretization(self.__features[feat_name][NP_ARRAY_DATA],
64 | strategy=self.__discretization_method,
65 | att_name=feat_name, plot=plot,
66 | plot_dir=plot_dir)
67 | names = []
68 | final_membership = np.zeros((n_clusters, self.__features[feat_name][NP_ARRAY_DATA].size))
69 | for curr_cluster in range(n_clusters):
70 | concept_name = self.__name_concept(feat_name, curr_cluster)
71 | names.append(concept_name)
72 | fun_args = {'membership': memberships[curr_cluster],
73 | 'val_list': val_list}
74 | self.__fcm.add_concept(concept_name, self.__features[feat_name][TYPE], is_active=True,
75 | activation_dict=fun_args)
76 | for val_pos in range(self.__features[feat_name][NP_ARRAY_DATA].size):
77 | curr_value = self.__features[feat_name][NP_ARRAY_DATA][val_pos]
78 | final_membership[curr_cluster][val_pos] = fuzzy_set(value=curr_value,
79 | membership=fun_args['membership'],
80 | val_list=fun_args['val_list'])
81 |
82 | self.__features[feat_name][CONCEPT_NAMES] = names
83 | self.__features[feat_name][CONCEPT_DESC] = ([i for i in range(n_clusters)], final_membership)
84 |
85 | def __gen_discrete_concepts(self, feat_name, target_feats, uniques_data=None):
86 | if feat_name in target_feats:
87 | self.__features[feat_name][TYPE] = TYPE_DECISION
88 | else:
89 | self.__features[feat_name][TYPE] = TYPE_SIMPLE
90 |
91 | if uniques_data is None:
92 | uniques_data = np.unique(self.__features[feat_name][NP_ARRAY_DATA], return_counts=True)
93 |
94 | feat_matrix = gen_discrete_feature_matrix(self.__features[feat_name][NP_ARRAY_DATA], uniques_data[UNIQUE_ARRAY])
95 |
96 | names = []
97 | for val_pos in range(uniques_data[UNIQUE_ARRAY].size):
98 | name = self.__name_concept(feat_name, uniques_data[UNIQUE_ARRAY][val_pos])
99 | names.append(name)
100 | self.__fcm.add_concept(name, self.__features[feat_name][TYPE], is_active=True)
101 | self.__features[feat_name][CONCEPT_NAMES] = names
102 | self.__features[feat_name][CONCEPT_DESC] = (uniques_data[UNIQUE_ARRAY], feat_matrix)
103 |
104 | def __def_inner_feat_relations(self, feat_name):
105 | # FIXME: This function only works for fuzzy and discrete features, use the function type instead.
106 | if self.__exclusion_val != 0:
107 | related_concepts = self.__features[feat_name][CONCEPT_DESC][FEATURE_CONCEPTS]
108 | for concept1_pos in range(len(related_concepts) - 1):
109 | for concept2_pos in range(concept1_pos + 1, len(related_concepts)):
110 | self.__fcm.add_relation(self.__features[feat_name][CONCEPT_NAMES][concept1_pos],
111 | self.__features[feat_name][CONCEPT_NAMES][concept2_pos],
112 | self.__exclusion_val)
113 | self.__fcm.add_relation(self.__features[feat_name][CONCEPT_NAMES][concept2_pos],
114 | self.__features[feat_name][CONCEPT_NAMES][concept1_pos],
115 | self.__exclusion_val)
116 |
117 | def __def_relation_weight(self, feat_name_a, concept_a_pos, feat_name_b, concept_b_pos, sign, p_q, p_nq, np_q,
118 | np_nq):
119 | causality_p_q = self.__causality_evaluation_function(p_q, p_nq, np_q, np_nq)
120 | if abs(causality_p_q) > self.__causal_threshold:
121 | relation_weight = sign * self.__causality_value_function(p_q, p_nq, np_q, np_nq)
122 | if relation_weight != 0:
123 | self.__fcm.add_relation(self.__features[feat_name_a][CONCEPT_NAMES][concept_a_pos],
124 | self.__features[feat_name_b][CONCEPT_NAMES][concept_b_pos],
125 | relation_weight)
126 |
127 | def __def_two_feat_relation(self, feat_name_a, feat_name_b):
128 | for concept_p_pos in range(len(self.__features[feat_name_a][CONCEPT_NAMES])):
129 | for concept_q_pos in range(len(self.__features[feat_name_b][CONCEPT_NAMES])):
130 | relation_coefficients = calc_concepts_coefficient(
131 | self.__features[feat_name_a][CONCEPT_DESC][FEATURE_DESC][concept_p_pos],
132 | self.__features[feat_name_b][CONCEPT_DESC][FEATURE_DESC][concept_q_pos]
133 | )
134 | if relation_coefficients is None:
135 | raise Exception("Invalid relation input data")
136 | # define relation sign
137 | sign_p_q = 1
138 | sign_q_p = 1
139 | if self.__sign_function is not None:
140 | if self.__sign_function_cut_val > self.__sign_function(*relation_coefficients[P_Q_COEFF]):
141 | sign_p_q = -1
142 | if self.__sign_function_cut_val > self.__sign_function(*relation_coefficients[Q_P_COEFF]):
143 | sign_q_p = -1
144 | if self.__double_relation:
145 | # define causality degree p -> q
146 | self.__def_relation_weight(feat_name_a, concept_p_pos, feat_name_b, concept_q_pos, sign_p_q,
147 | *relation_coefficients[P_Q_COEFF])
148 | # define causality degree q -> p
149 | self.__def_relation_weight(feat_name_b, concept_q_pos, feat_name_a, concept_p_pos, sign_q_p,
150 | *relation_coefficients[Q_P_COEFF])
151 | else:
152 | if self.__are_same_feature_group(feat_name_a, feat_name_b):
153 | p_q = abs(self.__causality_value_function(*relation_coefficients[P_Q_COEFF]))
154 | q_p = abs(self.__causality_value_function(*relation_coefficients[Q_P_COEFF]))
155 | if p_q > q_p:
156 | # define causality degree p -> q
157 | self.__def_relation_weight(feat_name_a, concept_p_pos, feat_name_b, concept_q_pos,
158 | sign_p_q, *relation_coefficients[P_Q_COEFF])
159 | if q_p > p_q:
160 | # define causality degree q -> p
161 | self.__def_relation_weight(feat_name_b, concept_q_pos, feat_name_a, concept_p_pos,
162 | sign_q_p, *relation_coefficients[Q_P_COEFF])
163 | else:
164 | if not self.__is_target_concept(feat_name_a):
165 | self.__def_relation_weight(feat_name_a, concept_p_pos, feat_name_b, concept_q_pos,
166 | sign_p_q, *relation_coefficients[P_Q_COEFF])
167 | else:
168 | self.__def_relation_weight(feat_name_b, concept_q_pos, feat_name_a, concept_p_pos,
169 | sign_q_p, *relation_coefficients[Q_P_COEFF])
170 |
171 | def __def_all_feat_relations(self, feat_name):
172 | for other_feat in self.__processed_features:
173 | if other_feat != feat_name:
174 | self.__def_two_feat_relation(feat_name, other_feat)
175 |
176 | self.__processed_features.add(feat_name)
177 |
178 | def __is_target_concept(self, feat_name):
179 | if self.__features[feat_name][TYPE] == TYPE_REGRESOR or self.__features[feat_name][TYPE] == TYPE_DECISION:
180 | return True
181 | return False
182 |
183 | def __are_same_feature_group(self, name_feat1, name_feat2):
184 | return ((self.__is_target_concept(name_feat1) and self.__is_target_concept(name_feat2)) or
185 | not self.__is_target_concept(name_feat1) and not self.__is_target_concept(name_feat2))
186 |
187 | def __def_feat_target_relation(self, feat_name):
188 | for other_feat in self.__processed_features:
189 | if other_feat != feat_name and not self.__are_same_feature_group(feat_name, other_feat):
190 | self.__def_two_feat_relation(feat_name, other_feat)
191 | self.__processed_features.add(feat_name)
192 |
193 | def build_fcm(self, dataset: DataFrame, target_features=None, fcm=None, plot=False,
194 | plot_dir='.') -> FuzzyCognitiveMap:
195 | # TODO: handle features multivalued and with missing values
196 | if fcm is not None and type(fcm) == FuzzyCognitiveMap:
197 | self.__fcm = fcm
198 | else:
199 | self.__fcm = FuzzyCognitiveMap()
200 | if target_features is None:
201 | target_features = set()
202 | # define map concepts
203 | for feat_name in dataset.loc[:, ]:
204 | self.__features[feat_name] = {NP_ARRAY_DATA: np.array(dataset.loc[:, feat_name].values)}
205 | # discrete features
206 | # TODO: extend to categorical series
207 | if dataset[feat_name].dtype == np.object or dataset[feat_name].dtype == np.bool:
208 | self.__gen_discrete_concepts(feat_name, target_features)
209 |
210 | # possible continuous feature
211 | elif self.infer_concept_type:
212 | uniques_data = np.unique(self.__features[feat_name][NP_ARRAY_DATA], return_counts=True)
213 | if uniques_data[UNIQUE_ARRAY].size < 10:
214 | self.__gen_discrete_concepts(feat_name, target_features, uniques_data)
215 | else:
216 | # TODO: review type inference
217 | if (uniques_data[UNIQUE_ARRAY].size / self.__features[feat_name][NP_ARRAY_DATA].size) > 0.2:
218 | warnings.warn("Possible discrete feature behavior for " + feat_name + " feature.")
219 | self.__features[feat_name][NP_ARRAY_DATA] = self.__features[feat_name][
220 | NP_ARRAY_DATA].astype(
221 | np.float64)
222 | self.__gen_continuous_concepts(feat_name, target_features, plot, plot_dir)
223 | else:
224 | self.__gen_continuous_concepts(feat_name, target_features, plot, plot_dir)
225 |
226 | # TODO: identify feature type to define the inner concept's relation properly
227 | self.__def_inner_feat_relations(feat_name)
228 | self.__processed_features.add(feat_name)
229 |
230 | if self.__features_relation:
231 | self.__def_all_feat_relations(feat_name)
232 | else:
233 | self.__def_feat_target_relation(feat_name)
234 |
235 | return self.__fcm
236 |
237 | def init_concept_by_feature_data(self, feat_name, value):
238 | try:
239 | if self.__features[feat_name][TYPE] == TYPE_FUZZY or self.__features[feat_name][TYPE] == TYPE_REGRESOR:
240 | for concept in self.__features[feat_name][CONCEPT_NAMES]:
241 | self.__fcm.init_concept(concept, value, required_presence=False)
242 | else:
243 | if type(value) != str:
244 | value = int(value)
245 | self.__fcm.init_concept(self.__name_concept(feat_name, value), 1.0, required_presence=False)
246 | except Exception:
247 | raise Exception("Cannot init concept")
248 |
249 | def __get_feature_and_info(self, concept: str):
250 | res = concept.split(self.__str_separator)
251 | return res[1], res[0]
252 |
253 | def __get_discrete_feature_result(self, fcm_results):
254 | final_result = {}
255 | for feat_name in fcm_results:
256 | max_actv = -1
257 | res_pos = 0
258 | curr_pos = 0
259 | for value, output in fcm_results[feat_name]:
260 | if output > max_actv:
261 | max_actv = output
262 | res_pos = curr_pos
263 | curr_pos += 1
264 | # return result with highest activation
265 | result = fcm_results[feat_name][res_pos][0]
266 | if result.isnumeric():
267 | result = int(result)
268 | final_result[feat_name] = result
269 | return final_result
270 |
271 | def get_inference_result(self, plot=False, map_name="fcm", plot_dir='.'):
272 | self.__fcm.run_inference()
273 | fcm_result = self.__fcm.get_final_state()
274 | cont_res_feat = defaultdict(list)
275 | disc_res_feat = defaultdict(list)
276 | for concept in fcm_result:
277 | feat_name, info = self.__get_feature_and_info(concept)
278 | if self.__features[feat_name][TYPE] == TYPE_FUZZY or self.__features[feat_name][TYPE] == TYPE_REGRESOR:
279 | cont_res_feat[feat_name].append((info, fcm_result[concept]))
280 | else:
281 | disc_res_feat[feat_name].append((info, fcm_result[concept]))
282 | if plot:
283 | self.__fcm.plot_execution(fig_name=map_name, plot_dir=plot_dir)
284 | # TODO: handle continuous features output for regression problems
285 | return self.__get_discrete_feature_result(disc_res_feat)
286 |
287 | def reset(self):
288 | self.__features = {}
289 | self.__processed_features = set()
290 | self.__fcm.clear_all()
291 |
--------------------------------------------------------------------------------
/py_fcm/learning/discretization/__init__.py:
--------------------------------------------------------------------------------
1 | import warnings
2 |
3 | import numpy as np
4 |
5 | from py_fcm.learning.discretization.clusters_estimation import estimate_clusters
6 | from py_fcm.learning.discretization.rl_fuzzy_cmeans import rl_fuzzy_cmeans
7 | from py_fcm.learning.discretization.fuzzy_cmeans import fuzzy_cmeans
8 |
9 |
10 | def __create_three_clusters(val_list):
11 | if len(val_list) % 2 == 0:
12 | mid = int(len(val_list) / 2) - 1
13 | new_value = val_list[mid] + abs(val_list[mid] - val_list[mid + 1]) / 2
14 | new_list = np.concatenate((val_list[:mid + 1], [new_value], val_list[mid + 1:]))
15 | val_list = new_list.copy()
16 | else:
17 | new_list = val_list.copy()
18 |
19 | if new_list[0] <= 0:
20 | abs_min = 1 + abs(new_list[0])
21 | new_list = new_list + abs_min
22 | clusters_desc = np.zeros((len(new_list), 3))
23 | mid = int(len(new_list) / 2) + 1
24 | max = len(clusters_desc) - 1
25 | for pos in range(mid):
26 | clusters_desc[pos][2] = 0.0
27 | clusters_desc[pos][1] = pos / mid # new_list[pos] / mid_val
28 | clusters_desc[pos][0] = 1.0 - clusters_desc[pos][1]
29 |
30 | clusters_desc[max - pos][0] = 0.0
31 | clusters_desc[max - pos][2] = clusters_desc[pos][0]
32 | clusters_desc[max - pos][1] = clusters_desc[pos][1]
33 | # start
34 | clusters_desc[0][0] = 1.0
35 | clusters_desc[0][1] = 0.0
36 | clusters_desc[0][2] = 0.0
37 | # mid
38 | clusters_desc[mid][0] = 0.0
39 | clusters_desc[mid][1] = 1.0
40 | clusters_desc[mid][2] = 0.0
41 | # end
42 | clusters_desc[-1][0] = 0.0
43 | clusters_desc[-1][1] = 0.0
44 | clusters_desc[-1][2] = 1.0
45 | clusters_desc = clusters_desc.T
46 | return val_list, clusters_desc
47 |
48 |
49 | def fuzzy_feature_discretization(val_list, max_clusters=7, max_iter=200, seed=None,
50 | strategy="cmeans-gap", plot=False, att_name=None, plot_dir="."):
51 | """
52 | Estimate fuzzy clusters that define a continuous feature. Propose the amount of clusters to be used and return the
53 | membership degree of each provided point using fuzzy cmeans algorithm as kernel
54 |
55 | Args:
56 | val_list:
57 | max_clusters:
58 | gen_init_state:
59 | max_iter:
60 | seed:
61 | strategy: 'cmeans-gap' or 'cmeans-sil' for fuzzy cmeans or 'rl-cmeans' for robust learning fuzzy cmeans.
62 | force_clusters:
63 | plot:
64 | att_name:
65 | plot_dir:
66 |
67 | Returns: Membership dict of each provided value to each cluster. e.g. {'1.3':[0.01,0.57,0.23]}
68 |
69 | """
70 | # TODO: add a possiblistic fuzzy cmeans approach
71 | val_list = np.unique(val_list)
72 | if len(val_list) < 10:
73 | raise Exception("Too few (" + str(len(val_list)) + ") variations for fuzzy clustering on feature: " + att_name)
74 | val_list.sort()
75 | input_values = np.vstack(val_list)
76 | kwargs = {
77 | 'maxiter': max_iter,
78 | 'seed': seed
79 | }
80 | if strategy == 'cmeans-gap':
81 | num_clusters, clusters_desc = estimate_clusters(input_values, max_clusters, method='gap_concentration',
82 | **kwargs)
83 | elif strategy == 'cmeans-sil':
84 | num_clusters, clusters_desc = estimate_clusters(input_values, max_clusters, method='fuzzy_silhouette',
85 | **kwargs)
86 | elif strategy == 'cmeans-cfe':
87 | num_clusters, clusters_desc = estimate_clusters(input_values, max_clusters, method='combined_fuzzy_entropy',
88 | **kwargs)
89 | elif strategy == 'rl-cmeans':
90 | centroids, clusters_desc, alpha, t = rl_fuzzy_cmeans(input_values, max_iter=max_iter)
91 | clusters_desc = clusters_desc.T
92 | num_clusters = len(alpha)
93 | if num_clusters > max_clusters:
94 | warnings.warn("Unexpected number of clusters found for " + att_name + ": " + str(num_clusters))
95 | else:
96 | raise Exception("Unsupported clustering method: " + strategy)
97 | # forcing clusters
98 | if num_clusters < 2:
99 | num_clusters = 3
100 | val_list, clusters_desc = __create_three_clusters(val_list)
101 |
102 | if plot:
103 | import matplotlib.pyplot as plt
104 | colors = ['b', 'orange', 'g', 'r', 'c', 'm', 'y', 'k', 'Brown', 'ForestGreen']
105 |
106 | # plotting membership degree of values over found clusters
107 | for current in range(0, num_clusters):
108 | # plt.plot(val_list, clusters_desc[:, current], colors[current])
109 | plt.plot(range(0, len(val_list)), clusters_desc[current], colors[current])
110 | # show plotted values
111 | if att_name is not None:
112 | plt.savefig(plot_dir + '/' + att_name + '.png')
113 | else:
114 | plt.savefig(plot_dir + '/cluster_test.png')
115 | plt.close()
116 |
117 | return num_clusters, val_list, clusters_desc
118 |
--------------------------------------------------------------------------------
/py_fcm/learning/discretization/clusters_estimation.py:
--------------------------------------------------------------------------------
1 | import warnings
2 |
3 | import numpy as np
4 | from numba import njit
5 |
6 | from py_fcm.learning.utils import one_dimension_distance
7 | from py_fcm.learning.discretization.fuzzy_cmeans import fuzzy_cmeans
8 |
9 |
10 | @njit
11 | def __estimate_change_points(values: np.ndarray, max_clusters: int):
12 | """
13 | Curve fit approach to find the amount of clusters to be used. Execute a curve fit process to input data and try to
14 | find the change points in input data, the amount of change points -1 is the number of clusters to be found and the
15 | change points can be used as algorithm initial state.
16 | Args:
17 | values:
18 | max_clusters:
19 |
20 | Returns:
21 |
22 | """
23 | n_samples = values.size
24 |
25 | min_interval = float(1 / max_clusters)
26 | mean = 0.0
27 |
28 | x_change_points = []
29 | y_change_points = []
30 | flag_grow = False
31 | for value in range(n_samples - 1):
32 | if abs(values[value] - values[value + 1]) > abs(mean) and not flag_grow:
33 | flag_grow = True
34 | x_change_points.append(value)
35 | y_change_points.append(values[value])
36 |
37 | if abs(values[value] - values[value + 1]) < abs(mean) and flag_grow:
38 | flag_grow = False
39 | x_change_points.append(value)
40 | y_change_points.append(values[value])
41 |
42 | if len(x_change_points) > 1 and value - x_change_points[-2] < n_samples * min_interval:
43 | del x_change_points[-1]
44 | del y_change_points[-1]
45 |
46 | mean = (abs(values[value] - values[value + 1]) + mean) / 2
47 |
48 | if len(x_change_points) > 1 and n_samples - 1 - x_change_points[-1] < n_samples * min_interval:
49 | del x_change_points[-1]
50 | del y_change_points[-1]
51 | x_change_points.append(n_samples - 1)
52 | y_change_points.append(values[-1])
53 | elif len(x_change_points) > 1:
54 | x_change_points.append(n_samples - 1)
55 | y_change_points.append(values[-1])
56 |
57 | return x_change_points, y_change_points
58 |
59 |
60 | def gap_concentration(data: np.array, max_clusters: int):
61 | n_samples = data.size
62 | poly = np.polyfit(list(range(n_samples)), data[:, 0], max_clusters)
63 | func = np.poly1d(poly)
64 | image = func(np.linspace(0, data.size - 1, n_samples))
65 | change_points = __estimate_change_points(image, max_clusters)
66 | return image, change_points
67 |
68 |
69 | def fuzzy_silhouette(data: np.array, max_clusters: int, alpha=2, function=fuzzy_cmeans, **kwargs):
70 | from sklearn.metrics import pairwise_distances_chunked
71 | if function is not None:
72 | dist_matrix = next(pairwise_distances_chunked(data))
73 | clusters = 0
74 | final_fuzzy_silhouette = 0
75 | final_desc = data
76 | for n_clusters in range(2, max_clusters + 1):
77 | clusters_desc, centroids = function(data, n_clusters, **kwargs)
78 | silhouette = np.zeros(len(data))
79 | weight = np.zeros(len(data))
80 | for i in range(len(data)):
81 | curr_membership = clusters_desc[:, i]
82 | if curr_membership[0] > curr_membership[1]:
83 | max_val = curr_membership[0]
84 | max_pos = 0
85 | sec_max = curr_membership[1]
86 | sec_max_pos = 1
87 | else:
88 | max_val = curr_membership[1]
89 | max_pos = 1
90 | sec_max = curr_membership[0]
91 | sec_max_pos = 0
92 | for j in range(2, n_clusters):
93 | if curr_membership[j] > max_val:
94 | sec_max = max_val
95 | sec_max_pos = max_pos
96 | max_val = curr_membership[j]
97 | max_pos = j
98 | elif sec_max < curr_membership[j] != max_val:
99 | sec_max = curr_membership[j]
100 | sec_max_pos = j
101 | weight[i] = (max_val - sec_max) ** alpha
102 | a = np.sum(np.multiply(dist_matrix[i], clusters_desc[max_pos])) / (len(data) - 1)
103 | b = np.sum(np.multiply(dist_matrix[i], clusters_desc[sec_max_pos])) / (len(data) - 1)
104 | silhouette[i] = (b - a) / max(b, a)
105 | fuzzy_silhouette = np.sum(np.multiply(weight, silhouette)) / np.sum(weight)
106 | if fuzzy_silhouette > final_fuzzy_silhouette:
107 | final_fuzzy_silhouette = fuzzy_silhouette
108 | clusters = n_clusters
109 | final_desc = clusters_desc
110 | return clusters, final_desc, final_fuzzy_silhouette
111 |
112 |
113 | def __fuzzy_cross_entropy(u_i, u_j):
114 | return np.sum(
115 | u_i * np.log2(u_i / (u_i / 2 + u_j / 2)) +
116 | (1 - u_i) * np.log2((1 - u_i) / (1 - (u_i + u_j) / 2))
117 | )
118 |
119 |
120 | def cfe(data: np.array, max_clusters: int, function=fuzzy_cmeans, **kwargs):
121 | if function is not None:
122 | n = len(data)
123 | clusters = 0
124 | final_cfe = 0
125 | final_desc = data
126 | absolute_center = np.sum(data) / len(data)
127 | for n_clusters in range(2, max_clusters + 1):
128 | clusters_desc, centroids = function(data, n_clusters, **kwargs)
129 | sum_hui = wsgd = bgds = sfce = 0.0
130 | for cluster_pos in range(n_clusters):
131 | curr_cluster = clusters_desc[cluster_pos]
132 | a_i = np.sum(curr_cluster)
133 | sum_val = np.sum(np.multiply(curr_cluster, np.log2(curr_cluster)) +
134 | np.multiply(1 - curr_cluster, np.log2(1 - curr_cluster)))
135 | h_u_i = -sum_val / (n * np.log2(2))
136 | sum_hui += h_u_i
137 |
138 | wsgd += np.sum(curr_cluster * (np.linalg.norm(data - centroids[cluster_pos]) ** 2))
139 | bgds += a_i * np.linalg.norm(centroids[cluster_pos] - absolute_center) ** 2
140 | for j in range(n_clusters):
141 | if cluster_pos != j:
142 | sfce += (__fuzzy_cross_entropy(clusters_desc[cluster_pos], clusters_desc[j]) +
143 | __fuzzy_cross_entropy(clusters_desc[j], clusters_desc[cluster_pos]))
144 | # final calculations
145 | sfce = (2 / (n_clusters * (n_clusters - 1))) * sfce
146 | fe = sum_hui / n_clusters
147 | ch = (bgds / (n_clusters - 1)) * ((n - n_clusters) / wsgd)
148 | mc = sfce / fe
149 | cfe = (mc + ch) / 2
150 | if cfe > final_cfe:
151 | final_cfe = cfe
152 | clusters = n_clusters
153 | final_desc = clusters_desc
154 | return clusters, final_desc, final_cfe
155 |
156 |
157 | def estimate_clusters(data: np.array, max_clusters: int, method='gap_concentration', gen_init_state=True,
158 | function=fuzzy_cmeans, **kwargs):
159 | available_methods = {'gap_concentration', 'fuzzy_silhouette', 'combined_fuzzy_entropy'}
160 | clusters_desc = None
161 | clusters = 1
162 | init_state = None
163 | kwargs['m'] = 2.5
164 | kwargs['error'] = 0.0001
165 | if max_clusters >= data.size:
166 | max_clusters = data.size / 2
167 | if method not in available_methods or method != 'gap_concentration' and function is None:
168 | warnings.warn("Unsupported clusters estimation function " + method + ". Used gap concentration instead.")
169 | method = gap_concentration
170 | if method == 'gap_concentration':
171 | image, change_points = gap_concentration(data, max_clusters)
172 | clusters = len(change_points[1]) - 1
173 | if gen_init_state:
174 | # generating initial state for cluster iterations
175 | init_list = np.reshape(data, (len(data), 1))
176 | # exclude last element if greater than 2
177 | if len(change_points[1]) > 2:
178 | init_cntrs = np.reshape(change_points[1][:-1], (len(change_points[1]) - 1, 1))
179 | else:
180 | init_cntrs = np.reshape(change_points[1], (len(change_points[1]), 1))
181 | init_state = one_dimension_distance(init_list, init_cntrs)
182 | if 'init' not in kwargs or kwargs['init'] is None:
183 | kwargs['init'] = init_state
184 | # clusters_desc, centrs = function(data, clusters, m=kwargs['m'], error=kwargs['error'],
185 | # maxiter=kwargs['maxiter'], init=kwargs['init'], seed=kwargs['seed'])
186 | if clusters > 1:
187 | clusters_desc, centrs = function(data, clusters, **kwargs)
188 | else:
189 | clusters = 1
190 | clusters_desc = np.ones(len(data), dtype=np.float64)
191 | if method == 'fuzzy_silhouette':
192 | clusters, clusters_desc, final_fuzzy_silhouette = fuzzy_silhouette(data, max_clusters, alpha=2,
193 | function=function, **kwargs)
194 | if method == 'combined_fuzzy_entropy':
195 | clusters, clusters_desc, final_cfe = cfe(data, max_clusters, function=function, **kwargs)
196 | return clusters, clusters_desc
197 |
--------------------------------------------------------------------------------
/py_fcm/learning/discretization/fuzzy_cmeans.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | from numba import njit
3 |
4 | from py_fcm.learning.utils import normalize_columns, normalize_power_columns, one_dimension_distance
5 |
6 |
7 | @njit
8 | def _cmeans0(data, u_old, m):
9 | """
10 | Single step in generic fuzzy c-means clustering algorithm.
11 | Modified from Ross, Fuzzy Logic w/Engineering Applications (2010),
12 | pages 352-353, equations 10.28 - 10.35.
13 | Parameters inherited from cmeans()
14 | """
15 | # Normalizing, then eliminating any potential zero values.
16 | u_old = normalize_columns(u_old)
17 |
18 | u_old = np.fmax(u_old, np.finfo(np.float64).eps)
19 |
20 | um = u_old ** m
21 | # Calculate cluster centers
22 | # data = data.T
23 | cntr = um.dot(data) / np.atleast_2d(um.sum(axis=1)).T
24 |
25 | d = one_dimension_distance(data, cntr)
26 | d = np.fmax(d, np.finfo(np.float64).eps)
27 |
28 | jm = (um * d ** 2).sum()
29 |
30 | u = normalize_power_columns(d, - 2. / (m - 1))
31 |
32 | return cntr, u, jm, d
33 |
34 |
35 | @njit
36 | def _fp_coeff(u):
37 | """
38 | Fuzzy partition coefficient `fpc` relative to fuzzy c-partitioned
39 | matrix `u`. Measures 'fuzziness' in partitioned clustering.
40 | Parameters
41 | ----------
42 | u : 2d array (C, N)
43 | Fuzzy c-partitioned matrix; N = number of data points and C = number
44 | of clusters.
45 | Returns
46 | -------
47 | fpc : float
48 | Fuzzy partition coefficient.
49 | """
50 | n = u.shape[1]
51 |
52 | return np.trace(u.dot(u.T)) / float(n)
53 |
54 |
55 | @njit
56 | def cmeans(data, c, m, error, maxiter,
57 | init=None, seed=None):
58 | """
59 | Fuzzy c-means clustering algorithm [1].
60 | Parameters
61 | ----------
62 | data : 2d array, size (S, N)
63 | Data to be clustered. N is the number of data sets; S is the number
64 | of features within each sample vector.
65 | c : int
66 | Desired number of clusters or classes.
67 | m : float
68 | Array exponentiation applied to the membership function u_old at each
69 | iteration, where U_new = u_old ** m.
70 | error : float
71 | Stopping criterion; stop early if the norm of (u[p] - u[p-1]) < error.
72 | maxiter : int
73 | Maximum number of iterations allowed.
74 | metric: string
75 | By default is set to euclidean. Passes any option accepted by
76 | ``scipy.spatial.distance.cdist``.
77 | init : 2d array, size (c, N)
78 | Initial fuzzy c-partitioned matrix. If none provided, algorithm is
79 | randomly initialized.
80 | seed : int
81 | If provided, sets random seed of init. No effect if init is
82 | provided. Mainly for debug/testing purposes.
83 | Returns
84 | -------
85 | cntr : 2d array, size (c, S)
86 | Cluster centers. Data for each center along each feature provided
87 | for every cluster (of the `c` requested clusters).
88 | u : 2d array, (c, N)
89 | Final fuzzy c-partitioned matrix.
90 | u0 : 2d array, (c, N)
91 | Initial guess at fuzzy c-partitioned matrix (either provided init or
92 | random guess used if init was not provided).
93 | d : 2d array, (c, N)
94 | Final Euclidian distance matrix.
95 | jm : 1d array, length P
96 | Objective function history.
97 | p : int
98 | Number of iterations run.
99 | fpc : float
100 | Final fuzzy partition coefficient.
101 | Notes
102 | -----
103 | The algorithm implemented is from Ross et al. [1]_.
104 | Fuzzy C-Means has a known problem with high dimensionality datasets, where
105 | the majority of cluster centers are pulled into the overall center of
106 | gravity. If you are clustering data with very high dimensionality and
107 | encounter this issue, another clustering method may be required. For more
108 | information and the theory behind this, see Winkler et al. [2]_.
109 | References
110 | ----------
111 | .. [1] Ross, Timothy J. Fuzzy Logic With Engineering Applications, 3rd ed.
112 | Wiley. 2010. ISBN 978-0-470-74376-8 pp 352-353, eq 10.28 - 10.35.
113 | .. [2] Winkler, R., Klawonn, F., & Kruse, R. Fuzzy c-means in high
114 | dimensional spaces. 2012. Contemporary Theory and Pragmatic
115 | Approaches in Fuzzy Computing Utilization, 1.
116 | """
117 | # Setup u0
118 | n = data.shape[0]
119 | if init is None:
120 | if seed is not None:
121 | np.random.seed(seed=seed)
122 | u0 = np.random.rand(c, n)
123 | u0 = normalize_columns(u0)
124 | init = u0.copy()
125 | # print(init)
126 | u0 = init
127 | u = np.zeros((c, n), dtype=np.float64)
128 | for i in range(c):
129 | for j in range(n):
130 | u[i, j] = max(u0[i, j], np.finfo(np.float64).eps)
131 |
132 | # Initialize loop parameters
133 | jm = np.zeros(0)
134 | p = 0
135 |
136 | while p < maxiter - 1:
137 | u2 = u.copy()
138 | cntr, u, Jjm, d = _cmeans0(data, u2, m)
139 | jm = np.append(jm, Jjm)
140 | p += 1
141 |
142 | # Stopping rule
143 | if np.linalg.norm(u - u2) < error:
144 | break
145 |
146 | # error = np.linalg.norm(u - u2)
147 | fpc = _fp_coeff(u)
148 | return cntr, u, u0, d, jm, p, fpc
149 |
150 |
151 | @njit
152 | def fuzzy_cmeans(data: np.array, c: int, m=2.5, error=0.0001, maxiter=250, init=None, seed=None):
153 | """Simplify the clustering algorithm function call"""
154 | cntr, u, u0, d, jm, p, fpc = cmeans(data, c, m, error, maxiter, init, seed)
155 | return u, cntr
156 |
--------------------------------------------------------------------------------
/py_fcm/learning/discretization/rl_fuzzy_cmeans.py:
--------------------------------------------------------------------------------
1 | import math
2 | import numpy as np
3 | from numba import njit
4 |
5 |
6 | @njit
7 | def _compute_u(data, v, alpha, r1, r2):
8 | u = np.zeros((len(data), len(v)), dtype=np.float64)
9 | c = len(alpha)
10 | for i in range(u.shape[0]):
11 | denom = 0.0
12 | for t in range(c):
13 | d_it = np.linalg.norm(data[i] - v[t])
14 | denom += math.exp((-(d_it ** 2) + (r1 * math.log(alpha[t]))) / r2)
15 | for k in range(u.shape[1]):
16 | d_ik = np.linalg.norm(data[i] - v[k])
17 | u[i][k] = math.exp((-(d_ik ** 2) + (r1 * math.log(alpha[k]))) / r2) / denom
18 | return u
19 |
20 |
21 | @njit
22 | def _update_alpha(u, alpha, r1, r3):
23 | new_alpha = alpha.copy()
24 | n = len(u)
25 | c = len(alpha)
26 | ln_accum = np.sum(np.multiply(alpha, np.log2(alpha)))
27 | for k in range(c):
28 | par_content = np.log2(alpha[k]) - ln_accum
29 | pre_val = 0.0
30 | for i in range(len(u)):
31 | pre_val += u[i][k] + ((r3 / r1) * alpha[k] * par_content)
32 | new_alpha[k] = pre_val / n
33 | return new_alpha
34 |
35 |
36 | @njit
37 | def _update_r3(alpha_old, new_alpha, u):
38 | # TODO: add niu function
39 | niu = 1
40 | n = len(u)
41 | c = len(alpha_old)
42 | num = 0.0
43 | ku_sum = []
44 | v2_denom_accum = []
45 |
46 | alpha_old_sum = 0.0
47 | for t in range(c):
48 | alpha_old_sum += alpha_old[t] * math.log(alpha_old[t])
49 | for k in range(c):
50 | num += math.exp(-niu * n * abs(new_alpha[k] - alpha_old[k]))
51 | ku_sum.append(u.sum(0)[k] / n)
52 | v2_denom_accum.append(alpha_old[k] * alpha_old_sum)
53 | v1 = num / c
54 | v2 = (1 - max(ku_sum)) / (-max(v2_denom_accum))
55 |
56 | return min(v1, v2)
57 |
58 |
59 | @njit
60 | def _resize_references(alpha, u):
61 | j = 0
62 | n = len(u)
63 | c = alpha.size
64 | for t in range(c):
65 | if alpha[t] < (1 / n):
66 | j += 1
67 | new_c = c - j
68 | resized_alpha = np.empty(new_c, dtype=alpha.dtype)
69 | resized_u = np.zeros((n, new_c), dtype=u.dtype)
70 | j = 0
71 | for t in range(c):
72 | if alpha[t] >= 1 / n:
73 | resized_alpha[j] = alpha[t]
74 | for i in range(n):
75 | resized_u[i][j] = u[i][t]
76 | j += 1
77 | resized_alpha = resized_alpha / resized_alpha.sum()
78 | for i in range(n):
79 | resized_u[i] = resized_u[i] / resized_u[i].sum()
80 |
81 | return resized_alpha, resized_u
82 |
83 |
84 | @njit
85 | def _update_v(data, u, c):
86 | new_v = np.zeros((c, data.shape[1]), dtype=u.dtype)
87 | u_sum = u.sum(0)
88 | for k in range(c):
89 | num = np.zeros(data.shape[1], dtype=u.dtype)
90 | for i in range(len(data)):
91 | num = num + (data[i] * u[i][k])
92 | new_v[k] = num / u_sum[k]
93 | return new_v
94 |
95 |
96 | @njit
97 | def rl_fuzzy_cmeans(data, error=0.0005, max_iter=110):
98 | u = None
99 | c = data.shape[0]
100 | r1 = r2 = r3 = t = 1
101 | v = data.copy()
102 | alpha = np.full(c, 1 / c, dtype=np.float64)
103 | while t < max_iter:
104 | u = _compute_u(data, v, alpha, r1, r2)
105 | r1 = math.exp(-t / 10)
106 | r2 = math.exp(-t / 100)
107 | new_alpha = _update_alpha(u, alpha, r1, r3)
108 | r3 = _update_r3(alpha, new_alpha, u)
109 | new_alpha, u = _resize_references(new_alpha, u)
110 | c = len(new_alpha)
111 | if t >= 100 and (len(alpha) - len(new_alpha)) == 0:
112 | r3 = 0
113 | new_v = _update_v(data, u, c)
114 | if len(new_alpha) == 1 or len(new_v) == len(v) and np.linalg.norm(new_v - v) < error:
115 | v = new_v
116 | alpha = new_alpha
117 | break
118 | v = new_v
119 | alpha = new_alpha
120 | t += 1
121 |
122 | return v, u, alpha, t
123 |
124 |
125 | class RlFuzzyCmeans:
126 | centroids = None
127 |
128 | def fit(self, X):
129 | v, u, alpha, t = rl_fuzzy_cmeans(X)
130 | print("Clusters: ", len(v))
131 | self.centroids = v
132 |
133 | def predict(self, X):
134 | y_pred = []
135 | for element in X:
136 | min_dist = np.linalg.norm(self.centroids[0] - element)
137 | res = 0
138 | for cent_pos in range(1, len(self.centroids)):
139 | curr_dist = np.linalg.norm(self.centroids[cent_pos] - element)
140 | if curr_dist < min_dist:
141 | min_dist = curr_dist
142 | res = cent_pos
143 | y_pred.append(res)
144 | return y_pred
145 |
--------------------------------------------------------------------------------
/py_fcm/learning/utils.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | from numba import njit
3 |
4 |
5 | # TODO: use njit decorator
6 | def gen_discrete_feature_matrix(val_list, unique_values):
7 | val_num = len(val_list)
8 | feat_num = len(unique_values)
9 | matrix = np.zeros((feat_num, val_num), dtype=np.int8)
10 | for val_pos in range(val_num):
11 | for single_val_pos in range(feat_num):
12 | if val_list[val_pos] == unique_values[single_val_pos]:
13 | matrix[single_val_pos][val_pos] = 1
14 | return matrix
15 |
16 |
17 | @njit
18 | def calc_concepts_coefficient(array1: np.array, array2: np.array):
19 | if len(array1) == len(array2):
20 | a_b = 0
21 | na_b = 0
22 | a_nb = 0
23 | na_nb = 0
24 | for val_pos in range(len(array1)):
25 | a_b += array1[val_pos] * array2[val_pos]
26 | a_nb += array1[val_pos] * (1 - array2[val_pos])
27 | na_b += (1 - array1[val_pos]) * array2[val_pos]
28 | na_nb += (1 - array1[val_pos]) * (1 - array2[val_pos])
29 | # return: a->b relation coefficients, b->a relation coefficients
30 | return (a_b, a_nb, na_b, na_nb), (a_b, na_b, a_nb, na_nb)
31 |
32 |
33 | @njit
34 | def one_dimension_distance(data: np.ndarray, centers: np.ndarray):
35 | res = np.empty((len(centers), len(data)), dtype=np.float64)
36 | for c_pos in range(len(centers)):
37 | for d_pos in range(len(data)):
38 | res[c_pos][d_pos] = np.linalg.norm(data[d_pos] - centers[c_pos])
39 | return res
40 |
41 |
42 | @njit
43 | def normalize_columns(columns):
44 | """
45 | Normalize columns of matrix.
46 | Parameters
47 | ----------
48 | columns : 2d array (M x N)
49 | Matrix with columns
50 | Returns
51 | -------
52 | normalized_columns : 2d array (M x N)
53 | columns/np.sum(columns, axis=0, keepdims=1)
54 | """
55 |
56 | # broadcast sum over columns
57 | normalized_columns = columns / np.sum(columns, axis=0)
58 |
59 | return normalized_columns
60 |
61 |
62 | @njit
63 | def normalize_power_columns(columns: np.ndarray, exponent: float):
64 | """
65 | Calculate normalize_columns(x**exponent)
66 | in a numerically safe manner.
67 | Parameters
68 | ----------
69 | columns : 2d array (M x N)
70 | Matrix with columns
71 | exponent : float
72 | Exponent
73 | Returns
74 | -------
75 | result : 2d array (M x N)
76 | normalize_columns(x**exponent) but safe
77 | """
78 |
79 | assert np.all(columns >= 0.0)
80 |
81 | columns = columns.astype(np.float64)
82 |
83 | # values in range [0, 1]
84 | # columns = columns / np.max(columns, axis=0)
85 | for col in range(columns.shape[1]):
86 | columns[:, col] /= np.max(columns[:, col])
87 |
88 | # values in range [eps, 1]
89 |
90 | columns = np.fmax(columns, np.finfo(columns.dtype).eps)
91 |
92 | if exponent < 0:
93 | # values in range [1, 1/eps]
94 | # columns /= np.min(columns, axis=0)
95 | for col in range(columns.shape[1]):
96 | columns[:, col] /= np.min(columns[:, col])
97 |
98 | # values in range [1, (1/eps)**exponent] where exponent < 0
99 | # this line might trigger an underflow warning
100 | # if (1/eps)**exponent becomes zero, but that's ok
101 | columns = columns ** exponent
102 | else:
103 | # values in range [eps**exponent, 1] where exponent >= 0
104 | columns = columns ** exponent
105 |
106 | result = normalize_columns(columns)
107 |
108 | return result
109 |
110 |
111 | # ensure compilation
112 | # gen_discrete_feature_matrix(np.array(['1', '1', '2', '2']), np.array(['1', '2']))
113 | calc_concepts_coefficient(np.array([1, 0, 0, 1]), np.array([0, 0, 1, 1]))
114 | one_dimension_distance(np.array([[-1], [-1.5], [-2]]), np.array([[0.1], [0.3]]))
115 | normalize_columns(np.array([[1.0, 1.8], [0.3, 0.7]]))
116 | normalize_power_columns(np.array([[1.0, 1.8], [0.3, 0.7]]), 2)
117 |
--------------------------------------------------------------------------------
/py_fcm/loader.py:
--------------------------------------------------------------------------------
1 | import json
2 |
3 | from py_fcm.utils.__const import *
4 | from py_fcm.fcm import FuzzyCognitiveMap
5 |
6 |
7 | def from_json(str_json: str):
8 | """
9 | Function to genrate a FCM object form a JSON like:
10 | {
11 | "max_iter": 500,
12 | "activation_function": "sigmoid",
13 | "actv_func_args": {"lambda_val":1},
14 | "memory_influence": false,
15 | "decision_function": "LAST",
16 | "concepts" :
17 | [
18 | {"id": "concept_1", "type": "SIMPLE", "activation": 0.5},
19 | {"id": "concept_2", "type": "DECISION", "custom_function": "gceq", "custom_function_args": {"weight":0.3}},
20 | {"id": "concept_3", "type": "SIMPLE", "memory_influence":true },
21 | {"id": "concept_4", "type": "SIMPLE", "custom_function": "saturation", "activation": 0.3}
22 | ],
23 | "relations":
24 | [
25 | {"origin": "concept_4", "destiny": "concept_2", "weight": -0.1},
26 | {"origin": "concept_1", "destiny": "concept_3", "weight": 0.59},
27 | {"origin": "concept_3", "destiny": "concept_2", "weight": 0.8911}
28 | ]
29 | }
30 | Structure:
31 | * iter: max map iterations, required.
32 | * activation_function: defalt activation function, required
33 | * activation_function_args: object (JSON serializable) to describe required function params, optional value
34 | * memory_influence: use memory or not, required
35 | * stability_diff: difference to consider a stable FCM state, optional with 0.001 by default
36 | * stop_at_stabilize: stop the inference process when the FCM reach a stable state, optional True by default
37 | * extra_steps: additional steps to execute after reach a stable state, optionay with 5 by default
38 | * weight: FCM weight ti be used in joint map process, optional with 1 by default
39 | * decision_function: define the decision function to get the final value, required:
40 | - "LAST": last inference value
41 | - "MEAN": whole execution average value
42 | - "EXITED": Highest last execution value in decision nodes
43 | * concepts: a concept list describing each concept, required
44 | * relations: a relations list between defined concepts, required
45 |
46 | Concept descrption:
47 | * id: concept id
48 | * type: node type => "SIMPLE": regular node and default ,"DECISION": target for a classification problems
49 | * active: define if node is active or not, by default is considered active
50 | * custom_function: custom node function, by default use map defined function
51 | * custom_function_args: object (JSON serializable) to describe custom_function params
52 | * memory_influence: use memory or not, by default use FCM memory definition
53 | * exitation_function: node exitation function, KOSKO by default
54 | * activation: initial node activation value, by default 0
55 |
56 | Relation descrption:
57 | * origin: start concept id
58 | * destiny: destiny concept id
59 | * weight: relaion weight in range => [-1,1]
60 |
61 | Exitation functions:
62 | * "MEAN": Mean values of all neighbors that influence the node
63 | * "KOSKO": B. Kosko proposed activation function
64 | * "PAPAGEORGIUS": E. Papageorgius proposed function to avoid saturation
65 |
66 | Activation functions:
67 | * "saturation": 1 if value is > 1, 0 if values is < 0 and value otherwise. Domain => [0,1]
68 | * "biestate": 1 if value is > 0, 0 otherwise. Domain => {0,1}
69 | * "threestate": 0 if value is < 0.25, 0.5 if 0.25 <= value <= 0.75, 1 otherwise. Domain => {0,0.5,1}
70 | * "gceq": weight(float), return value if > weight, 0 otherwise. Domain => [-1,1]
71 | * "sigmoid": lambda_val(int), sigmoid function => [0,1]
72 | * "sigmoid_hip": lambda_val(int), sigmoid hyperbolic function => [-1,1]
73 |
74 | Args:
75 | str_json: string JSON
76 |
77 | Returns: FCM object
78 |
79 | """
80 | try:
81 | data_dict = json.loads(str_json)
82 | actv_param = {}
83 | stability_diff = 0.001
84 | stop_at_stabilize = True
85 | extra_steps = 5
86 | weight = 1
87 | if 'activation_function_args' in data_dict:
88 | actv_param = data_dict['activation_function_args']
89 |
90 | if 'stability_diff' in data_dict:
91 | stability_diff = data_dict['stability_diff']
92 |
93 | if 'stop_at_stabilize' in data_dict:
94 | stop_at_stabilize = data_dict['stop_at_stabilize']
95 |
96 | if 'extra_steps' in data_dict:
97 | extra_steps = data_dict['extra_steps']
98 |
99 | if 'weight' in data_dict:
100 | weight = data_dict['weight']
101 |
102 | my_fcm = FuzzyCognitiveMap(max_it=data_dict['max_iter'],
103 | decision_function=data_dict['decision_function'],
104 | mem_influence=data_dict['memory_influence'],
105 | activation_function=data_dict['activation_function'],
106 | stability_diff=stability_diff,
107 | stabilize=stop_at_stabilize,
108 | extra_steps=extra_steps,
109 | **actv_param)
110 | my_fcm.weight = weight
111 | # adding concepts
112 | for concept in data_dict['concepts']:
113 | use_mem = None
114 | if 'memory_influence' in concept:
115 | use_mem = concept['memory_influence']
116 | exitation = 'KOSKO'
117 | if 'exitation_function' in concept:
118 | exitation = concept['exitation_function']
119 | active = True
120 | if 'active' in concept:
121 | active = concept['active']
122 | custom_function = None
123 | if 'custom_function' in concept:
124 | custom_function = concept['custom_function']
125 | custom_func_args = {}
126 | if 'custom_function_args' in concept:
127 | custom_func_args = concept['custom_function_args']
128 | activation_dict = None
129 | if 'activation_dict' in concept:
130 | activation_dict = concept['activation_dict']
131 | concept_type = TYPE_SIMPLE
132 | if concept['type'] == 'DECISION':
133 | concept_type = TYPE_DECISION
134 | my_fcm.add_concept(concept['id'],
135 | concept_type=concept_type,
136 | is_active=active,
137 | use_memory=use_mem,
138 | excitation_function=exitation,
139 | activation_function=custom_function,
140 | activation_dict=activation_dict,
141 | **custom_func_args)
142 | if 'activation' in concept:
143 | my_fcm.init_concept(concept['id'], concept['activation'])
144 |
145 | # adding relations
146 | for relation in data_dict['relations']:
147 | my_fcm.add_relation(origin_concept=relation['origin'],
148 | destiny_concept=relation['destiny'],
149 | weight=relation['weight'])
150 | return my_fcm
151 | except Exception as err:
152 | raise Exception("Cannot load json data due: " + str(err))
153 |
154 |
155 | def join_maps(map_set, concept_strategy='union', value_strategy="average", relation_strategy="average",
156 | ignore_zeros=False):
157 | """
158 | Join a set of FuzzyCognitiveMap in a new one according to defined strategy. All nodes will be set to default
159 | behaviour to avid mixing issues in the result. The final map also will be created with default behavior so, is
160 | required to update the map behavior after join process. Default setting will be updated on future library versions.
161 | Args:
162 | map_set: An iterable object that contains the FCMs
163 | concept_strategy: Strategy to join all maps nodes
164 | union: the new FuzzyCognitiveMap will have the set union of nodes in map_set
165 | intersection: the new FuzzyCognitiveMap will have the set intersection of nodes in map_set
166 | value_strategy: Strategy to define the initial state of map nodes
167 | highest: Select the highest node value as initial node state
168 | lowest: Select the lowest node value as initial node state
169 | average: Select the average of node values as initial node state
170 | relation_strategy: Strategy to define the value for repeated relations weight in map topology
171 | highest: Select the highest relation value as new relation value
172 | lowest: Select the lowest relation value as new relation value
173 | average: Select the average of relations values as new relation value
174 | ignore_zeros: Ignore zero evaluated concepts in value_strategy selected
175 |
176 | Returns: A new FuzzyCognitiveMap generated using defined strategies
177 |
178 | """
179 | concept_strategies = {'union', 'intersection'}
180 | value_strategies = {'highest', 'lowest', 'average'}
181 | relation_strategies = {'highest', 'lowest', 'average'}
182 | if concept_strategy not in concept_strategies:
183 | raise Exception("Unknown concept strategy: " + concept_strategy)
184 | if value_strategy not in value_strategies:
185 | raise Exception("Unknown value strategy: " + value_strategy)
186 | if relation_strategy not in relation_strategies:
187 | raise Exception("Unknown relation strategy: " + relation_strategy)
188 |
189 | nodes_desc = {}
190 | relations = []
191 | is_first = True
192 | final_map = {}
193 | if len(map_set) > 0:
194 | for fcm in map_set:
195 | map_desc = json.loads(fcm.to_json())
196 | for relation in map_desc['relations']:
197 | relations.append(relation)
198 | if is_first:
199 | is_first = False
200 | final_map = map_desc
201 | for concept in map_desc['concepts']:
202 | nodes_desc[concept['id']] = concept
203 | nodes_desc[concept['id']]['accumulation'] = [nodes_desc[concept['id']]['activation']]
204 | else:
205 | new_node_set = {}
206 | for concept in map_desc['concepts']:
207 | new_node_set[concept['id']] = concept
208 | if concept_strategy == 'union':
209 | for key in new_node_set:
210 | if key in nodes_desc:
211 | nodes_desc[key]['accumulation'].append(new_node_set[key]['activation'])
212 | else:
213 | nodes_desc[key] = new_node_set[key]
214 | nodes_desc[key]['accumulation'] = [nodes_desc[key]['activation']]
215 | if concept_strategy == 'intersection':
216 | node_set = set(nodes_desc.keys())
217 | node_set = node_set.intersection(new_node_set.keys())
218 | to_remove = []
219 | for key in nodes_desc:
220 | if key not in node_set:
221 | to_remove.append(key)
222 | else:
223 | nodes_desc[key]['accumulation'].append(new_node_set[key]['activation'])
224 | for key in to_remove:
225 | nodes_desc.pop(key)
226 | final_concepts = []
227 | for key in nodes_desc:
228 | if value_strategy == "highest":
229 | nodes_desc[key]['activation'] = max(nodes_desc[key]['accumulation'])
230 | if value_strategy == "lowest":
231 | nodes_desc[key]['activation'] = min(nodes_desc[key]['accumulation'])
232 | if value_strategy == "average":
233 | num_elements = len(nodes_desc[key]['accumulation'])
234 | if num_elements > 0:
235 | nodes_desc[key]['activation'] = sum(nodes_desc[key]['accumulation']) / num_elements
236 | nodes_desc[key].pop('accumulation')
237 | if nodes_desc[key]['activation'] != 0:
238 | final_concepts.append(nodes_desc[key])
239 | elif not ignore_zeros:
240 | final_concepts.append(nodes_desc[key])
241 |
242 | relation_data = {}
243 | rel_separator = ' |=====> '
244 | for curr_relation in relations:
245 | relation_name = curr_relation['origin'] + rel_separator + curr_relation['destiny']
246 | if relation_name not in relation_data:
247 | relation_data[relation_name] = [curr_relation['weight']]
248 | else:
249 | relation_data[relation_name].append(curr_relation['weight'])
250 |
251 | final_relations = []
252 | for curr_relation in relation_data:
253 | origin = curr_relation.split(rel_separator)[0]
254 | destiny = curr_relation.split(rel_separator)[1]
255 | if origin in nodes_desc and destiny in nodes_desc:
256 | new_relation = {
257 | 'origin': origin,
258 | 'destiny': destiny
259 | }
260 | if relation_strategy == "highest":
261 | new_relation['weight'] = max(relation_data[curr_relation])
262 | if relation_strategy == "lowest":
263 | new_relation['weight'] = min(relation_data[curr_relation])
264 | if relation_strategy == "average":
265 | new_relation['weight'] = sum(relation_data[curr_relation]) / len(relation_data[curr_relation])
266 | final_relations.append(new_relation)
267 |
268 | final_map['concepts'] = final_concepts
269 | final_map['relations'] = final_relations
270 | final_json = json.dumps(final_map)
271 | return from_json(final_json)
272 | return FuzzyCognitiveMap()
273 |
--------------------------------------------------------------------------------
/py_fcm/utils/__const.py:
--------------------------------------------------------------------------------
1 | # Saturation-type function
2 | FUNC_SATURATION = 0
3 | # Bistate-type function
4 | FUNC_BISTATE = 1
5 | # Tristate-type function
6 | FUNC_THREESTATE = 2
7 | # Sigmoid-type function
8 | FUNC_SIGMOID = 3
9 | # Sigmoid Hyperbolic / Tangent Hyperbolic -type function
10 | FUNC_SIGMOID_HIP = 4
11 | # Fuzzy-type function
12 | FUNC_FUZZY = 5
13 | # Greater conditional equality
14 | FUNC_GCEQ = 6
15 | # Lesser conditional equality
16 | FUNC_LCEQ = 7
17 | # Rectified Linear Activation Modified
18 | FUNC_RELM = 8
19 |
20 | # relations const
21 | RELATION_DESTINY = 0
22 | RELATION_WEIGHT = 1
23 | RELATION_ORIGIN = 2
24 |
25 | # topology const
26 | NODE_ACTIVE = "current node is active or not" # non active nodes do not activate neighbors
27 | NODE_ARCS = "current node outgoing arcs"
28 | NODE_TYPE = "type of node"
29 | NODE_VALUE = "current node value"
30 | NODE_USE_MEM = "use memory in current node execution"
31 | NODE_ACTV_SUM = "maximum node activation input value"
32 | NODE_AUX = "auxiliary influence value list for new value calculation"
33 | NODE_USE_MAP_FUNC = "use map activation function"
34 | NODE_EXEC_FUNC = "custom execution function"
35 | NODE_EXEC_FUNC_NAME = "custom execution function name"
36 | NODE_ACTV_FUNC = "custom activation function"
37 | NODE_ACTV_FUNC_NAME = "custom activation function name"
38 | NODE_ACTV_FUNC_ARGS = "custom activation function arguments"
39 | NODE_ACTV_FUNC_ARGS_VECT = "custom activation function arguments in vector"
40 | NODE_FUZZY_ACTIVATION = "activation relation for continuous values"
41 | NODE_FUZZY_MIN = "minimum continuous value"
42 | NODE_FUZZY_MAX = "maximum continuous value"
43 |
44 | # node types
45 | TYPE_SIMPLE = "default node type"
46 | TYPE_DECISION = "node type for classification problems"
47 | TYPE_FUZZY = "node type for fuzzy treatment of continuous values"
48 | TYPE_REGRESOR = "node type for regression problems"
49 | TYPE_MUTI = "node type for multivalues fields"
50 | TYPE_MUTI_DESC = "node type for multivalues fields in decision nodes"
51 |
52 |
53 | def is_valid_type(node_type) -> bool:
54 | type_set = set()
55 | type_set.add(TYPE_SIMPLE)
56 | type_set.add(TYPE_DECISION)
57 | type_set.add(TYPE_FUZZY)
58 | type_set.add(TYPE_REGRESOR)
59 | type_set.add(TYPE_MUTI)
60 | type_set.add(TYPE_MUTI_DESC)
61 | return node_type in type_set
62 |
--------------------------------------------------------------------------------
/py_fcm/utils/__init__.py:
--------------------------------------------------------------------------------
1 | import ast
2 | from collections import OrderedDict
3 |
4 | import arff
5 | import pandas
6 |
7 |
8 | def load_dataset(ds_path, factorize=False, max_int_uniques=10, int_factor=0.2):
9 | ATT_NAME = 0
10 | data_dict = OrderedDict()
11 | ds_name = ds_path.split('/')[-1]
12 | ds_ext = ds_name.split('.')[-1]
13 | known_format = False
14 | last_feat = ''
15 | try:
16 | if 'arff' == ds_ext:
17 | with open(ds_path) as file:
18 | text = file.read()
19 | arff_dict = arff.loads(text)
20 | for attribute in arff_dict['attributes']:
21 | data_dict[attribute[ATT_NAME]] = []
22 | for row in arff_dict['data']:
23 | for att_pos in range(0, len(arff_dict['attributes'])):
24 | try:
25 | data_dict[arff_dict['attributes'][att_pos][ATT_NAME]].append(ast.literal_eval(row[att_pos]))
26 | last_feat = arff_dict['attributes'][att_pos][ATT_NAME]
27 | except:
28 | data_dict[arff_dict['attributes'][att_pos][ATT_NAME]].append(row[att_pos])
29 | known_format = True
30 | if 'csv' == ds_ext:
31 | with open(ds_path) as file:
32 | content = []
33 | for line in file:
34 | content.append(line.strip())
35 | # first line describe the atributes names
36 | attributes = str(content[0]).split(',')
37 | for current_att in attributes:
38 | data_dict[current_att] = []
39 | for line in range(1, len(content)):
40 | data_line = str(content[line]).split(',')
41 | # avoid rows with different attributes length
42 | if len(data_line) == len(attributes):
43 | # the missing data must be identified
44 | for data in range(0, len(data_line)):
45 | # reusing for value type inference
46 | try:
47 | data_dict[attributes[data]].append(ast.literal_eval(data_line[data]))
48 | last_feat = attributes[data]
49 | except:
50 | data_dict[attributes[data]].append(data_line[data])
51 | else:
52 | # Handle errors in dataset matrix
53 | raise Exception("Errors in line: ", line, len(data_line), len(attributes))
54 | known_format = True
55 | except Exception as err:
56 | print("Error: ", str(err))
57 | if not known_format:
58 | raise Exception("Unknown dataset format")
59 | else:
60 | if 'class' in data_dict:
61 | codes, uniques = pandas.factorize(data_dict['class'])
62 | classes = len(uniques)
63 | objects = len(codes)
64 | else:
65 | codes, uniques = pandas.factorize(data_dict[last_feat])
66 | classes = len(uniques)
67 | objects = len(codes)
68 | print("\n===> Dataset for test: ", ds_name)
69 | print('Features: ', len(data_dict), " | Objects: ", objects, " | Classes: ", classes)
70 |
71 | try:
72 | if factorize:
73 | for key in data_dict:
74 | if type(data_dict[key][0]) == str:
75 | codes, uniques = pandas.factorize(data_dict[key])
76 | data_dict[key] = codes
77 | elif type(data_dict[key][0]) == int:
78 | codes, uniques = pandas.factorize(data_dict[key])
79 | if len(uniques) <= max_int_uniques or (len(uniques) / len(codes)) < int_factor:
80 | data_dict[key] = codes
81 | # remove equal entries
82 | dataset_frame = pandas.DataFrame(data_dict).drop_duplicates()
83 | except Exception as err:
84 | for key, value in data_dict.items():
85 | print(key, value)
86 | pass
87 | raise Exception(ds_name + " " + str(err))
88 |
89 | return dataset_frame
90 |
--------------------------------------------------------------------------------
/py_fcm/utils/functions.py:
--------------------------------------------------------------------------------
1 | import math
2 | from math import exp
3 |
4 | import numpy as np
5 | from numba.typed import List
6 | from numba import njit
7 |
8 | from py_fcm.utils.__const import *
9 |
10 |
11 | @njit
12 | def __partition(array: np.array, start: int, end: int, mirrored_array: np.array):
13 | pivot = array[start]
14 | low = start + 1
15 | high = end
16 |
17 | while True:
18 | while low <= high and array[high] >= pivot:
19 | high = high - 1
20 |
21 | while low <= high and array[low] <= pivot:
22 | low = low + 1
23 | if low <= high:
24 | array[low], array[high] = array[high], array[low]
25 | mirrored_array[low], mirrored_array[high] = mirrored_array[high], mirrored_array[low]
26 | else:
27 | break
28 |
29 | array[start], array[high] = array[high], array[start]
30 | mirrored_array[start], mirrored_array[high] = mirrored_array[high], mirrored_array[start]
31 |
32 | return high
33 |
34 |
35 | @njit
36 | def dual_quick_sort(main_array: np.array, start: int, end: int, mirrored_array: np.array):
37 | if start >= end:
38 | return
39 | p = __partition(main_array, start, end, mirrored_array)
40 | dual_quick_sort(main_array, start, p - 1, mirrored_array)
41 | dual_quick_sort(main_array, p + 1, end, mirrored_array)
42 |
43 |
44 | @njit
45 | def __exec_actv_function(function_id: int, val: float, args=np.empty(1, dtype=np.float64)) -> float:
46 | if function_id == FUNC_SATURATION:
47 | return saturation(val)
48 | if function_id == FUNC_BISTATE:
49 | return bistate(val)
50 | if function_id == FUNC_THREESTATE:
51 | return threestate(val)
52 | if function_id == FUNC_GCEQ:
53 | if args.size == 0:
54 | return greater_cond_equality(val)
55 | else:
56 | return greater_cond_equality(val, weight=args[0])
57 | if function_id == FUNC_LCEQ:
58 | if args.size == 0:
59 | return lower_cond_equality(val)
60 | else:
61 | return lower_cond_equality(val, weight=args[0])
62 | if function_id == FUNC_SIGMOID:
63 | if args.size == 0:
64 | return sigmoid(val)
65 | else:
66 | return sigmoid(val, lambda_val=args[0])
67 | if function_id == FUNC_SIGMOID_HIP:
68 | if args.size == 0:
69 | return sigmoid_hip(val)
70 | else:
71 | return sigmoid_hip(val, lambda_val=args[0])
72 | if function_id == FUNC_RELM:
73 | if args.size == 0:
74 | return relm(val)
75 | else:
76 | return relm(val, lambda_val=args[0])
77 | if function_id == FUNC_FUZZY:
78 | membership = args[:int(args.size / 2)]
79 | val_list = args[int(args.size / 2):]
80 | return fuzzy_set(val, membership, val_list)
81 |
82 |
83 | # vectorized inference process
84 | @njit
85 | def vectorized_run(state_vector: np.ndarray, relation_matrix: np.ndarray, functions: np.ndarray, func_args: List,
86 | reduce_values: np.ndarray, memory_usage: List, avoid_saturation: List, max_iterations: int,
87 | min_diff: float, extra_steps: int):
88 | output = np.full((state_vector.size, max_iterations), 2.0)
89 | keep_execution = True
90 | extra_steps_counter = extra_steps
91 | it_counter = max_iterations
92 |
93 | for val_pos in range(state_vector.size):
94 | output[val_pos][0] = state_vector[val_pos]
95 | if avoid_saturation[val_pos]:
96 | state_vector[val_pos] = (2 * state_vector[val_pos]) - 1
97 |
98 | current_step = 1
99 | difference = min_diff
100 | while keep_execution:
101 | it_counter = it_counter - 1
102 | if it_counter <= 0:
103 | keep_execution = False
104 | new_state = np.dot(state_vector, relation_matrix)
105 | for val_pos in range(state_vector.size):
106 | if memory_usage[val_pos]:
107 | new_state[val_pos] = new_state[val_pos] + state_vector[val_pos]
108 | if reduce_values[val_pos] > 0:
109 | new_state[val_pos] = new_state[val_pos] / reduce_values[val_pos]
110 | new_state[val_pos] = __exec_actv_function(functions[val_pos], new_state[val_pos], func_args[val_pos])
111 | output[val_pos][current_step] = new_state[val_pos]
112 |
113 | if avoid_saturation[val_pos]:
114 | new_state[val_pos] = 2 * new_state[val_pos] - 1
115 |
116 | state_vector = new_state
117 | current_step = current_step + 1
118 |
119 | if current_step > 1:
120 | difference = abs(np.sum(output[:, current_step - 1]) - np.sum(output[:, current_step - 2]))
121 | if difference < min_diff:
122 | extra_steps_counter = extra_steps_counter - 1
123 | if extra_steps_counter == 0:
124 | keep_execution = False
125 | else:
126 | extra_steps_counter = extra_steps
127 | return output
128 |
129 |
130 | # activation functions relations
131 |
132 | @njit
133 | def sigmoid(val: float, lambda_val=1.0) -> float:
134 | return 1.0 / (1.0 + exp(-1 * lambda_val * val))
135 |
136 |
137 | @njit
138 | def sigmoid_lambda(x: float, y: float) -> float:
139 | res = -(math.log((1 / y) - 1) / x)
140 | return res
141 |
142 |
143 | @njit
144 | def sigmoid_hip(val: float, lambda_val=2.0) -> float:
145 | # avoiding estimation errors
146 | if (-1 * lambda_val * val) > 500:
147 | return (1.0 - exp(500)) / (1.0 + exp(500))
148 | else:
149 | return (1.0 - exp(-1 * lambda_val * val)) / (1.0 + exp(-1 * lambda_val * val))
150 |
151 |
152 | @njit
153 | def sigmoid_hip_lambda(x: float, y: float) -> float:
154 | res = -(math.log((1 - y) / (1 + y)) / x)
155 | return res
156 |
157 |
158 | @njit
159 | def relm(val: float, lambda_val=1.0) -> float:
160 | if val < 0:
161 | return 0
162 | res = val / lambda_val
163 | if res > 1:
164 | return 1
165 | return res
166 |
167 |
168 | @njit
169 | def saturation(val: float) -> float:
170 | if val < 0:
171 | return 0.0
172 | elif val > 1:
173 | return 1.0
174 | else:
175 | return val
176 |
177 |
178 | @njit
179 | def bistate(val: float) -> float:
180 | if val <= 0.0:
181 | return 0.0
182 | return 1.0
183 |
184 |
185 | @njit
186 | def threestate(val: float) -> float:
187 | if val <= 1.0 / 3.0:
188 | return 0.0
189 | elif val <= 2.0 / 3.0:
190 | return 0.5
191 | return 1.0
192 |
193 |
194 | @njit
195 | def greater_cond_equality(val: float, weight=-1.0) -> float:
196 | if val >= weight:
197 | if val > 1:
198 | return 1
199 | if val < -1:
200 | return -1
201 | return val
202 | return 0
203 |
204 |
205 | @njit
206 | def lower_cond_equality(val: float, weight=1.0) -> float:
207 | if val <= weight:
208 | if val > 1:
209 | return 1
210 | if val < -1:
211 | return -1
212 | return val
213 | return 0
214 |
215 |
216 | @njit
217 | def fuzzy_set(value: float, membership=np.empty(1, dtype=np.float64),
218 | val_list=np.empty(1, dtype=np.float64)) -> float:
219 | # is assumed that the list of values (val_list) is sorted from lowest to greatest and with no repetitions
220 |
221 | negative_activation = False
222 | if 0.0 <= val_list.min() <= 1.0 and 0.0 <= val_list.max() <= 1.0 and value < 0.0:
223 | negative_activation = True
224 | value = abs(value)
225 |
226 | # result positions
227 | prev_pos = 0
228 |
229 | # find nearest values index
230 | index = np.searchsorted(val_list, value)
231 | if index == val_list.size:
232 | index = index - 1
233 | if val_list[index] == value:
234 | if not negative_activation:
235 | return membership[index]
236 | else:
237 | return -1 * (1 - membership[index])
238 | if index == 0:
239 | if val_list[index] > value:
240 | if not negative_activation:
241 | return membership[index]
242 | else:
243 | return -1 * (1 - membership[index])
244 | else:
245 | next_pos = 1
246 | elif index == val_list.size - 1:
247 | if val_list[index] < value:
248 | if not negative_activation:
249 | return membership[index]
250 | else:
251 | return -1 * (1 - membership[index])
252 | else:
253 | prev_pos = index - 1
254 | next_pos = index
255 | else:
256 | if (value - val_list[index]) > 0:
257 | prev_pos = index
258 | next_pos = index + 1
259 | else:
260 | prev_pos = index - 1
261 | next_pos = index
262 |
263 | sign = 1.0
264 | if value != 0:
265 | sign = value / abs(value)
266 | value = abs(value)
267 |
268 | # f(Xi) = (f(Xi-1)*Xi/Xi-1)*Xi-1_Xi_coef + (f(Xi+1)*Xi/Xi+1)*Xi+1_Xi_coef
269 | # inf_estimation = (membership[prev_pos] * value) / float(val_list[prev_pos])
270 | # sup_estimation = (membership[next_pos] * value) / float(val_list[next_pos])
271 | inf_estimation = membership[prev_pos]
272 | sup_estimation = membership[next_pos]
273 | diff = val_list[next_pos] - val_list[prev_pos]
274 | # calc influence coefficents
275 | inf_coef = 1 - ((value - val_list[prev_pos]) / diff)
276 | # 1 - inf_coef
277 | sup_coef = 1 - ((val_list[next_pos] - value) / diff)
278 | # result estimation according to distance between extremes
279 | estimation = sign * ((inf_coef * inf_estimation) + (sup_coef * sup_estimation))
280 |
281 | if not negative_activation:
282 | if estimation > 1:
283 | estimation = 1
284 | if estimation < -1:
285 | estimation = -1
286 | return estimation
287 | return -1 * (1 - estimation)
288 |
289 |
290 | # ensure functions numba compilation
291 | dual_quick_sort(np.array([2, 5, 1]), 0, 2, np.array([2, 5, 1]))
292 | __empt_arr = np.ones(2, np.float64)
293 | __empt_mat = np.ones((2, 2), np.float64)
294 | vectorized_run(__empt_arr, __empt_mat, __empt_arr, List([__empt_arr, __empt_arr]),
295 | __empt_arr, List([True, False]), List([True, False]),
296 | max_iterations=3, min_diff=0.0001, extra_steps=0)
297 |
298 | sigmoid(10, 1.5)
299 | sigmoid_lambda(500, 0.8)
300 | sigmoid_hip(10)
301 | sigmoid_hip_lambda(500, 0.85)
302 | relm(5, 5)
303 | bistate(10)
304 | threestate(10)
305 | saturation(10)
306 | greater_cond_equality(10, 0.5)
307 | fuzzy_set(10, np.array([0.0, 1.0]), np.array([5, 15]))
308 |
309 |
310 | class Activation:
311 | """
312 | Class to map all activation functions that can be used by FCM concepts. The function args structure will be the
313 | next one: val, arg_list
314 | Where:
315 | val: is the value to apply the function
316 | arg_list: is a numpy array that contains the list of arguments values sorted
317 | Note: If some function require more than one argument will be assumed that the values will be sorted according to
318 | the alphabetical sort of arguments names
319 | """
320 |
321 | @staticmethod
322 | def get_function_by_name(func_name: str):
323 | """
324 | Get the function callable object from the function name
325 | Args:
326 | func_name: Activation function name
327 |
328 | Returns: Function callable object if func_name is found, None otherwise
329 |
330 | """
331 | if func_name == "biestate":
332 | return bistate
333 | if func_name == "threestate":
334 | return threestate
335 | if func_name == "saturation":
336 | return saturation
337 | if func_name == "tan_hip":
338 | return sigmoid_hip
339 | if func_name == "sigmoid":
340 | return sigmoid
341 | if func_name == "sigmoid_hip":
342 | return sigmoid_hip
343 | if func_name == "fuzzy":
344 | return fuzzy_set
345 | if func_name == "gceq":
346 | return greater_cond_equality
347 | if func_name == "lceq":
348 | return lower_cond_equality
349 | if func_name == "relm":
350 | return relm
351 | return None
352 |
353 | @staticmethod
354 | def get_const_by_name(func_name: str):
355 | """
356 | Get the function const value from the function name
357 | Args:
358 | func_name: Activation function name
359 |
360 | Returns: Function cont value if func_name is found, None otherwise
361 |
362 | """
363 | if func_name == "biestate":
364 | return FUNC_BISTATE
365 | if func_name == "threestate":
366 | return FUNC_THREESTATE
367 | if func_name == "saturation":
368 | return FUNC_SATURATION
369 | if func_name == "tan_hip":
370 | return FUNC_SIGMOID_HIP
371 | if func_name == "sigmoid":
372 | return FUNC_SIGMOID
373 | if func_name == "sigmoid_hip":
374 | return FUNC_SIGMOID_HIP
375 | if func_name == "relm":
376 | return FUNC_RELM
377 | if func_name == "fuzzy":
378 | return FUNC_FUZZY
379 | if func_name == "gceq":
380 | return FUNC_GCEQ
381 | if func_name == "lceq":
382 | return FUNC_LCEQ
383 | return None
384 |
385 | @staticmethod
386 | def get_function_names() -> set:
387 | """
388 | Get available activation function names
389 | Returns: Set of names
390 |
391 | """
392 | names = set()
393 | names.add("biestate")
394 | names.add("threestate")
395 | names.add("saturation")
396 | names.add("tan_hip")
397 | names.add("sigmoid")
398 | names.add("sigmoid_hip")
399 | names.add("relm")
400 | names.add("gceq")
401 | names.add("lceq")
402 | names.add("proportion")
403 | return names
404 |
405 |
406 | class Excitation:
407 | """
408 | All exitation functions must get a node as parameter
409 | """
410 |
411 | # node: node dict for all functions
412 | @staticmethod
413 | def kosko(node):
414 | """ TeX functions:
415 | not memory: A^{(t+1)}_i = f\left(\sum_{j=1}^N w_{ij}*A^{(t)}_j \right) , i \neq j
416 | use memory: A^{(t+1)}_i = f\left(A^{(t)}_i+\sum_{j=1}^N w_{ij}*A^{(t)}_j \right) , i \neq j
417 | """
418 | neighbors_val = node[NODE_AUX]
419 | node_val = node[NODE_VALUE]
420 | use_memory = node[NODE_USE_MEM]
421 | res = sum(neighbors_val)
422 | if use_memory:
423 | res += node_val
424 | return res
425 |
426 | @staticmethod
427 | def papageorgius(node):
428 | # to avoid saturation
429 | neighbors_val = node[NODE_AUX]
430 | node_val = node[NODE_VALUE]
431 | use_memory = node[NODE_USE_MEM]
432 | res = sum(neighbors_val)
433 | if use_memory:
434 | res += (2 * node_val) - 1
435 | return res
436 |
437 | @staticmethod
438 | def get_by_name(func_name: str):
439 | """
440 | Get the function callable object from the function name
441 | Args:
442 | func_name: Excitation function name
443 |
444 | Returns: Function callable object if func_name is found, None otherwise
445 |
446 | """
447 | if func_name == "KOSKO":
448 | return Excitation.kosko
449 | if func_name == "PAPAGEORGIUS":
450 | return Excitation.papageorgius
451 | return None
452 |
453 | @staticmethod
454 | def get_function_names() -> set:
455 | """
456 | Get available excitation function names
457 | Returns: Set of names
458 |
459 | """
460 | names = set()
461 | names.add("KOSKO")
462 | names.add("PAPAGEORGIUS")
463 | names.add("MEAN")
464 | return names
465 |
466 |
467 | class Decision:
468 | @staticmethod
469 | def last(val_list: list, last_pos=0) -> float:
470 | # return last value
471 | if last_pos > 0:
472 | return val_list[last_pos]
473 | return val_list[-1]
474 |
475 | @staticmethod
476 | def mean(val_list: list, last_pos=0) -> float:
477 | # return average execution value
478 | result = 0
479 | if last_pos <= 0:
480 | last_pos = len(val_list) - 1
481 | for elem_pos in range(last_pos + 1):
482 | result += val_list[elem_pos]
483 | return result / (last_pos + 1)
484 |
485 | @staticmethod
486 | def exited(val_list: list, last_pos=0) -> float:
487 | # return highest execution value
488 | if last_pos >= 0:
489 | res = val_list[:last_pos]
490 | else:
491 | res = val_list
492 | return max(res)
493 |
494 | @staticmethod
495 | def get_by_name(func_name: str):
496 | """
497 | Get the function callable object from the function name
498 | Args:
499 | func_name: Decision function name
500 |
501 | Returns: Function callable object if func_name is found, None otherwise
502 |
503 | """
504 | if func_name == "LAST":
505 | return Decision.last
506 | if func_name == "MEAN":
507 | return Decision.mean
508 | if func_name == "EXITED":
509 | return Decision.exited
510 | return None
511 |
512 | @staticmethod
513 | def get_function_names() -> set:
514 | """
515 | Get available excitation function names
516 | Returns: Set of names
517 |
518 | """
519 | names = set()
520 | names.add("LAST")
521 | names.add("MEAN")
522 | names.add("EXITED")
523 | return names
524 |
525 |
526 | class Fuzzy:
527 | @staticmethod
528 | def defuzzyfication(memb_val, min_scale, norm_val, membership=[], val_list=[]):
529 | """
530 | Estimate possibles neron outputs according to activation.
531 | Args:
532 | memb_val: activation value
533 | min_scale: minimum value in values list for scale data
534 | norm_val: maximum value of scaled data for normalization
535 | membership: membership degree
536 | val_list: result values associated to membership
537 |
538 | Returns: List of possibles results
539 |
540 | """
541 | raise NotImplementedError("Not implemented function")
542 |
543 | @staticmethod
544 | def fuzzyfication(value, min_scale, norm_val, membership=[], val_list=[]):
545 | """
546 | Estimate the neuron activation according to described discrete fuzzy set.
547 | Args:
548 | value: value for estimate activation
549 | min_scale: minimum value in values list for scale data
550 | norm_val: maximum value of scaled data for normalization
551 | membership: membership degree list, each value must belong to domain [-1,1]
552 | val_list: result values associated to membership and len(val_list) = len(membership)
553 |
554 | Returns: Estimated activation
555 |
556 | """
557 | value = (float(value) + min_scale) / norm_val
558 |
559 | # cmp values
560 | prev_value = 1
561 | next_value = -1
562 |
563 | # result positions
564 | prev_pos = 0
565 | next_pos = 0
566 |
567 | # calc result
568 | for elem_pos in range(0, len(val_list)):
569 | if value == val_list[elem_pos]:
570 | return membership[elem_pos]
571 |
572 | diff = float(value) - float(val_list[elem_pos])
573 | if diff > 0:
574 | if diff < prev_value:
575 | prev_value = diff
576 | prev_pos = elem_pos
577 | else:
578 | if diff > next_value:
579 | next_value = diff
580 | next_pos = elem_pos
581 | # minimum value
582 | if prev_value == 1:
583 | return min(membership)
584 | # maximum value
585 | if next_value == -1:
586 | return max(membership)
587 | # estimate value
588 | inf_estimation = membership[prev_pos]
589 | sup_estimation = membership[next_pos]
590 | diff = float(val_list[next_pos]) - float(val_list[prev_pos])
591 | if diff == 0:
592 | return membership[next_pos]
593 | # calc influence coefficents
594 | inf_coef = 1 - ((value - float(val_list[prev_pos])) / diff)
595 | # 1 - inf_coef
596 | sup_coef = 1 - ((float(val_list[next_pos]) - value) / diff)
597 | # result estimation according to distance between extremes
598 | estimation = (inf_coef * inf_estimation) + (sup_coef * sup_estimation)
599 | if estimation > 1:
600 | estimation = 1
601 | if estimation < -1:
602 | estimation = -1
603 | return estimation
604 |
605 |
606 | class Relation:
607 | """
608 | All execution function must have the same params described in supp function, even if not used
609 | """
610 |
611 | # support
612 | @staticmethod
613 | def supp(p_q, p_nq, np_q, np_nq):
614 | return p_q / (p_q + p_nq + np_q + np_nq)
615 |
616 | # confidence
617 | @staticmethod
618 | def conf(p_q, p_nq, np_q, np_nq):
619 | if (p_q + p_nq) != 0:
620 | return p_q / (p_q + p_nq)
621 | return 0
622 |
623 | # lift
624 | @staticmethod
625 | def lift(p_q, p_nq, np_q, np_nq):
626 | return (p_q / (p_q + p_nq)) / ((p_q + np_q) / (p_q + p_nq + np_q + np_nq))
627 |
628 | # red odss ratio
629 | @staticmethod
630 | def rodr(p_q, p_nq, np_q, np_nq):
631 | if (p_nq + np_q) != 0:
632 | return (p_q + np_nq) / (p_nq + np_q)
633 | else:
634 | # zero div behavior
635 | return p_q + np_nq
636 |
637 | # odss ratio
638 | @staticmethod
639 | def odr(p_q, p_nq, np_q, np_nq):
640 | if (p_nq * np_q) != 0:
641 | return (p_q * np_nq) / (p_nq * np_q)
642 | else:
643 | # zero div behavior
644 | return p_q * np_nq
645 |
646 | # positive influence
647 | @staticmethod
648 | def pos_inf(p_q, p_nq, np_q, np_nq):
649 | total = p_q + p_nq + np_q + np_nq
650 | if (p_nq + np_q) != 0:
651 | pos_inf = (p_q / (p_nq + np_q)) / (p_q + p_nq)
652 | else:
653 | # zero div behavior
654 | pos_inf = p_q / (p_q + p_nq)
655 |
656 | if (p_q + np_nq) != 0:
657 | neg_inf = (p_nq / (p_q + np_nq)) / (p_q + p_nq)
658 | else:
659 | # zero div behavior
660 | neg_inf = p_nq / (p_q + p_nq)
661 | if pos_inf > neg_inf:
662 | return pos_inf
663 | if pos_inf < neg_inf:
664 | return -1 * neg_inf
665 | return 0
666 |
667 | @staticmethod
668 | def bayes(p_q, p_nq, np_q, np_nq):
669 | total = p_q + p_nq + np_q + np_nq
670 | prob_p = (p_q + p_nq) / total
671 | prob_q = (p_q + np_q) / total
672 | if p_q > np_q:
673 | return (((p_q - np_q) / total) * prob_q) / prob_p
674 | return 0
675 |
676 | @staticmethod
677 | def simple(p_q, p_nq, np_q, np_nq):
678 | total = p_q + p_nq + np_q + np_nq
679 | if p_q == p_nq:
680 | return 0
681 | if p_q - p_nq != 0:
682 | return ((p_q - p_nq) + (np_nq + np_q)) / total
683 | else:
684 | # zero div behavior
685 | return ((p_q - p_nq) - (np_nq + np_q)) / total
686 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | import setuptools
2 |
3 | with open("README.md", "r") as fh:
4 | long_description = fh.read()
5 |
6 | setuptools.setup(
7 | name='py_fcm',
8 | version='1.0.0',
9 | scripts=[],
10 | author="Jairo Lefebre",
11 | author_email="jairo.lefebre@gmail.com",
12 | description="Fuzzy cognitive maps python library",
13 | long_description=long_description,
14 | long_description_content_type="text/markdown",
15 | url="https://github.com/J41R0/PyFCM",
16 | packages=setuptools.find_packages("py_fcm", exclude=["tests"]),
17 | install_requires=[
18 | 'pandas >= 0.24.2',
19 | 'matplotlib >= 3.1.0',
20 | 'networkx >= 2.3',
21 | 'numpy >= 1.19.1',
22 | 'numba >= 0.51.2',
23 | ],
24 | python_requires='>=3.7',
25 | classifiers=[
26 | "Programming Language :: Python :: 3",
27 | "Operating System :: OS Independent",
28 | ],
29 | )
30 |
--------------------------------------------------------------------------------
/tests/__init__.py:
--------------------------------------------------------------------------------
1 | from py_fcm.utils.__const import *
2 | from py_fcm.utils.functions import Excitation, Activation
3 |
4 |
5 | def create_concept(node_type=TYPE_SIMPLE, is_active=True, use_memory=True,
6 | exitation_function='KOSKO', activ_function=None, **kwargs):
7 | test_concept = {NODE_ACTIVE: is_active, NODE_ARCS: [], NODE_AUX: [1, 1, 1], NODE_VALUE: 1.0}
8 | test_concept[NODE_TYPE] = node_type
9 | test_concept[NODE_EXEC_FUNC] = Excitation.get_by_name(exitation_function)
10 | test_concept[NODE_USE_MEM] = use_memory
11 | test_concept[NODE_ACTV_FUNC] = Activation.get_function_by_name(activ_function)
12 | test_concept[NODE_ACTV_FUNC_ARGS] = kwargs
13 | return test_concept
14 |
--------------------------------------------------------------------------------
/tests/test_estimator.py:
--------------------------------------------------------------------------------
1 | import json
2 | import unittest
3 | import pandas as pd
4 | from py_fcm.learning.association import AssociationBasedFCM
5 |
6 |
7 | class GeneratorTests(unittest.TestCase):
8 | @staticmethod
9 | def gen_fcm():
10 | generator = AssociationBasedFCM()
11 |
12 | test_input = [
13 | ['x', 5, 2.3, 'v1'],
14 | ['y', 7, 4.8, 'v1'],
15 | ['z', 3, 28.01, 'v2'],
16 | ['w', 1, 15.7, 'v2']
17 | ]
18 |
19 | my_ds = pd.DataFrame(test_input, columns=['f1', 'f2', 'f3', 'class'])
20 | generated_fcm = generator.build_fcm(my_ds, target_features=['class'])
21 | return generated_fcm
22 |
23 | def test_association_generator_concepts(self):
24 | expected_json = {"concepts": [{"id": "w___f1", "is_active": True, "type": "SIMPLE", "activation": 0.0},
25 | {"id": "x___f1", "is_active": True, "type": "SIMPLE", "activation": 0.0},
26 | {"id": "y___f1", "is_active": True, "type": "SIMPLE", "activation": 0.0},
27 | {"id": "z___f1", "is_active": True, "type": "SIMPLE", "activation": 0.0},
28 | {"id": "0___f2", "is_active": True, "type": "SIMPLE", "activation": 0.0,
29 | "custom_function": "fuzzy", "activation_dict": {
30 | "membership": [0.14285714285714285, 0.42857142857142855, 0.7142857142857143,
31 | 1.0], "val_list": [1.0, 3.0, 5.0, 7.0]}},
32 | {"id": "0___f3", "is_active": True, "type": "SIMPLE", "activation": 0.0,
33 | "custom_function": "fuzzy", "activation_dict": {
34 | "membership": [0.0821135308818279, 0.17136736879685824, 0.5605141021063905,
35 | 1.0], "val_list": [2.3, 4.8, 15.7, 28.01]}},
36 | {"id": "v1___class", "is_active": True, "type": "DECISION", "activation": 0.0},
37 | {"id": "v2___class", "is_active": True, "type": "DECISION", "activation": 0.0}]
38 | }
39 |
40 | generated_fcm = GeneratorTests.gen_fcm()
41 | json_fcm = json.loads(generated_fcm.to_json())
42 | self.assertEqual(expected_json['concepts'], json_fcm['concepts'])
43 |
44 | def test_association_generator_relations(self):
45 | expected_json = {"relations": [{"origin": "w___f1", "destiny": "x___f1", "weight": -1},
46 | {"origin": "x___f1", "destiny": "w___f1", "weight": -1},
47 | {"origin": "w___f1", "destiny": "y___f1", "weight": -1},
48 | {"origin": "y___f1", "destiny": "w___f1", "weight": -1},
49 | {"origin": "w___f1", "destiny": "z___f1", "weight": -1},
50 | {"origin": "z___f1", "destiny": "w___f1", "weight": -1},
51 | {"origin": "x___f1", "destiny": "y___f1", "weight": -1},
52 | {"origin": "y___f1", "destiny": "x___f1", "weight": -1},
53 | {"origin": "x___f1", "destiny": "z___f1", "weight": -1},
54 | {"origin": "z___f1", "destiny": "x___f1", "weight": -1},
55 | {"origin": "y___f1", "destiny": "z___f1", "weight": -1},
56 | {"origin": "z___f1", "destiny": "y___f1", "weight": -1},
57 | {"origin": "0___f2", "destiny": "w___f1", "weight": 0.4375},
58 | {"origin": "w___f1", "destiny": "0___f2", "weight": 1.0},
59 | {"origin": "0___f2", "destiny": "x___f1", "weight": 0.0625},
60 | {"origin": "x___f1", "destiny": "0___f2", "weight": 0.14285714285714285},
61 | {"origin": "0___f2", "destiny": "y___f1", "weight": 0.1875},
62 | {"origin": "y___f1", "destiny": "0___f2", "weight": 0.42857142857142855},
63 | {"origin": "0___f2", "destiny": "z___f1", "weight": 0.3125},
64 | {"origin": "z___f1", "destiny": "0___f2", "weight": 0.7142857142857143},
65 | {"origin": "0___f3", "destiny": "w___f1", "weight": 0.5512694351505609},
66 | {"origin": "w___f1", "destiny": "0___f3", "weight": 1.0},
67 | {"origin": "0___f3", "destiny": "x___f1", "weight": 0.045266679787443406},
68 | {"origin": "x___f1", "destiny": "0___f3", "weight": 0.0821135308818279},
69 | {"origin": "0___f3", "destiny": "y___f1", "weight": 0.09446959259988191},
70 | {"origin": "y___f1", "destiny": "0___f3", "weight": 0.17136736879685824},
71 | {"origin": "0___f3", "destiny": "z___f1", "weight": 0.30899429246211374},
72 | {"origin": "z___f1", "destiny": "0___f3", "weight": 0.5605141021063905},
73 | {"origin": "0___f3", "destiny": "0___f2", "weight": 0.8189332808502263},
74 | {"origin": "0___f2", "destiny": "0___f3", "weight": 0.6499241342377723},
75 | {"origin": "v1___class", "destiny": "v2___class", "weight": -1},
76 | {"origin": "v2___class", "destiny": "v1___class", "weight": -1},
77 | {"origin": "v1___class", "destiny": "0___f3", "weight": 0.12674044983934307},
78 | {"origin": "0___f3", "destiny": "v1___class", "weight": 0.13973627238732533},
79 | {"origin": "v2___class", "destiny": "0___f3", "weight": 0.7802570510531952},
80 | {"origin": "0___f3", "destiny": "v2___class", "weight": 0.8602637276126747},
81 | {"origin": "v1___class", "destiny": "x___f1", "weight": 0.5},
82 | {"origin": "x___f1", "destiny": "v1___class", "weight": 1.0},
83 | {"origin": "v1___class", "destiny": "y___f1", "weight": 0.5},
84 | {"origin": "y___f1", "destiny": "v1___class", "weight": 1.0},
85 | {"origin": "v2___class", "destiny": "w___f1", "weight": 0.5},
86 | {"origin": "w___f1", "destiny": "v2___class", "weight": 1.0},
87 | {"origin": "v2___class", "destiny": "z___f1", "weight": 0.5},
88 | {"origin": "z___f1", "destiny": "v2___class", "weight": 1.0},
89 | {"origin": "v1___class", "destiny": "0___f2", "weight": 0.2857142857142857},
90 | {"origin": "0___f2", "destiny": "v1___class", "weight": 0.25},
91 | {"origin": "v2___class", "destiny": "0___f2", "weight": 0.8571428571428572},
92 | {"origin": "0___f2", "destiny": "v2___class", "weight": 0.7500000000000001}]
93 | }
94 |
95 | generated_fcm = GeneratorTests.gen_fcm()
96 | json_fcm = json.loads(generated_fcm.to_json())
97 | for elment_pos in range(len(expected_json['relations'])):
98 | self.assertIn(expected_json['relations'][elment_pos], json_fcm['relations'])
99 |
--------------------------------------------------------------------------------
/tests/test_fcm.py:
--------------------------------------------------------------------------------
1 | import json
2 | import unittest
3 | from py_fcm import FuzzyCognitiveMap, TYPE_DECISION, TYPE_FUZZY, TYPE_SIMPLE
4 |
5 |
6 | class FuzzyCognitiveMapTests(unittest.TestCase):
7 | def setUp(self) -> None:
8 | self.fcm = FuzzyCognitiveMap()
9 |
10 | def __init_complex_fcm(self):
11 | fcm = FuzzyCognitiveMap()
12 |
13 | fcm.add_concept('result_1', concept_type=TYPE_DECISION)
14 | fcm.add_concept('result_2', concept_type=TYPE_DECISION)
15 |
16 | fcm.add_concept('input_1')
17 | fcm.init_concept('input_1', 0.5)
18 | fcm.add_concept('input_2')
19 | fcm.init_concept('input_2', 0.2)
20 | fcm.add_concept('input_3')
21 | fcm.init_concept('input_3', 1)
22 | fcm.add_concept('input_4')
23 | fcm.init_concept('input_4', -0.2)
24 | fcm.add_concept('input_5')
25 | fcm.init_concept('input_5', -0.5)
26 |
27 | fcm.add_relation('input_1', 'result_1', 0.5)
28 | fcm.add_relation('input_2', 'result_1', 1)
29 |
30 | fcm.add_relation('input_4', 'result_2', 0.5)
31 | fcm.add_relation('input_5', 'result_2', 1)
32 |
33 | fcm.add_relation('input_1', 'input_4', -0.3)
34 | fcm.add_relation('input_1', 'input_3', 0.7)
35 | fcm.add_relation('input_5', 'input_2', -0.3)
36 | fcm.add_relation('input_5', 'input_3', 0.7)
37 |
38 | fcm.add_relation('result_2', 'input_3', 0.5)
39 | fcm.add_relation('result_1', 'input_3', 0.5)
40 | return fcm
41 |
42 | def test_default_and_to_json(self) -> None:
43 | expected_json = {
44 | "max_iter": 200,
45 | "decision_function": "MEAN",
46 | "activation_function": "sigmoid_hip",
47 | "memory_influence": False,
48 | "stability_diff": 0.001,
49 | "stop_at_stabilize": True,
50 | "extra_steps": 5,
51 | "weight": 1,
52 | "concepts": [],
53 | "relations": []
54 | }
55 | json_fcm = json.loads(self.fcm.to_json())
56 | self.assertEqual(expected_json, json_fcm)
57 |
58 | def test_set_map_fuctions(self) -> None:
59 | expected_json = {
60 | "max_iter": 200,
61 | "decision_function": "LAST",
62 | "activation_function": "sigmoid",
63 | 'activation_function_args': {'lambda_val': 10},
64 | "memory_influence": False,
65 | "stability_diff": 0.001,
66 | "stop_at_stabilize": True,
67 | "extra_steps": 5,
68 | "weight": 1,
69 | "concepts": [],
70 | "relations": []
71 | }
72 | self.fcm.set_map_decision_function('LAST')
73 | self.fcm.set_map_activation_function('sigmoid', lambda_val=10)
74 | json_fcm = json.loads(self.fcm.to_json())
75 | self.assertEqual(expected_json, json_fcm)
76 |
77 | def test_concept_addition_default(self) -> None:
78 | expected_json = {
79 | "concepts": [{'id': 'test', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.0}]
80 | }
81 | self.fcm.add_concept('test')
82 | json_fcm = json.loads(self.fcm.to_json())
83 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
84 |
85 | def test_concept_init_and_get_value(self) -> None:
86 | self.fcm.add_concept('test')
87 | self.fcm.init_concept('test', 0.5)
88 | self.assertEqual(0.5, self.fcm.get_concept_value('test'))
89 |
90 | def test_concept_redefinition(self) -> None:
91 | expected_json = {
92 | "concepts": [{'id': 'test', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.0}]
93 | }
94 | self.fcm.add_concept('test')
95 | json_fcm = json.loads(self.fcm.to_json())
96 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
97 | expected_json["concepts"][0]['type'] = 'DECISION'
98 |
99 | self.fcm.add_concept('test', concept_type=TYPE_DECISION)
100 | json_fcm = json.loads(self.fcm.to_json())
101 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
102 |
103 | def test_concept_addition_custom_values(self) -> None:
104 | expected_json = {
105 | "concepts": [{'id': 'test', 'is_active': False, 'type': 'DECISION', 'activation': 0.0, 'use_memory': True,
106 | 'custom_function': 'gceq', 'custom_function_args': {'weight': 0.3}}]
107 | }
108 | self.fcm.add_concept('test', is_active=False, concept_type=TYPE_DECISION, use_memory=True,
109 | excitation_function='PAPAGEORGIUS', activation_function='gceq', weight=0.3)
110 | json_fcm = json.loads(self.fcm.to_json())
111 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
112 |
113 | def test_concept_addition_with_default_definition(self) -> None:
114 | expected_json = {
115 | "concepts": [{'id': 'test', 'is_active': True, 'type': 'DECISION', 'activation': 0.0, 'use_memory': True,
116 | 'custom_function': 'gceq', 'custom_function_args': {'weight': 0.3}}]
117 | }
118 | self.fcm.set_default_concept_properties(concept_type=TYPE_DECISION, use_memory=True,
119 | excitation_function='PAPAGEORGIUS', activation_function='gceq',
120 | weight=0.3)
121 | self.fcm.add_concept('test')
122 | json_fcm = json.loads(self.fcm.to_json())
123 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
124 |
125 | def test_concept_property_update(self) -> None:
126 | expected_json = {
127 | "concepts": [{'id': 'test', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.0}]
128 | }
129 | self.fcm.add_concept('test')
130 | json_fcm = json.loads(self.fcm.to_json())
131 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
132 | self.fcm.set_concept_properties('test', is_active=False, concept_type=TYPE_DECISION, use_memory=True,
133 | excitation_function='PAPAGEORGIUS', activation_function='gceq', weight=0.3)
134 | expected_json = {
135 | "concepts": [{'id': 'test', 'is_active': False, 'type': 'DECISION', 'activation': 0.0, 'use_memory': True,
136 | 'custom_function': 'gceq', 'custom_function_args': {'weight': 0.3}}]
137 | }
138 | json_fcm = json.loads(self.fcm.to_json())
139 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
140 |
141 | def test_relation_addition(self) -> None:
142 | expected_json = {
143 | "concepts": [{'id': 'test', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.0},
144 | {'id': 'test_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.0}],
145 | "relations": [{'origin': 'test', 'destiny': 'test_1', 'weight': 0.5}]
146 | }
147 | self.fcm.add_concept('test')
148 | self.fcm.add_concept('test_1')
149 | self.fcm.add_relation('test', 'test_1', 0.5)
150 | json_fcm = json.loads(self.fcm.to_json())
151 | self.assertEqual(expected_json["relations"], json_fcm["relations"])
152 |
153 | def test_clear_all(self) -> None:
154 | expected_json = {
155 | "max_iter": 200,
156 | "decision_function": "MEAN",
157 | "activation_function": "sigmoid_hip",
158 | "memory_influence": False,
159 | "stability_diff": 0.001,
160 | "stop_at_stabilize": True,
161 | "extra_steps": 5,
162 | "weight": 1,
163 | "concepts": [],
164 | "relations": []
165 | }
166 | self.fcm.add_concept('test')
167 | self.fcm.add_concept('test_1')
168 | self.fcm.add_relation('test', 'test_1', 0.5)
169 | self.fcm.clear_all()
170 | json_fcm = json.loads(self.fcm.to_json())
171 | self.assertEqual(expected_json, json_fcm)
172 |
173 | def test_inference_default(self):
174 | # sigmoid_hip
175 | expected_json = {
176 | 'concepts': [{'id': 'result_1', 'is_active': True, 'type': 'DECISION', 'activation': 0.056969360711939906},
177 | {'id': 'result_2', 'is_active': True, 'type': 'DECISION', 'activation': -0.06113548755394814},
178 | {'id': 'input_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.05},
179 | {'id': 'input_2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.034888503362331805},
180 | {'id': 'input_3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.09792163723261006},
181 | {'id': 'input_4', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.034888503362331805},
182 | {'id': 'input_5', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.05}]
183 | }
184 |
185 | fcm = self.__init_complex_fcm()
186 | fcm.run_inference()
187 | json_fcm = json.loads(fcm.to_json())
188 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
189 | fcm.debug = False
190 | fcm.run_inference()
191 | json_fcm = json.loads(fcm.to_json())
192 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
193 |
194 | def test_is_stable(self):
195 | fcm = self.__init_complex_fcm()
196 | fcm.run_inference()
197 | self.assertEqual(True, fcm.is_stable())
198 | fcm.debug = False
199 | fcm.run_inference()
200 | self.assertEqual(True, fcm.is_stable())
201 |
202 | def test_get_final_state_default(self):
203 | fcm = self.__init_complex_fcm()
204 | fcm.run_inference()
205 | expected_result = {'result_1': 0.056969360711939906, 'result_2': -0.06113548755394814}
206 | self.assertEqual(expected_result, fcm.get_final_state())
207 | fcm.debug = False
208 | fcm.run_inference()
209 | self.assertEqual(expected_result, fcm.get_final_state())
210 |
211 | def test_search_concept_final_state_arg(self):
212 | fcm = self.__init_complex_fcm()
213 | fcm.run_inference()
214 | expected_result = {'result_1': 0.056969360711939906}
215 | self.assertEqual(expected_result, fcm.get_final_state(names=['result_1']))
216 | fcm.debug = False
217 | fcm.run_inference()
218 | self.assertEqual(expected_result, fcm.get_final_state(names=['result_1']))
219 |
220 | def test_get_final_state_any(self):
221 | fcm = self.__init_complex_fcm()
222 | fcm.run_inference()
223 | expected_result = {'result_1': 0.056969360711939906, 'result_2': -0.06113548755394814,
224 | 'input_1': 0.05, 'input_2': 0.034888503362331805,
225 | 'input_3': 0.09792163723261006, 'input_4': -0.034888503362331805,
226 | 'input_5': -0.05}
227 | self.assertEqual(expected_result, fcm.get_final_state("any"))
228 | fcm.debug = False
229 | fcm.run_inference()
230 | self.assertEqual(expected_result, fcm.get_final_state("any"))
231 |
232 | def test_get_final_state_custom_type(self):
233 | fcm = self.__init_complex_fcm()
234 | fcm.run_inference()
235 | expected_result = {'result_1': 0.056969360711939906, 'result_2': -0.06113548755394814}
236 | self.assertEqual(expected_result, fcm.get_final_state(TYPE_DECISION))
237 | fcm.debug = False
238 | fcm.run_inference()
239 | self.assertEqual(expected_result, fcm.get_final_state(TYPE_DECISION))
240 |
241 | def test_reset_execution(self):
242 | fcm = self.__init_complex_fcm()
243 | fcm.run_inference()
244 | fcm.reset_execution()
245 | expected_result = {'input_1': 0.5}
246 | self.assertEqual(expected_result, fcm.get_final_state(names=['input_1']))
247 |
248 | def test_clear_execution(self):
249 | fcm = self.__init_complex_fcm()
250 | fcm.run_inference()
251 | fcm.clear_execution()
252 | expected_result = {'input_1': 0.0}
253 | self.assertEqual(expected_result, fcm.get_final_state(names=['input_1']))
254 |
255 | def test_inference_sigmoid(self):
256 | expected_json = {
257 | 'concepts': [{'id': 'result_1', 'is_active': True, 'type': 'DECISION', 'activation': 0.5994700184028905},
258 | {'id': 'result_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.5755041378442186},
259 | {'id': 'input_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.5},
260 | {'id': 'input_2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.44379910825937535},
261 | {'id': 'input_3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.7851790048472618},
262 | {'id': 'input_4', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.3963131391906254},
263 | {'id': 'input_5', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.4}]
264 | }
265 |
266 | fcm = self.__init_complex_fcm()
267 | fcm.set_map_activation_function('sigmoid')
268 | fcm.run_inference()
269 | json_fcm = json.loads(fcm.to_json())
270 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
271 | fcm.debug = False
272 | fcm.run_inference()
273 | json_fcm = json.loads(fcm.to_json())
274 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
275 |
276 | def test_inference_biestate(self):
277 | expected_json = {
278 | 'concepts': [{'id': 'result_1', 'is_active': True, 'type': 'DECISION', 'activation': 0.2},
279 | {'id': 'result_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.0},
280 | {'id': 'input_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.05},
281 | {'id': 'input_2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.12},
282 | {'id': 'input_3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.3},
283 | {'id': 'input_4', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.02},
284 | {'id': 'input_5', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.05}]
285 |
286 | }
287 |
288 | fcm = self.__init_complex_fcm()
289 | fcm.set_map_activation_function('biestate')
290 | fcm.run_inference()
291 | json_fcm = json.loads(fcm.to_json())
292 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
293 | fcm.debug = False
294 | fcm.run_inference()
295 | json_fcm = json.loads(fcm.to_json())
296 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
297 |
298 | def test_inference_threestate(self):
299 | expected_json = {
300 | 'concepts': [{'id': 'result_1', 'is_active': True, 'type': 'DECISION', 'activation': 0.0625},
301 | {'id': 'result_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.0},
302 | {'id': 'input_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.0625},
303 | {'id': 'input_2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.025},
304 | {'id': 'input_3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.125},
305 | {'id': 'input_4', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.025},
306 | {'id': 'input_5', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.0625}]
307 | }
308 |
309 | fcm = self.__init_complex_fcm()
310 | fcm.set_map_activation_function('threestate')
311 | fcm.run_inference()
312 | json_fcm = json.loads(fcm.to_json())
313 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
314 | fcm.debug = False
315 | fcm.run_inference()
316 | json_fcm = json.loads(fcm.to_json())
317 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
318 |
319 | def test_inference_saturation(self):
320 | expected_json = {
321 | 'concepts': [{'id': 'result_1', 'is_active': True, 'type': 'DECISION', 'activation': 0.06},
322 | {'id': 'result_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.0},
323 | {'id': 'input_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.05},
324 | {'id': 'input_2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.034999999999999996},
325 | {'id': 'input_3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.13},
326 | {'id': 'input_4', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.02},
327 | {'id': 'input_5', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.05}]
328 | }
329 |
330 | fcm = self.__init_complex_fcm()
331 | fcm.set_map_activation_function('saturation')
332 | fcm.run_inference()
333 | json_fcm = json.loads(fcm.to_json())
334 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
335 | fcm.debug = False
336 | fcm.run_inference()
337 | json_fcm = json.loads(fcm.to_json())
338 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
339 |
340 | def test_inference_gceq(self):
341 | expected_json = {
342 | 'concepts': [{'id': 'result_1', 'is_active': True, 'type': 'DECISION', 'activation': 0.06},
343 | {'id': 'result_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.0},
344 | {'id': 'input_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.05},
345 | {'id': 'input_2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.034999999999999996},
346 | {'id': 'input_3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.13},
347 | {'id': 'input_4', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.02},
348 | {'id': 'input_5', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.05}]
349 | }
350 |
351 | fcm = self.__init_complex_fcm()
352 | fcm.set_map_activation_function('gceq', weight=0.0001)
353 | fcm.run_inference()
354 | json_fcm = json.loads(fcm.to_json())
355 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
356 | fcm.debug = False
357 | fcm.run_inference()
358 | json_fcm = json.loads(fcm.to_json())
359 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
360 |
361 | def test_inference_lceq(self):
362 | expected_json = {
363 | 'concepts': [{'id': 'result_1', 'is_active': True, 'type': 'DECISION', 'activation': 0.0},
364 | {'id': 'result_2', 'is_active': True, 'type': 'DECISION', 'activation': -0.06749999999999999},
365 | {'id': 'input_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.05},
366 | {'id': 'input_2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.02},
367 | {'id': 'input_3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.06625},
368 | {'id': 'input_4', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.034999999999999996},
369 | {'id': 'input_5', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.05}]
370 | }
371 |
372 | fcm = self.__init_complex_fcm()
373 | fcm.set_map_activation_function('lceq', weight=0.0001)
374 | fcm.run_inference()
375 | json_fcm = json.loads(fcm.to_json())
376 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
377 | fcm.debug = False
378 | fcm.run_inference()
379 | json_fcm = json.loads(fcm.to_json())
380 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
381 |
382 | def test_inference_fuzzy(self):
383 | expected_json = {
384 | 'concepts': [{'id': 'r1', 'is_active': True, 'type': 'DECISION', 'activation': 0.4687996143920417},
385 | {'id': 'r2', 'is_active': True, 'type': 'DECISION', 'activation': 0.4625401367231719},
386 | {'id': 'c1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.3,
387 | 'custom_function': 'threestate'},
388 | {'id': 'c2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.3,
389 | 'custom_function': 'threestate'},
390 | {'id': 'c3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.3,
391 | 'custom_function': 'fuzzy', 'activation_dict': {'membership': [0.25, 0.5, 1.0, 0.5, 0.25],
392 | 'val_list': [1.0, 2.0, 3.0, 4.0, 5.0]}}]
393 | }
394 |
395 | fcm = FuzzyCognitiveMap(activation_function='sigmoid')
396 |
397 | fcm.add_concept('r1', TYPE_DECISION)
398 | fcm.add_concept('r2', TYPE_DECISION)
399 | fcm.add_concept('c1', TYPE_SIMPLE, activation_function="threestate")
400 | fcm.add_concept('c2', TYPE_SIMPLE, activation_function="threestate")
401 | fcm.add_concept('c3', TYPE_FUZZY, activation_dict={
402 | 'membership': [0.25, 0.5, 1.0, 0.5, 0.25],
403 | 'val_list': [1, 2, 3, 4, 5]
404 | })
405 |
406 | fcm.add_relation('c3', 'r1', 0.5)
407 | fcm.add_relation('c3', 'r2', 0.5)
408 |
409 | fcm.add_relation('r1', 'c1', 1.0)
410 | fcm.add_relation('r2', 'c2', 1.0)
411 |
412 | fcm.add_relation('c1', 'c3', 0.7)
413 | fcm.add_relation('c1', 'r2', -0.3)
414 |
415 | fcm.add_relation('c2', 'c3', 0.8)
416 | fcm.add_relation('c2', 'r1', -0.2)
417 |
418 | fcm.init_concept('c1', -1.0)
419 | fcm.init_concept('c2', -1.0)
420 |
421 | fcm.run_inference()
422 |
423 | json_fcm = json.loads(fcm.to_json())
424 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
425 | fcm.debug = False
426 | fcm.run_inference()
427 | json_fcm = json.loads(fcm.to_json())
428 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
429 |
430 | def test_inference_several_functions(self):
431 | expected_json = {
432 | 'concepts': [
433 | {'id': 'result_1', 'is_active': True, 'type': 'DECISION', 'activation': 0.0, 'use_memory': True,
434 | 'custom_function': 'gceq', 'custom_function_args': {'weight': 0.003}},
435 | {'id': 'result_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.00012648385157387873,
436 | 'use_memory': True, 'custom_function': 'sigmoid', 'custom_function_args': {'lambda_val': 10}},
437 | {'id': 'input_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.07375, 'use_memory': True,
438 | 'custom_function': 'threestate'},
439 | {'id': 'input_2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.04361062920291475},
440 | {'id': 'input_3', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.6816478125020062},
441 | {'id': 'input_4', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.18674101269496457},
442 | {'id': 'input_5', 'is_active': True, 'type': 'SIMPLE', 'activation': -0.0625}]
443 | }
444 |
445 | fcm = self.__init_complex_fcm()
446 | fcm.add_concept('result_1', concept_type=TYPE_DECISION, use_memory=True,
447 | excitation_function='PAPAGEORGIUS', activation_function='gceq', weight=0.003)
448 |
449 | fcm.add_concept('result_2', concept_type=TYPE_DECISION, use_memory=True,
450 | excitation_function='PAPAGEORGIUS', activation_function='sigmoid', lambda_val=10)
451 |
452 | fcm.add_concept('input_1', use_memory=True, excitation_function='PAPAGEORGIUS',
453 | activation_function='threestate')
454 | fcm.init_concept('input_1', 0.59)
455 | fcm.run_inference()
456 | json_fcm = json.loads(fcm.to_json())
457 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
458 | fcm.debug = False
459 | fcm.run_inference()
460 | json_fcm = json.loads(fcm.to_json())
461 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"])
462 |
--------------------------------------------------------------------------------
/tests/test_functions.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | from tests import create_concept
4 | from py_fcm.utils.functions import *
5 |
6 |
7 | class ExitationFunctionsTests(unittest.TestCase):
8 | def test_kosko(self):
9 | test_concept = create_concept("test", exitation_function='KOSKO')
10 | res = test_concept[NODE_EXEC_FUNC](test_concept)
11 | self.assertEqual(4, res)
12 | test_concept = create_concept("test", exitation_function='KOSKO', use_memory=False)
13 | res = test_concept[NODE_EXEC_FUNC](test_concept)
14 | self.assertEqual(3, res)
15 |
16 | def test_papageorgius(self):
17 | test_concept = create_concept("test", exitation_function='PAPAGEORGIUS')
18 | res = test_concept[NODE_EXEC_FUNC](test_concept)
19 | self.assertEqual(4, res)
20 | test_concept = create_concept("test", exitation_function='PAPAGEORGIUS', use_memory=False)
21 | res = test_concept[NODE_EXEC_FUNC](test_concept)
22 | self.assertEqual(3, res)
23 |
24 |
25 | class ActivationFunctionsTests(unittest.TestCase):
26 | def test_biestate(self):
27 | test_concept = create_concept(activ_function='biestate')
28 | res = test_concept[NODE_ACTV_FUNC](0.5)
29 | self.assertEqual(1.0, res)
30 | res = test_concept[NODE_ACTV_FUNC](-0.5)
31 | self.assertEqual(0.0, res)
32 |
33 | def test_threestate(self):
34 | test_concept = create_concept(activ_function='threestate')
35 | res = test_concept[NODE_ACTV_FUNC](0.30)
36 | self.assertEqual(0.0, res)
37 | res = test_concept[NODE_ACTV_FUNC](0.60)
38 | self.assertEqual(0.5, res)
39 | res = test_concept[NODE_ACTV_FUNC](0.75)
40 | self.assertEqual(1.0, res)
41 |
42 | def test_saturation(self):
43 | test_concept = create_concept(activ_function='saturation')
44 | res = test_concept[NODE_ACTV_FUNC](-60)
45 | self.assertEqual(0.0, res)
46 | res = test_concept[NODE_ACTV_FUNC](75)
47 | self.assertEqual(1.0, res)
48 |
49 | def test_sigmoid(self):
50 | test_concept = create_concept(activ_function='sigmoid')
51 | res = test_concept[NODE_ACTV_FUNC](1)
52 | self.assertEqual(0.7310585786300049, res)
53 | res = test_concept[NODE_ACTV_FUNC](-1)
54 | self.assertEqual(0.2689414213699951, res)
55 | res = test_concept[NODE_ACTV_FUNC](-1, 50)
56 | self.assertEqual(1.928749847963918e-22, res)
57 |
58 | def test_sigmoid_and_tan_hip(self):
59 | test_concept = create_concept(activ_function='sigmoid_hip')
60 | res = test_concept[NODE_ACTV_FUNC](1)
61 | self.assertEqual(0.7615941559557649, res)
62 | res = test_concept[NODE_ACTV_FUNC](-1)
63 | self.assertEqual(-0.7615941559557649, res)
64 | test_concept = create_concept("test", activ_function='tan_hip')
65 | res = test_concept[NODE_ACTV_FUNC](1)
66 | self.assertEqual(0.7615941559557649, res)
67 | res = test_concept[NODE_ACTV_FUNC](-1)
68 | self.assertEqual(-0.7615941559557649, res)
69 |
70 | def test_gceq(self):
71 | test_concept = create_concept(activ_function='gceq')
72 | res = test_concept[NODE_ACTV_FUNC](0.7, 0.5)
73 | self.assertEqual(0.7, res)
74 | res = test_concept[NODE_ACTV_FUNC](7, 0.5)
75 | self.assertEqual(1.0, res)
76 | res = test_concept[NODE_ACTV_FUNC](0.3, 0.5)
77 | self.assertEqual(0.0, res)
78 | res = test_concept[NODE_ACTV_FUNC](-7, -3.5)
79 | self.assertEqual(0.0, res)
80 | res = test_concept[NODE_ACTV_FUNC](-2, -3.5)
81 | self.assertEqual(-1.0, res)
82 |
83 | def test_lceq(self):
84 | test_concept = create_concept(activ_function='lceq')
85 | res = test_concept[NODE_ACTV_FUNC](0.7, 0.5)
86 | self.assertEqual(0.0, res)
87 | res = test_concept[NODE_ACTV_FUNC](7, 0.5)
88 | self.assertEqual(0.0, res)
89 | res = test_concept[NODE_ACTV_FUNC](0.3, 0.5)
90 | self.assertEqual(0.3, res)
91 | res = test_concept[NODE_ACTV_FUNC](-7, -3.5)
92 | self.assertEqual(-1.0, res)
93 | res = test_concept[NODE_ACTV_FUNC](-2, -3.5)
94 | self.assertEqual(0.0, res)
95 |
96 | def test_fuzzy(self):
97 | test_concept = create_concept(activ_function='fuzzy')
98 | res = test_concept[NODE_ACTV_FUNC](10, np.array([0.0, 1.0]), np.array([5, 15]))
99 | self.assertEqual(0.5, res)
100 | res = test_concept[NODE_ACTV_FUNC](4, np.array([0.0, 1.0]), np.array([5, 15]))
101 | self.assertEqual(0.0, res)
102 | res = test_concept[NODE_ACTV_FUNC](16, np.array([0.0, 1.0]), np.array([5, 15]))
103 | self.assertEqual(1.0, res)
104 | res = test_concept[NODE_ACTV_FUNC](7, np.array([0.0, 0.5, 1.0]), np.array([5, 15, 20]))
105 | self.assertEqual(0.09999999999999998, res)
106 | res = test_concept[NODE_ACTV_FUNC](16, np.array([0.0, 0.5, 1.0]), np.array([5, 15, 20]))
107 | self.assertEqual(0.6, res)
108 | res = test_concept[NODE_ACTV_FUNC](-1.0, np.array([0.25, 0.5, 1.0, 0.5, 0.25]),
109 | np.array([0.2, 0.4, 0.6, 0.8, 1.0]))
110 | self.assertEqual(-0.75, res)
111 | res = test_concept[NODE_ACTV_FUNC](-0.5, np.array([0.25, 0.5, 1.0, 0.5, 0.25]),
112 | np.array([0.2, 0.4, 0.6, 0.8, 1.0]))
113 | self.assertEqual(-0.25, res)
114 |
115 |
116 | class RelationFunctionsTests(unittest.TestCase):
117 | def test_supp(self):
118 | res = Relation.supp(1, 2, 2, 1)
119 | self.assertEqual(0.16666666666666666, res)
120 |
121 | def test_conf(self):
122 | res = Relation.conf(1, 1, 1, 1)
123 | self.assertEqual(0.5, res)
124 |
125 | def test_lift(self):
126 | res = Relation.lift(1, 2, 2, 1)
127 | self.assertEqual(0.6666666666666666, res)
128 |
129 | def test_odr(self):
130 | res = Relation.odr(1, 2, 2, 1)
131 | self.assertEqual(0.25, res)
132 |
133 | def test_rodr(self):
134 | res = Relation.rodr(1, 2, 2, 1)
135 | self.assertEqual(0.5, res)
136 |
137 | def test_pos_inf(self):
138 | res = Relation.pos_inf(1, 2, 2, 1)
139 | self.assertEqual(-0.3333333333333333, res)
140 |
141 | def test_simple(self):
142 | res = Relation.simple(1, 2, 2, 1)
143 | self.assertEqual(0.3333333333333333, res)
144 |
145 |
146 | class OtherFucntionsTest(unittest.TestCase):
147 | def test_dual_quick_sort(self):
148 | array = np.array([2, 6, 1, 5])
149 | mirror = np.array([2, 6, 1, 5])
150 | dual_quick_sort(array, 0, len(array) - 1, mirror)
151 | for pos in range(len(array)):
152 | self.assertEqual(array[pos], mirror[pos])
153 |
--------------------------------------------------------------------------------
/tests/test_generator.py:
--------------------------------------------------------------------------------
1 | import json
2 | import unittest
3 | import pandas as pd
4 | from py_fcm.learning.association import AssociationBasedFCM
5 |
6 |
7 | class GeneratorTests(unittest.TestCase):
8 | @staticmethod
9 | def gen_fcm():
10 | generator = AssociationBasedFCM()
11 |
12 | test_input = [
13 | ['x', 5, 2.3, 'v1'],
14 | ['y', 7, 4.8, 'v1'],
15 | ['z', 3, 28.01, 'v2'],
16 | ['w', 1, 15.7, 'v2']
17 | ]
18 |
19 | my_ds = pd.DataFrame(test_input, columns=['f1', 'f2', 'f3', 'class'])
20 | generated_fcm = generator.build_fcm(my_ds, target_features=['class'])
21 | return generated_fcm
22 |
23 | def test_association_generator_concepts(self):
24 | expected_json = {"concepts": [{"id": "w___f1", "is_active": True, "type": "SIMPLE", "activation": 0.0},
25 | {"id": "x___f1", "is_active": True, "type": "SIMPLE", "activation": 0.0},
26 | {"id": "y___f1", "is_active": True, "type": "SIMPLE", "activation": 0.0},
27 | {"id": "z___f1", "is_active": True, "type": "SIMPLE", "activation": 0.0},
28 | {"id": "0___f2", "is_active": True, "type": "SIMPLE", "activation": 0.0,
29 | "custom_function": "fuzzy", "activation_dict": {
30 | "membership": [0.14285714285714285, 0.42857142857142855, 0.7142857142857143,
31 | 1.0], "val_list": [1.0, 3.0, 5.0, 7.0]}},
32 | {"id": "0___f3", "is_active": True, "type": "SIMPLE", "activation": 0.0,
33 | "custom_function": "fuzzy", "activation_dict": {
34 | "membership": [0.0821135308818279, 0.17136736879685824, 0.5605141021063905,
35 | 1.0], "val_list": [2.3, 4.8, 15.7, 28.01]}},
36 | {"id": "v1___class", "is_active": True, "type": "DECISION", "activation": 0.0},
37 | {"id": "v2___class", "is_active": True, "type": "DECISION", "activation": 0.0}]
38 | }
39 |
40 | generated_fcm = GeneratorTests.gen_fcm()
41 | json_fcm = json.loads(generated_fcm.to_json())
42 | self.assertEqual(expected_json['concepts'], json_fcm['concepts'])
43 |
44 | def test_association_generator_relations(self):
45 | expected_json = {"relations": [{"origin": "w___f1", "destiny": "x___f1", "weight": -1},
46 | {"origin": "x___f1", "destiny": "w___f1", "weight": -1},
47 | {"origin": "w___f1", "destiny": "y___f1", "weight": -1},
48 | {"origin": "y___f1", "destiny": "w___f1", "weight": -1},
49 | {"origin": "w___f1", "destiny": "z___f1", "weight": -1},
50 | {"origin": "z___f1", "destiny": "w___f1", "weight": -1},
51 | {"origin": "x___f1", "destiny": "y___f1", "weight": -1},
52 | {"origin": "y___f1", "destiny": "x___f1", "weight": -1},
53 | {"origin": "x___f1", "destiny": "z___f1", "weight": -1},
54 | {"origin": "z___f1", "destiny": "x___f1", "weight": -1},
55 | {"origin": "y___f1", "destiny": "z___f1", "weight": -1},
56 | {"origin": "z___f1", "destiny": "y___f1", "weight": -1},
57 | {"origin": "0___f2", "destiny": "w___f1", "weight": 0.4375},
58 | {"origin": "w___f1", "destiny": "0___f2", "weight": 1.0},
59 | {"origin": "0___f2", "destiny": "x___f1", "weight": 0.0625},
60 | {"origin": "x___f1", "destiny": "0___f2", "weight": 0.14285714285714285},
61 | {"origin": "0___f2", "destiny": "y___f1", "weight": 0.1875},
62 | {"origin": "y___f1", "destiny": "0___f2", "weight": 0.42857142857142855},
63 | {"origin": "0___f2", "destiny": "z___f1", "weight": 0.3125},
64 | {"origin": "z___f1", "destiny": "0___f2", "weight": 0.7142857142857143},
65 | {"origin": "0___f3", "destiny": "w___f1", "weight": 0.5512694351505609},
66 | {"origin": "w___f1", "destiny": "0___f3", "weight": 1.0},
67 | {"origin": "0___f3", "destiny": "x___f1", "weight": 0.045266679787443406},
68 | {"origin": "x___f1", "destiny": "0___f3", "weight": 0.0821135308818279},
69 | {"origin": "0___f3", "destiny": "y___f1", "weight": 0.09446959259988191},
70 | {"origin": "y___f1", "destiny": "0___f3", "weight": 0.17136736879685824},
71 | {"origin": "0___f3", "destiny": "z___f1", "weight": 0.30899429246211374},
72 | {"origin": "z___f1", "destiny": "0___f3", "weight": 0.5605141021063905},
73 | {"origin": "0___f3", "destiny": "0___f2", "weight": 0.8189332808502263},
74 | {"origin": "0___f2", "destiny": "0___f3", "weight": 0.6499241342377723},
75 | {"origin": "v1___class", "destiny": "v2___class", "weight": -1},
76 | {"origin": "v2___class", "destiny": "v1___class", "weight": -1},
77 | {"origin": "v1___class", "destiny": "0___f3", "weight": 0.12674044983934307},
78 | {"origin": "0___f3", "destiny": "v1___class", "weight": 0.13973627238732533},
79 | {"origin": "v2___class", "destiny": "0___f3", "weight": 0.7802570510531952},
80 | {"origin": "0___f3", "destiny": "v2___class", "weight": 0.8602637276126747},
81 | {"origin": "v1___class", "destiny": "x___f1", "weight": 0.5},
82 | {"origin": "x___f1", "destiny": "v1___class", "weight": 1.0},
83 | {"origin": "v1___class", "destiny": "y___f1", "weight": 0.5},
84 | {"origin": "y___f1", "destiny": "v1___class", "weight": 1.0},
85 | {"origin": "v2___class", "destiny": "w___f1", "weight": 0.5},
86 | {"origin": "w___f1", "destiny": "v2___class", "weight": 1.0},
87 | {"origin": "v2___class", "destiny": "z___f1", "weight": 0.5},
88 | {"origin": "z___f1", "destiny": "v2___class", "weight": 1.0},
89 | {"origin": "v1___class", "destiny": "0___f2", "weight": 0.2857142857142857},
90 | {"origin": "0___f2", "destiny": "v1___class", "weight": 0.25},
91 | {"origin": "v2___class", "destiny": "0___f2", "weight": 0.8571428571428572},
92 | {"origin": "0___f2", "destiny": "v2___class", "weight": 0.7500000000000001}]
93 | }
94 |
95 | generated_fcm = GeneratorTests.gen_fcm()
96 | json_fcm = json.loads(generated_fcm.to_json())
97 | for elment_pos in range(len(expected_json['relations'])):
98 | self.assertIn(expected_json['relations'][elment_pos], json_fcm['relations'])
99 |
--------------------------------------------------------------------------------
/tests/test_public_library_functions.py:
--------------------------------------------------------------------------------
1 | import unittest
2 | import json
3 |
4 | from py_fcm import join_maps, from_json
5 |
6 |
7 | class FromJsonTests(unittest.TestCase):
8 | def setUp(self) -> None:
9 | default_fcm = """
10 | {
11 | "max_iter": 500,
12 | "activation_function": "sigmoid",
13 | "activation_function_args": {"lambda_val":1},
14 | "memory_influence": false,
15 | "decision_function": "LAST",
16 | "concepts" :
17 | [
18 | {"id": "concept_1", "type": "SIMPLE", "activation": 0.5},
19 | {"id": "concept_2", "type": "DECISION", "custom_function": "gceq", "custom_function_args": {"weight":0.3}},
20 | {"id": "concept_3", "type": "SIMPLE", "memory_influence":true },
21 | {"id": "concept_4", "type": "SIMPLE", "custom_function": "saturation", "activation": 0.3}
22 | ],
23 | "relations":
24 | [
25 | {"origin": "concept_4", "destiny": "concept_2", "weight": -0.1},
26 | {"origin": "concept_1", "destiny": "concept_3", "weight": 0.59},
27 | {"origin": "concept_3", "destiny": "concept_2", "weight": 0.8911}
28 | ]
29 | }"""
30 | self.fcm = from_json(default_fcm)
31 |
32 | def test_default(self):
33 | expected = {
34 | "max_iter": 500,
35 | "decision_function": "LAST",
36 | "activation_function": "sigmoid",
37 | "memory_influence": False,
38 | "stability_diff": 0.001,
39 | "stop_at_stabilize": True,
40 | "extra_steps": 5,
41 | "weight": 1,
42 | "concepts":
43 | [
44 | {
45 | "id": "concept_1",
46 | "is_active": True,
47 | "type": "SIMPLE",
48 | "activation": 0.5
49 | },
50 | {
51 | "id": "concept_2", "is_active": True,
52 | "type": "DECISION", "activation": 0.0,
53 | "custom_function": "gceq",
54 | "custom_function_args": {"weight": 0.3}
55 | },
56 | {
57 | "id": "concept_3",
58 | "is_active": True,
59 | "type": "SIMPLE",
60 | "activation": 0.0,
61 | "use_memory": True
62 | },
63 | {
64 | "id": "concept_4",
65 | "is_active": True,
66 | "type": "SIMPLE",
67 | "activation": 0.3,
68 | "custom_function": "saturation"
69 | }
70 | ],
71 | "relations":
72 | [
73 | {"origin": "concept_4", "destiny": "concept_2", "weight": -0.1},
74 | {"origin": "concept_1", "destiny": "concept_3", "weight": 0.59},
75 | {"origin": "concept_3", "destiny": "concept_2", "weight": 0.8911}
76 | ],
77 | 'activation_function_args': {'lambda_val': 1},
78 | }
79 | fcm_json = json.loads(self.fcm.to_json())
80 | self.assertEqual(expected, fcm_json)
81 |
82 |
83 | class JoinMapsTests(unittest.TestCase):
84 | def setUp(self) -> None:
85 | fcm_json1 = """
86 | {
87 | "max_iter": 500,
88 | "activation_function": "sigmoid",
89 | "actv_func_args": {"lambda_val":1},
90 | "memory_influence": false,
91 | "decision_function": "LAST",
92 | "concepts" :
93 | [
94 | {"id": "concept_1", "type": "SIMPLE", "activation": 0.25},
95 | {"id": "concept_2", "type": "DECISION", "custom_function": "gceq", "custom_function_args": {"weight":0.3}}
96 | ],
97 | "relations":
98 | [
99 | {"origin": "concept_1", "destiny": "concept_2", "weight": 0.25}
100 | ]
101 | }"""
102 |
103 | fcm_json2 = """
104 | {
105 | "max_iter": 500,
106 | "activation_function": "sigmoid",
107 | "actv_func_args": {"lambda_val":1},
108 | "memory_influence": false,
109 | "decision_function": "LAST",
110 | "concepts" :
111 | [
112 | {"id": "concept_1", "type": "SIMPLE", "activation": 0.5},
113 | {"id": "concept_2", "type": "DECISION", "custom_function": "gceq", "custom_function_args": {"weight":0.3}},
114 | {"id": "concept_4", "type": "SIMPLE", "custom_function": "saturation", "activation": 0.3}
115 | ],
116 | "relations":
117 | [
118 | {"origin": "concept_4", "destiny": "concept_2", "weight": 0.2},
119 | {"origin": "concept_1", "destiny": "concept_2", "weight": 0.75}
120 | ]
121 | }"""
122 |
123 | fcm_json3 = """
124 | {
125 | "max_iter": 500,
126 | "activation_function": "sigmoid",
127 | "actv_func_args": {"lambda_val":1},
128 | "memory_influence": false,
129 | "decision_function": "LAST",
130 | "concepts" :
131 | [
132 | {"id": "concept_1", "type": "SIMPLE", "activation": 0.75},
133 | {"id": "concept_2", "type": "DECISION", "custom_function": "gceq", "custom_function_args": {"weight":0.3}},
134 | {"id": "concept_3", "type": "SIMPLE", "memory_influence":true }
135 | ],
136 | "relations":
137 | [
138 | {"origin": "concept_1", "destiny": "concept_4", "weight": -0.3911},
139 | {"origin": "concept_2", "destiny": "concept_3", "weight": 0.8911}
140 | ]
141 | }"""
142 | self.fcm1 = from_json(fcm_json1)
143 | self.fcm2 = from_json(fcm_json2)
144 | self.fcm3 = from_json(fcm_json3)
145 |
146 | def test_default(self):
147 | fcm = join_maps([self.fcm1, self.fcm2, self.fcm3])
148 | expected = {
149 | "max_iter": 500,
150 | "decision_function": "LAST",
151 | "activation_function": "sigmoid",
152 | "memory_influence": False,
153 | "stability_diff": 0.001,
154 | "stop_at_stabilize": True,
155 | "extra_steps": 5,
156 | "weight": 1,
157 | "concepts": [
158 | {"id": "concept_1", "is_active": True, "type": "SIMPLE", "activation": 0.5},
159 | {"id": "concept_2", "is_active": True, "type": "DECISION", 'activation': 0.0,
160 | "custom_function": "gceq", "custom_function_args": {"weight": 0.3}},
161 | {"id": "concept_4", "is_active": True, "type": "SIMPLE", "activation": 0.3,
162 | "custom_function": "saturation"},
163 | {"id": "concept_3", "is_active": True, "type": "SIMPLE", "activation": 0.0},
164 | ],
165 | "relations": [
166 | {'origin': 'concept_1', 'destiny': 'concept_2', 'weight': 0.5},
167 | {'origin': 'concept_4', 'destiny': 'concept_2', 'weight': 0.2},
168 | {'origin': 'concept_2', 'destiny': 'concept_3', 'weight': 0.8911}
169 | ]
170 | }
171 |
172 | fcm_json = json.loads(fcm.to_json())
173 | self.assertEqual(expected, fcm_json)
174 |
175 | def test_inference_after_join(self):
176 | expected = {"max_iter": 500, "decision_function": "LAST", "activation_function": "sigmoid",
177 | "memory_influence": False,
178 | "stability_diff": 0.001, "stop_at_stabilize": True, "extra_steps": 5, "weight": 1,
179 | "concepts": [
180 | {"id": "concept_1", "is_active": True, "type": "SIMPLE", "activation": 0.5},
181 | {"id": "concept_2", "is_active": True, "type": "DECISION", "activation": 0.0,
182 | "custom_function": "gceq", "custom_function_args": {"weight": 0.3}},
183 | {"id": "concept_4", "is_active": True, "type": "SIMPLE", "activation": 0.0,
184 | "custom_function": "saturation"},
185 | {"id": "concept_3", "is_active": True, "type": "SIMPLE", "activation": 0.5}
186 | ],
187 | "relations": [
188 | {"origin": "concept_1", "destiny": "concept_2", "weight": 0.5},
189 | {"origin": "concept_4", "destiny": "concept_2", "weight": 0.2},
190 | {"origin": "concept_2", "destiny": "concept_3", "weight": 0.8911}
191 | ]}
192 | fcm = join_maps([self.fcm1, self.fcm2, self.fcm3])
193 | fcm.run_inference()
194 | fcm_json = json.loads(fcm.to_json())
195 | self.assertEqual(expected, fcm_json)
196 | fcm.debug = False
197 | fcm.run_inference()
198 | fcm_json = json.loads(fcm.to_json())
199 | self.assertEqual(expected, fcm_json)
200 |
201 | def test_intersection(self):
202 | fcm = join_maps([self.fcm1, self.fcm2, self.fcm3], concept_strategy='intersection')
203 | expected = {
204 | "concepts": [
205 | {'id': 'concept_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.5},
206 | {'id': 'concept_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.0,
207 | 'custom_function': 'gceq', 'custom_function_args': {'weight': 0.3}}
208 | ],
209 | "relations": [
210 | {'origin': 'concept_1', 'destiny': 'concept_2', 'weight': 0.5}
211 | ]
212 | }
213 |
214 | fcm_json = json.loads(fcm.to_json())
215 | self.assertEqual(expected['concepts'], fcm_json['concepts'])
216 | self.assertEqual(expected['relations'], fcm_json['relations'])
217 |
218 | def test_highest_strategies(self):
219 | fcm = join_maps([self.fcm1, self.fcm2, self.fcm3],
220 | concept_strategy='intersection',
221 | value_strategy='highest',
222 | relation_strategy='highest')
223 | expected = {
224 | "concepts": [
225 | {'id': 'concept_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.75},
226 | {'id': 'concept_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.0,
227 | 'custom_function': 'gceq', 'custom_function_args': {'weight': 0.3}}
228 | ],
229 | "relations": [
230 | {'origin': 'concept_1', 'destiny': 'concept_2', 'weight': 0.75}
231 | ]
232 | }
233 |
234 | fcm_json = json.loads(fcm.to_json())
235 | self.assertEqual(expected['concepts'], fcm_json['concepts'])
236 | self.assertEqual(expected['relations'], fcm_json['relations'])
237 |
238 | def test_lowest_strategies(self):
239 | fcm = join_maps([self.fcm1, self.fcm2, self.fcm3],
240 | concept_strategy='intersection',
241 | value_strategy='lowest',
242 | relation_strategy='lowest')
243 | expected = {
244 | "concepts": [
245 | {'id': 'concept_1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.25},
246 | {'id': 'concept_2', 'is_active': True, 'type': 'DECISION', 'activation': 0.0,
247 | 'custom_function': 'gceq', 'custom_function_args': {'weight': 0.3}}
248 | ],
249 | "relations": [
250 | {'origin': 'concept_1', 'destiny': 'concept_2', 'weight': 0.25}
251 | ]
252 | }
253 |
254 | fcm_json = json.loads(fcm.to_json())
255 | self.assertEqual(expected['concepts'], fcm_json['concepts'])
256 | self.assertEqual(expected['relations'], fcm_json['relations'])
257 |
258 | def test_exeptions(self):
259 | res = ''
260 | expected = 'Unknown concept strategy: aaaa'
261 | try:
262 | fcm = join_maps([self.fcm1, self.fcm2, self.fcm3], concept_strategy='aaaa')
263 | except Exception as err:
264 | res = str(err)
265 | self.assertEqual(expected, res)
266 |
267 | expected = 'Unknown value strategy: aaaa'
268 | try:
269 | fcm = join_maps([self.fcm1, self.fcm2, self.fcm3], value_strategy='aaaa')
270 | except Exception as err:
271 | res = str(err)
272 | self.assertEqual(expected, res)
273 |
274 | expected = 'Unknown relation strategy: aaaa'
275 | try:
276 | fcm = join_maps([self.fcm1, self.fcm2, self.fcm3], relation_strategy='aaaa')
277 | except Exception as err:
278 | res = str(err)
279 | self.assertEqual(expected, res)
280 |
281 | def test_join_empty_map_set(self):
282 | fcm = join_maps([],
283 | concept_strategy='intersection',
284 | value_strategy='highest',
285 | relation_strategy='highest')
286 |
287 | expected = {"max_iter": 200, "decision_function": "MEAN", "activation_function": "sigmoid_hip",
288 | "memory_influence": False, "stability_diff": 0.001, "stop_at_stabilize": True,
289 | "extra_steps": 5, "weight": 1, "concepts": [], "relations": []}
290 |
291 | fcm_json = json.loads(fcm.to_json())
292 | self.assertEqual(expected, fcm_json)
293 |
--------------------------------------------------------------------------------