Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
TBI Trajectory clustering
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Cecilia Akerlund
TBI Trajectory clustering
Commits
63b3a308
Commit
63b3a308
authored
Feb 12, 2023
by
Cecilia Akerlund
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit 2
parent
f301fd88
Changes
43
Hide whitespace changes
Inline
Side-by-side
Showing
43 changed files
with
6806 additions
and
0 deletions
+6806
-0
C_code Trajectory clustering TBI/dataformat/LICENSE-GPL-3.0
C_code Trajectory clustering TBI/dataformat/LICENSE-GPL-3.0
+674
-0
C_code Trajectory clustering TBI/dataformat/LICENSE-LGPL-3.0
C_code Trajectory clustering TBI/dataformat/LICENSE-LGPL-3.0
+165
-0
C_code Trajectory clustering TBI/dataformat/Makefile
C_code Trajectory clustering TBI/dataformat/Makefile
+37
-0
C_code Trajectory clustering TBI/dataformat/Makefile.intfloatlong
...rajectory clustering TBI/dataformat/Makefile.intfloatlong
+37
-0
C_code Trajectory clustering TBI/dataformat/data.cc
C_code Trajectory clustering TBI/dataformat/data.cc
+318
-0
C_code Trajectory clustering TBI/dataformat/data.hh
C_code Trajectory clustering TBI/dataformat/data.hh
+62
-0
C_code Trajectory clustering TBI/dataformat/datadispatch.cc
C_code Trajectory clustering TBI/dataformat/datadispatch.cc
+36
-0
C_code Trajectory clustering TBI/dataformat/datadispatch.hh
C_code Trajectory clustering TBI/dataformat/datadispatch.hh
+21
-0
C_code Trajectory clustering TBI/dataformat/datafile.cc
C_code Trajectory clustering TBI/dataformat/datafile.cc
+256
-0
C_code Trajectory clustering TBI/dataformat/datafile.hh
C_code Trajectory clustering TBI/dataformat/datafile.hh
+70
-0
C_code Trajectory clustering TBI/dataformat/format.cc
C_code Trajectory clustering TBI/dataformat/format.cc
+542
-0
C_code Trajectory clustering TBI/dataformat/format.hh
C_code Trajectory clustering TBI/dataformat/format.hh
+95
-0
C_code Trajectory clustering TBI/dataformat/formatbinary.cc
C_code Trajectory clustering TBI/dataformat/formatbinary.cc
+83
-0
C_code Trajectory clustering TBI/dataformat/formatbinary.hh
C_code Trajectory clustering TBI/dataformat/formatbinary.hh
+30
-0
C_code Trajectory clustering TBI/dataformat/formatcont.cc
C_code Trajectory clustering TBI/dataformat/formatcont.cc
+143
-0
C_code Trajectory clustering TBI/dataformat/formatcont.hh
C_code Trajectory clustering TBI/dataformat/formatcont.hh
+33
-0
C_code Trajectory clustering TBI/dataformat/formatdiscr.cc
C_code Trajectory clustering TBI/dataformat/formatdiscr.cc
+166
-0
C_code Trajectory clustering TBI/dataformat/formatdiscr.hh
C_code Trajectory clustering TBI/dataformat/formatdiscr.hh
+36
-0
C_code Trajectory clustering TBI/dataformat/formatdispatch.cc
...de Trajectory clustering TBI/dataformat/formatdispatch.cc
+148
-0
C_code Trajectory clustering TBI/dataformat/formatdispatch.hh
...de Trajectory clustering TBI/dataformat/formatdispatch.hh
+23
-0
C_code Trajectory clustering TBI/dataformat/formatignore.hh
C_code Trajectory clustering TBI/dataformat/formatignore.hh
+30
-0
C_code Trajectory clustering TBI/dataformat/formatsymbol.cc
C_code Trajectory clustering TBI/dataformat/formatsymbol.cc
+166
-0
C_code Trajectory clustering TBI/dataformat/formatsymbol.hh
C_code Trajectory clustering TBI/dataformat/formatsymbol.hh
+37
-0
C_code Trajectory clustering TBI/dataformat/formattime.cc
C_code Trajectory clustering TBI/dataformat/formattime.cc
+571
-0
C_code Trajectory clustering TBI/dataformat/formattime.hh
C_code Trajectory clustering TBI/dataformat/formattime.hh
+45
-0
C_code Trajectory clustering TBI/dataformat/formattime_old.cc
...de Trajectory clustering TBI/dataformat/formattime_old.cc
+571
-0
C_code Trajectory clustering TBI/dataformat/formattypes.hh
C_code Trajectory clustering TBI/dataformat/formattypes.hh
+28
-0
C_code Trajectory clustering TBI/dataformat/formatunknown.cc
C_code Trajectory clustering TBI/dataformat/formatunknown.cc
+111
-0
C_code Trajectory clustering TBI/dataformat/formatunknown.hh
C_code Trajectory clustering TBI/dataformat/formatunknown.hh
+34
-0
C_code Trajectory clustering TBI/dataformat/growable.hh
C_code Trajectory clustering TBI/dataformat/growable.hh
+122
-0
C_code Trajectory clustering TBI/dataformat/hashtable.hh
C_code Trajectory clustering TBI/dataformat/hashtable.hh
+155
-0
C_code Trajectory clustering TBI/dataformat/intfloat.hh
C_code Trajectory clustering TBI/dataformat/intfloat.hh
+46
-0
C_code Trajectory clustering TBI/dataformat/libdataformat.a
C_code Trajectory clustering TBI/dataformat/libdataformat.a
+0
-0
C_code Trajectory clustering TBI/dataformat/make.depend
C_code Trajectory clustering TBI/dataformat/make.depend
+21
-0
C_code Trajectory clustering TBI/dataformat/makeformat
C_code Trajectory clustering TBI/dataformat/makeformat
+0
-0
C_code Trajectory clustering TBI/dataformat/makeformat.cc
C_code Trajectory clustering TBI/dataformat/makeformat.cc
+107
-0
C_code Trajectory clustering TBI/dataformat/our-g++
C_code Trajectory clustering TBI/dataformat/our-g++
+51
-0
C_code Trajectory clustering TBI/dataformat/readtokens.cc
C_code Trajectory clustering TBI/dataformat/readtokens.cc
+389
-0
C_code Trajectory clustering TBI/dataformat/readtokens.hh
C_code Trajectory clustering TBI/dataformat/readtokens.hh
+44
-0
C_code Trajectory clustering TBI/dataformat/table.cc
C_code Trajectory clustering TBI/dataformat/table.cc
+255
-0
C_code Trajectory clustering TBI/dataformat/table.hh
C_code Trajectory clustering TBI/dataformat/table.hh
+62
-0
Clustervars_TS.R
Clustervars_TS.R
+787
-0
CreatingModelLists_TS_220826.R
CreatingModelLists_TS_220826.R
+199
-0
No files found.
C_code Trajectory clustering TBI/dataformat/LICENSE-GPL-3.0
0 → 100644
View file @
63b3a308
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
C_code Trajectory clustering TBI/dataformat/LICENSE-LGPL-3.0
0 → 100644
View file @
63b3a308
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
C_code Trajectory clustering TBI/dataformat/Makefile
0 → 100644
View file @
63b3a308
# Makefile
ifdef
OPT
OFLAGS
=
-O3
else
# Mac OS: uncomment if problems with linking libraries not compiled for 64 bits architecture
#OFLAGS = -m32 -g
OFLAGS
=
-g
endif
CXX
=
g++
#./our-g++
# CXXFLAGS = $(OFLAGS) -DGZLIB -Wall $(PG) ${DEFINES}
CXXFLAGS
=
$(OFLAGS)
-Wall
${DEFINES}
CC
=
g++
#./our-g++
CFLAGS
=
$(OFLAGS)
-Wall
${DEFINES}
OBJ1
=
readtokens.o table.o
OBJ2
=
format.o formatdispatch.o formatbinary.o formatdiscr.o formatcont.o formatsymbol.o formattime.o formatunknown.o
OBJ3
=
data.o datafile.o datadispatch.o
all
:
libdataformat.a
makeformat
:
makeformat.o ${OBJ1} ${OBJ2} ${OBJ3}
libdataformat.a
:
${OBJ1} ${OBJ2} ${OBJ3}
rm
-f
libdataformat.a
ar clq libdataformat.a
$^
ranlib libdataformat.a
clean
:
rm
-f
${OBJ1}
${OBJ2}
${OBJ3}
depend
:
rm
-f
make.depend
${CC}
-MM
*
.cc
${CFLAGS}
>
make.depend
include
make.depend
C_code Trajectory clustering TBI/dataformat/Makefile.intfloatlong
0 → 100644
View file @
63b3a308
# Makefile
ifdef
OPT
OFLAGS
=
-O3
else
# Mac OS: uncomment if problems with linking libraries not compiled for 64 bits architecture
#OFLAGS = -m32 -g
OFLAGS
=
-g
endif
CXX
=
./our-g++
#CXXFLAGS = $(OFLAGS) -DGZLIB -DINTFLOAT_LONG -Wall $(PG) ${DEFINES}
CXXFLAGS
=
$(OFLAGS)
-DINTFLOAT_LONG
-Wall
${DEFINES}
CC
=
gcc
CFLAGS
=
$(OFLAGS)
-Wall
${DEFINES}
OBJ1
=
readtokens.o table.o
OBJ2
=
format.o formatdispatch.o formatbinary.o formatdiscr.o formatcont.o formatsymbol.o formattime.o formatunknown.o
OBJ3
=
data.o datafile.o datadispatch.o
all
:
libdataformat.a
makeformat
:
makeformat.o ${OBJ1} ${OBJ2} ${OBJ3}
libdataformat.a
:
${OBJ1} ${OBJ2} ${OBJ3}
rm
-f
libdataformat.a
ar clq libdataformat.a
$^
ranlib libdataformat.a
clean
:
rm
-f
${OBJ1}
${OBJ2}
${OBJ3}
depend
:
rm
-f
make.depend
${CC}
-MM
*
.cc
${CFLAGS}
>
make.depend
include
make.depend
C_code Trajectory clustering TBI/dataformat/data.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef GZLIB
#include <zlib.h>
#endif
#include "readtokens.hh"
#include "format.hh"
#include "data.hh"
/*
class DataObject {
public:
DataObject();
DataObject(class Format* fr, const char* datafile = 0);
DataObject(class Format* fr, FILE* datafile);
DataObject(const char* formatfile, const char* datafile = 0);
virtual ~DataObject();
virtual void clear();
virtual void remove(int ind, int n = 1);
virtual union intfloat* newentry();
virtual int read(const char* datafile, int append = 0);
virtual int save(const char* datafile, int standard = 0);
virtual char* getname(int n);
virtual int findname(const char* str);
virtual int hasnames();
virtual void sort(int (*func)(union intfloat* v1, union intfloat* v2));
virtual union intfloat* operator[](int i) { if (i<(bl<0 ? num : num-1))
return (*data)[(bl<0 || bl>i ? i : i+1)];
return 0; };
virtual void block(int i) { bl = i; };
virtual int size() { return (bl<0 ? num : num-1); };
virtual int length() { return len; };
virtual class Format* format() { return form; };
virtual void setformat(Format* f, int own = 0) { if (!form) { form = f; len = f->length(); ownform = own; }};
protected:
virtual int read_file(FILE* f, int append = 0);
virtual int internal_read(FILE* f);
virtual int internal_save(FILE* f);
int bl;
int num;
int len;
Format* form;
int ownform;
class Growable<union intfloat* > *data;
};
*/
DataObject
::
DataObject
()
{
num
=
0
;
len
=
0
;
bl
=
-
1
;
form
=
0
;
ownform
=
0
;
data
=
new
Growable
<
intfloat
*>
(
0
,
128
);
}
DataObject
::
DataObject
(
class
Format
*
fr
,
const
char
*
datafile
)
{
num
=
0
;
bl
=
-
1
;
form
=
fr
;
ownform
=
0
;
len
=
form
->
length
();
data
=
new
Growable
<
intfloat
*>
(
0
,
128
);
if
(
datafile
)
read
(
datafile
);
}
DataObject
::
DataObject
(
class
Format
*
fr
,
FILE
*
datafile
)
{
num
=
0
;
bl
=
-
1
;
form
=
fr
;
ownform
=
0
;
len
=
form
->
length
();
data
=
new
Growable
<
intfloat
*>
(
0
,
128
);
if
(
datafile
)
read_file
(
datafile
);
}
DataObject
::
DataObject
(
const
char
*
formatfile
,
const
char
*
datafile
)
{
num
=
0
;
bl
=
-
1
;
data
=
new
Growable
<
intfloat
*>
(
0
,
128
);
form
=
new
Format
();
ownform
=
1
;
form
->
read
(
formatfile
);
len
=
form
->
length
();
if
(
len
==
-
1
)
{
fprintf
(
stderr
,
"Failed to read format file
\"
%s
\"\n
"
,
formatfile
);
return
;
}
if
(
datafile
)
read
(
datafile
);
}
DataObject
::~
DataObject
()
{
clear
();
delete
data
;
if
(
ownform
)
delete
form
;
}
void
DataObject
::
clear
()
{
intfloat
*
vec
;
if
(
data
)
{
FORVEC
(
*
data
,
vec
)
delete
[]
vec
;
data
->
remove
(
num
);
num
=
0
;
}
}
void
DataObject
::
remove
(
int
ind
,
int
n
)
{
int
i
;
if
(
data
)
{
if
(
ind
+
n
>
num
)
n
=
num
-
ind
;
for
(
i
=
ind
;
i
<
n
+
ind
;
i
++
)
{
delete
[]
(
*
data
)[
i
];
(
*
data
)[
i
]
=
(
*
data
)[
i
+
n
];
}
for
(;
i
<
num
-
n
;
i
++
)
(
*
data
)[
i
]
=
(
*
data
)[
i
+
n
];
data
->
remove
(
n
);
num
-=
n
;
}
}
union
intfloat
*
DataObject
::
newentry
()
{
intfloat
*
vec
;
vec
=
new
intfloat
[
len
];
(
*
data
)[
num
++
]
=
vec
;
return
vec
;
}
int
DataObject
::
read
(
const
char
*
datafile
,
int
append
)
{
FILE
*
f
;
if
((
f
=
fopen
(
datafile
,
"r"
))
==
0
)
{
fprintf
(
stderr
,
"Failed to read data file
\"
%s
\"\n
"
,
datafile
);
return
-
1
;
}
int
status
=
read_file
(
f
,
append
);
fclose
(
f
);
return
status
;
}
int
DataObject
::
read_file
(
FILE
*
f
,
int
append
)
{
int
status
;
if
(
!
append
)
{
clear
();
}
status
=
internal_read
(
f
);
return
(
status
?
num
:
-
1
);
}
#ifdef GZLIB
extern
FILE
*
gz_file
;
extern
gzFile
gz_gzfile
;
void
getfirst
(
FILE
*
f
)
{
int
c
=
fgetc
(
f
);
//printf("char1 = %d\n", c);
rewind
(
f
);
if
(
c
==
31
)
{
gz_gzfile
=
gzdopen
(
fileno
(
f
),
"rb"
);
//printf("gzf = %p\n", gz_gzfile);
gz_file
=
f
;
}
else
{
gz_gzfile
=
NULL
;
gz_file
=
NULL
;
}
}
#endif
int
DataObject
::
internal_read
(
FILE
*
f
)
{
intfloat
*
vec
;
TokenLink
*
row
;
int
*
trans
=
0
;
int
translen
;
#ifdef GZLIB
getfirst
(
f
);
#endif
row
=
readtokens
(
f
);
if
(
form
->
check_header
(
row
))
{
if
(
hasnames
())
{
trans
=
form
->
get_translation
(
row
,
translen
);
}
else
{
form
->
extract_header
(
row
);
}
freetokens
(
row
);
row
=
readtokens
(
f
);
}
while
(
row
)
{
if
(
!*
row
->
token
&&
!
row
->
next
)
{
freetokens
(
row
);
row
=
readtokens
(
f
);
continue
;
}
vec
=
new
intfloat
[
len
];
if
(
trans
)
form
->
insert_input_translated
(
row
,
vec
,
trans
,
translen
);
else
form
->
insert_input
(
row
,
vec
);
(
*
data
)[
num
++
]
=
vec
;
freetokens
(
row
);
row
=
readtokens
(
f
);
}
if
(
trans
)
delete
[]
trans
;
return
1
;
}
int
DataObject
::
save
(
const
char
*
datafile
,
int
standard
)
{
FILE
*
f
;
int
status
;
if
(
!
data
)
{
fprintf
(
stderr
,
"No data to save
\n
"
);
return
-
1
;
}
if
((
f
=
fopen
(
datafile
,
"w"
))
==
0
)
{
fprintf
(
stderr
,
"Failed to save data to file
\"
%s
\"\n
"
,
datafile
);
return
-
1
;
}
if
(
standard
)
status
=
DataObject
::
internal_save
(
f
);
else
status
=
internal_save
(
f
);
fclose
(
f
);
return
(
status
?
0
:
-
1
);
}
int
DataObject
::
internal_save
(
FILE
*
f
)
{
intfloat
*
vec
;
if
(
hasnames
())
form
->
save_header
(
f
);
FORVEC
(
*
data
,
vec
)
form
->
save_output
(
f
,
vec
);
return
1
;
}
char
*
DataObject
::
getname
(
int
n
)
{
FormatSpec
*
f
;
f
=
form
->
nth
(
n
);
if
(
f
)
return
f
->
name
;
else
return
0
;
}
int
DataObject
::
findname
(
const
char
*
str
)
{
FormatSpec
*
f
=
form
->
nth
(
0
);
int
i
=
0
;
while
(
f
&&
(
!
f
->
name
||
strcmp
(
str
,
f
->
name
)))
f
=
f
->
next
,
i
++
;
return
(
f
?
i
:
-
1
);
}
int
DataObject
::
hasnames
()
{
FormatSpec
*
f
;
f
=
form
->
nth
(
0
);
while
(
f
&&
!
f
->
name
)
f
=
f
->
next
;
return
(
f
?
1
:
0
);
}
static
int
(
*
intern_sfunc
)(
union
intfloat
*
v1
,
union
intfloat
*
v2
);
static
int
hfunc
(
const
void
*
p1
,
const
void
*
p2
)
{
return
intern_sfunc
(
*
(
intfloat
**
)
p1
,
*
(
intfloat
**
)
p2
);
}
void
DataObject
::
sort
(
int
(
*
func
)(
union
intfloat
*
v1
,
union
intfloat
*
v2
))
{
intern_sfunc
=
func
;
qsort
(
data
->
vect
(),
num
,
sizeof
(
intfloat
*
),
hfunc
);
}
C_code Trajectory clustering TBI/dataformat/data.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#ifndef DATA_HH
#define DATA_HH
#include "growable.hh"
#include <stdio.h>
class
DataObject
{
public:
DataObject
();
DataObject
(
class
Format
*
fr
,
const
char
*
datafile
=
0
);
DataObject
(
class
Format
*
fr
,
FILE
*
datafile
);
DataObject
(
const
char
*
formatfile
,
const
char
*
datafile
=
0
);
virtual
~
DataObject
();
virtual
void
clear
();
virtual
void
remove
(
int
ind
,
int
n
=
1
);
virtual
union
intfloat
*
newentry
();
virtual
int
read
(
const
char
*
datafile
,
int
append
=
0
);
virtual
int
save
(
const
char
*
datafile
,
int
standard
=
0
);
virtual
char
*
getname
(
int
n
);
virtual
int
findname
(
const
char
*
str
);
virtual
int
hasnames
();
virtual
void
sort
(
int
(
*
func
)(
union
intfloat
*
v1
,
union
intfloat
*
v2
));
virtual
union
intfloat
*
operator
[](
int
i
)
{
if
(
i
<
(
bl
<
0
?
num
:
num
-
1
))
return
(
*
data
)[(
bl
<
0
||
bl
>
i
?
i
:
i
+
1
)];
return
0
;
};
virtual
void
block
(
int
i
)
{
bl
=
i
;
};
virtual
int
size
()
{
return
(
bl
<
0
?
num
:
num
-
1
);
};
virtual
int
length
()
{
return
len
;
};
virtual
class
Format
*
format
()
{
return
form
;
};
virtual
void
setformat
(
Format
*
f
,
int
own
=
0
)
{
if
(
!
form
)
{
form
=
f
;
len
=
f
->
length
();
ownform
=
own
;
}};
protected:
virtual
int
read_file
(
FILE
*
f
,
int
append
=
0
);
virtual
int
internal_read
(
FILE
*
f
);
virtual
int
internal_save
(
FILE
*
f
);
int
bl
;
int
num
;
int
len
;
Format
*
form
;
int
ownform
;
class
Growable
<
union
intfloat
*
>
*
data
;
};
#endif
/* DATA_HH */
C_code Trajectory clustering TBI/dataformat/datadispatch.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "format.hh"
#include "data.hh"
#include "datadispatch.hh"
DataObject
*
read_data
(
char
*
datafile
)
{
Format
*
format
=
new
Format
();
format
->
construct
(
datafile
);
return
new
DataObject
(
format
,
datafile
);
}
DataObject
*
read_data
(
class
Format
*
fr
,
char
*
datafile
)
{
return
new
DataObject
(
fr
,
datafile
);
}
C_code Trajectory clustering TBI/dataformat/datadispatch.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
class
DataObject
*
read_data
(
char
*
datafile
);
class
DataObject
*
read_data
(
class
Format
*
fr
,
char
*
datafile
);
C_code Trajectory clustering TBI/dataformat/datafile.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#ifndef _MSC_VER // Microsoft Visual C++
#include <unistd.h>
#endif
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include "readtokens.hh"
#include "format.hh"
#include "data.hh"
#include "datafile.hh"
#ifdef _MSC_VER // Microsoft Visual C++
#include <io.h>
#define open _open
#define fdopen _fdopen
#endif
/*
class DataFileObject : public DataObject {
public:
DataFileObject(class Format* fr, const char* datafile) : DataObject(fr) { init(datafile); };
DataFileObject(const char* formatfile, const char* datafile) : DataObject(formatfile) { init(datafile); };
void init(const char* datafile);
virtual ~DataFileObject();
virtual void clear();
virtual void remove(int ind, int n = 1) {}; // Finns ej
virtual union intfloat* newentry() { return 0; }; // Finns ej
virtual int read(const char* datafile, int append = 0);
virtual int save(const char* datafile, int standard = 0) { return -1; }; // Finns ej
virtual void sort(int (*func)(union intfloat* v1, union intfloat* v2)) {}; // Inte n...
virtual union intfloat* operator[](int i);
protected:
virtual int read_start(const char* datafile);
virtual void read_stop();
virtual int read_one(off64_t pos, intfloat* vec);
class Growable<off64_t> *datapos;
off64_t lastpos;
int cachesize;
int* cacheind;
union intfloat** cachevec;
int currind;
int* trans;
int translen;
int fd;
FILE* fp;
int complete;
};
*/
void
DataFileObject
::
init
(
const
char
*
datafile
)
{
int
i
;
datapos
=
new
Growable
<
off64_t
>
(
0
,
1048576
);
cachesize
=
8
;
cacheind
=
new
int
[
cachesize
];
cachevec
=
new
intfloat
*
[
cachesize
];
for
(
i
=
0
;
i
<
cachesize
;
i
++
)
{
cacheind
[
i
]
=
-
1
;
cachevec
[
i
]
=
new
intfloat
[
len
];
}
currind
=
-
1
;
trans
=
0
;
translen
=
0
;
fd
=
-
1
;
fp
=
0
;
complete
=
1
;
read_start
(
datafile
);
}
DataFileObject
::~
DataFileObject
()
{
int
i
;
if
(
!
complete
)
read_stop
();
delete
datapos
;
delete
[]
cacheind
;
for
(
i
=
0
;
i
<
cachesize
;
i
++
)
delete
[]
cachevec
[
i
];
delete
[]
cachevec
;
if
(
trans
)
delete
[]
trans
;
}
void
DataFileObject
::
clear
()
{
int
i
;
if
(
num
)
{
delete
datapos
;
datapos
=
new
Growable
<
off64_t
>
(
0
,
1048576
);
}
for
(
i
=
0
;
i
<
cachesize
;
i
++
)
{
if
(
cachevec
[
i
])
delete
[]
cachevec
[
i
];
cachevec
[
i
]
=
new
intfloat
[
len
];
cacheind
[
i
]
=
-
1
;
}
currind
=
-
1
;
}
int
DataFileObject
::
read
(
const
char
*
datafile
,
int
append
)
{
int
status
;
clear
();
// Ignore append - always clear
if
(
!
complete
)
read_stop
();
status
=
read_start
(
datafile
);
return
(
status
?
num
:
-
1
);
}
int
DataFileObject
::
read_start
(
const
char
*
datafile
)
{
TokenLink
*
row
;
if
((
fd
=
open
(
datafile
,
O_RDONLY
|
O_LARGEFILE
))
==
-
1
)
{
fprintf
(
stderr
,
"Failed to open data file
\"
%s
\"\n
"
,
datafile
);
return
-
1
;
}
fp
=
fdopen
(
fd
,
"r"
);
// Nu har vi samma fil bde som en fd och en FILE*, bda behvs
row
=
readtokens
(
fp
);
if
(
form
->
check_header
(
row
))
{
if
(
hasnames
())
{
trans
=
form
->
get_translation
(
row
,
translen
);
}
else
{
form
->
extract_header
(
row
);
}
freetokens
(
row
);
}
else
{
// Ta hand om den oavsiktligt lsta dataraden
currind
=
(
currind
+
1
)
%
cachesize
;
form
->
insert_input
(
row
,
cachevec
[
currind
]);
(
*
datapos
)[
0
]
=
0
;
num
++
;
freetokens
(
row
);
}
lastpos
=
ftell
(
fp
);
num
++
;
complete
=
0
;
return
1
;
}
void
DataFileObject
::
read_stop
()
{
if
(
!
complete
)
{
num
-=
1
;
fclose
(
fp
);
complete
=
1
;
}
}
int
DataFileObject
::
read_one
(
off64_t
pos
,
intfloat
*
vec
)
{
TokenLink
*
row
;
fseek
(
fp
,
pos
,
SEEK_SET
);
row
=
readtokens
(
fp
);
if
(
!
row
||
(
!*
row
->
token
&&
!
row
->
next
))
{
if
(
row
)
freetokens
(
row
);
if
(
feof
(
fp
))
{
clearerr
(
fp
);
return
-
1
;
}
else
return
0
;
}
if
(
ferror
(
fp
))
{
freetokens
(
row
);
return
-
1
;
}
if
(
trans
)
form
->
insert_input_translated
(
row
,
vec
,
trans
,
translen
);
else
form
->
insert_input
(
row
,
vec
);
freetokens
(
row
);
return
1
;
}
intfloat
*
DataFileObject
::
operator
[](
int
i
)
{
TokenLink
*
row
;
int
j
;
if
(
i
<
0
)
return
0
;
if
(
bl
>=
0
&&
bl
<=
i
)
i
+=
1
;
if
(
complete
&&
i
>=
num
)
return
0
;
if
(
!
complete
&&
i
>=
num
-
1
)
{
fseek
(
fp
,
lastpos
,
SEEK_SET
);
row
=
0
;
for
(
j
=
num
-
1
;
j
<=
i
;
j
++
)
{
if
(
row
)
freetokens
(
row
);
row
=
readtokens
(
fp
);
while
(
!
row
||
(
!*
row
->
token
&&
!
row
->
next
))
{
if
(
row
)
freetokens
(
row
);
if
(
feof
(
fp
)
||
ferror
(
fp
))
{
clearerr
(
fp
);
num
=
j
;
complete
=
1
;
return
0
;
}
lastpos
=
ftell
(
fp
);
row
=
readtokens
(
fp
);
}
(
*
datapos
)[
j
]
=
lastpos
;
lastpos
=
ftell
(
fp
);
}
currind
=
(
currind
+
1
)
%
cachesize
;
if
(
trans
)
form
->
insert_input_translated
(
row
,
cachevec
[
currind
],
trans
,
translen
);
else
form
->
insert_input
(
row
,
cachevec
[
currind
]);
cacheind
[
currind
]
=
i
;
num
=
i
+
2
;
freetokens
(
row
);
return
cachevec
[
currind
];
}
else
{
for
(
j
=
currind
;
j
>=
0
;
j
--
)
if
(
cacheind
[
j
]
==
i
)
return
cachevec
[
j
];
for
(
j
=
cachesize
-
1
;
j
>
currind
;
j
--
)
if
(
cacheind
[
j
]
==
i
)
return
cachevec
[
j
];
currind
=
(
currind
+
1
)
%
cachesize
;
read_one
((
*
datapos
)[
i
],
cachevec
[
currind
]);
cacheind
[
currind
]
=
i
;
return
cachevec
[
currind
];
}
}
// Repetition
// Nr filen ppnas, kolla headrar och stt trans, om en rad lst stoppa in i cache
// Nr man accessar en rad:
// om den finns i cache, returnera
// om den finns i tabellen, anvnd read_one som hanterar cachen
// om inte complete, ls fram till rtt index, ha koll p filslut och stt complete
// annars skicka tillbaka 0
C_code Trajectory clustering TBI/dataformat/datafile.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#ifdef __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_MAC
/* [PD] off64_t is not defined in BSD, and thus not in darwin (Mac OS X)
either. We can use off_t instead since it is 64-bit in BSD. */
#define off64_t off_t
/* [PD] O_LARGEFILE is not defined in darwin (Mac OS X), and apparently
not necessary. */
#define O_LARGEFILE 0
#endif
#endif
#ifdef PLATFORM_MSW // [PD] Tentatively only check for WIN32.
// I don't know what to do (if anything) on WIN64.
// [Arndt] _WIN32 doesn't work on cygwin; use our own
/* [PD] O_LARGEFILE is apparently not necessary on MS Windows. */
#define O_LARGEFILE 0
#ifdef _MSC_VER // Microsoft Visual C++
#include <sys/types.h> // off_t
#endif
#define off64_t off_t
#endif
class
DataFileObject
:
public
DataObject
{
public:
DataFileObject
(
class
Format
*
fr
,
const
char
*
datafile
)
:
DataObject
(
fr
)
{
init
(
datafile
);
};
DataFileObject
(
const
char
*
formatfile
,
const
char
*
datafile
)
:
DataObject
(
formatfile
)
{
init
(
datafile
);
};
void
init
(
const
char
*
datafile
);
virtual
~
DataFileObject
();
virtual
void
clear
();
virtual
void
remove
(
int
ind
,
int
n
=
1
)
{};
// Finns ej
virtual
union
intfloat
*
newentry
()
{
return
0
;
};
// Finns ej
virtual
int
read
(
const
char
*
datafile
,
int
append
=
0
);
virtual
int
save
(
const
char
*
datafile
,
int
standard
=
0
)
{
return
-
1
;
};
// Finns ej
virtual
void
sort
(
int
(
*
func
)(
union
intfloat
*
v1
,
union
intfloat
*
v2
))
{};
// Inte n...
virtual
union
intfloat
*
operator
[](
int
i
);
protected:
virtual
int
read_start
(
const
char
*
datafile
);
virtual
void
read_stop
();
virtual
int
read_one
(
off64_t
pos
,
intfloat
*
vec
);
class
Growable
<
off64_t
>
*
datapos
;
off64_t
lastpos
;
int
cachesize
;
int
*
cacheind
;
union
intfloat
**
cachevec
;
int
currind
;
int
*
trans
;
int
translen
;
int
fd
;
FILE
*
fp
;
int
complete
;
};
C_code Trajectory clustering TBI/dataformat/format.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "table.hh"
#include "readtokens.hh"
#include "format.hh"
#include "formatunknown.hh"
#include "formatdispatch.hh"
#include "formattypes.hh"
#include "formattime.hh"
/*
class FormatSpec {
friend class Format;
public:
FormatSpec(const char* nm = 0);
virtual ~FormatSpec() { if (name) delete [] name; };
virtual FormatSpec* copy() { return 0; };
virtual void init(struct TokenLink* row) {};
virtual void setname(const char* nm);
virtual char* stripname(struct TokenLink*& row);
virtual void printname(FILE* f);
virtual void add(const char* str) {};
virtual void save(FILE* f) {};
virtual intfloat interpret(const char* str) { intfloat r; r.i = -1; return r; };
virtual const char* represent(intfloat v) { return "?"; };
virtual int get_discr(int val) { return -1; };
virtual const char* get_symbol(int val) { return 0; };
virtual void usurp(class FormatSpecUnknown* ele) {};
virtual int type() { return -1; };
virtual int getnum() { return -1; };
char* name;
FormatSpec* next;
int unset;
};
class Format {
public:
Format();
~Format();
int read(const char* name);
int save(const char* name);
int simple_construct(const char* formcodes);
int construct(const char* name);
int check_header(TokenLink* row);
void extract_header(TokenLink* row);
int* get_translation(TokenLink* row, int& tl);
void add(FormatSpec* formspec);
int length() { return (unset ? -1 : len); };
FormatSpec* nth(int n) { FormatSpec* f; for(f=list; f&&n; n--,f=f->next); return f; };
FormatSpec* findcolumn(const char* name) { FormatSpec* f; for (f=list; f && (!f->name || strcmp(name, f->name)); f = f->next); return f; };
void insert_input(TokenLink* tok, intfloat* vec);
void insert_input_translated(TokenLink* tok, intfloat* vec, int* trans, int tl);
void save_output(FILE* f, intfloat* vec);
void save_header(FILE* f);
int numsections();
int getsection(int n);
void newsection();
protected:
void internal_construct(TokenLink* row);
void internal_add(TokenLink* row, int ln);
void internal_guess();
int len;
int unset;
FormatSpec* list;
class Growable<int> *section_hints;
};
extern int global_format_quiet;
*/
int
global_format_quiet
=
0
;
FormatSpec
::
FormatSpec
(
const
char
*
nm
)
{
unset
=
1
;
next
=
0
;
name
=
(
nm
?
strcpy
(
new
char
[
strlen
(
nm
)
+
1
],
nm
)
:
0
);
}
void
FormatSpec
::
setname
(
const
char
*
nm
)
{
if
(
name
)
delete
[]
name
;
name
=
(
nm
?
strcpy
(
new
char
[
strlen
(
nm
)
+
1
],
nm
)
:
0
);
}
char
*
FormatSpec
::
stripname
(
struct
TokenLink
*&
row
)
{
size_t
len
;
char
*
res
;
if
(
row
&&
row
->
token
&&
(
*
row
->
token
==
'"'
))
{
len
=
strlen
(
row
->
token
)
-
1
;
res
=
new
char
[
len
+
1
];
res
=
strcpy
(
res
,
row
->
token
+
1
);
if
(
res
[
len
-
1
]
==
'"'
)
res
[
len
-
1
]
=
0
;
row
=
row
->
next
;
return
res
;
}
else
return
0
;
}
void
FormatSpec
::
printname
(
FILE
*
f
)
{
if
(
name
)
fprintf
(
f
,
"
\"
%s
\"\t
"
,
name
);
}
Format
::
Format
()
{
len
=
0
;
unset
=
1
;
list
=
0
;
section_hints
=
new
Growable
<
int
>
(
0
,
10
);
(
*
section_hints
)[
0
]
=
0
;
}
Format
::~
Format
()
{
FormatSpec
*
ele
;
while
(
list
)
{
ele
=
list
;
list
=
ele
->
next
;
delete
ele
;
}
delete
section_hints
;
}
int
Format
::
read
(
const
char
*
name
)
{
FILE
*
f
;
FormatSpec
*
ele
;
FormatSpec
*
last
;
TokenLink
*
row
;
char
last_separator_char
=
get_readtoken_separator_char
();
char
last_quote_delim
=
get_readtoken_quote_delim
();
int
hint
;
if
((
f
=
fopen
(
name
,
"r"
))
==
0
)
return
0
;
set_readtoken_separator_char
(
'\t'
);
set_readtoken_quote_delim
(
0
);
while
(
list
)
{
ele
=
list
;
list
=
ele
->
next
;
delete
ele
;
}
last
=
0
;
hint
=
1
;
do
{
while
((
row
=
readtokens
(
f
))
&&
(
*
row
->
token
!=
0
))
{
hint
=
0
;
ele
=
format_create
(
row
);
ele
->
init
(
row
);
if
(
!
list
)
last
=
list
=
ele
;
else
last
=
last
->
next
=
ele
;
len
+=
1
;
freetokens
(
row
);
}
freetokens
(
row
);
if
(
!
hint
)
{
(
*
section_hints
)[
0
]
++
;
(
*
section_hints
)
<<
len
;
hint
=
1
;
}
}
while
(
!
feof
(
f
));
fclose
(
f
);
unset
=
0
;
set_readtoken_separator_char
(
last_separator_char
);
set_readtoken_quote_delim
(
last_quote_delim
);
return
1
;
}
int
Format
::
save
(
const
char
*
name
)
{
FILE
*
f
;
FormatSpec
*
ele
;
int
i
,
sec
;
if
((
f
=
fopen
(
name
,
"w"
))
==
0
)
return
0
;
ele
=
list
;
i
=
0
;
sec
=
((
*
section_hints
)[
0
]
?
1
:
0
);
while
(
ele
)
{
if
(
sec
&&
(
*
section_hints
)[
sec
]
==
i
)
{
fprintf
(
f
,
"
\n
"
);
sec
++
;
}
ele
->
save
(
f
);
ele
=
ele
->
next
;
i
++
;
}
fclose
(
f
);
return
1
;
}
void
Format
::
insert_input
(
TokenLink
*
tok
,
intfloat
*
vec
)
{
FormatSpec
*
ele
;
ele
=
list
;
while
(
tok
&&
ele
)
{
*
vec
=
ele
->
interpret
(
tok
->
token
);
tok
=
tok
->
next
;
ele
=
ele
->
next
;
vec
++
;
}
if
(
ele
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too few values in input
\n
"
);
}
else
if
(
tok
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many values in input
\n
"
);
}
}
void
Format
::
insert_input_translated
(
TokenLink
*
tok
,
intfloat
*
vec
,
int
*
trans
,
int
tl
)
{
int
i
=
0
;
for
(
i
=
1
;
i
<
len
;
i
++
)
vec
[
i
].
i
=
-
1
;
i
=
0
;
while
(
tok
&&
i
<
tl
)
{
if
(
trans
[
i
]
>
-
1
)
vec
[
trans
[
i
]]
=
nth
(
trans
[
i
])
->
interpret
(
tok
->
token
);
tok
=
tok
->
next
;
i
++
;
}
if
(
i
<
tl
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too few values in input
\n
"
);
}
else
if
(
tok
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many values in input
\n
"
);
}
}
void
Format
::
save_output
(
FILE
*
f
,
intfloat
*
vec
)
{
int
i
;
intfloat
*
p
=
vec
;
FormatSpec
*
ele
=
list
;
if
(
ele
)
{
fprintf
(
f
,
"%s"
,
ele
->
represent
(
*
p
));
ele
=
ele
->
next
;
p
++
;
}
for
(
i
=
1
;
i
<
len
&&
ele
;
i
++
,
p
++
,
ele
=
ele
->
next
)
fprintf
(
f
,
"
\t
%s"
,
ele
->
represent
(
*
p
));
fprintf
(
f
,
"
\n
"
);
}
void
Format
::
save_header
(
FILE
*
f
)
{
int
i
;
FormatSpec
*
ele
=
list
;
if
(
ele
)
{
fprintf
(
f
,
"%s"
,
(
ele
->
name
?
ele
->
name
:
""
));
ele
=
ele
->
next
;
}
for
(
i
=
1
;
i
<
len
&&
ele
;
i
++
,
ele
=
ele
->
next
)
fprintf
(
f
,
"
\t
%s"
,
(
ele
->
name
?
ele
->
name
:
""
));
fprintf
(
f
,
"
\n
"
);
}
void
Format
::
add
(
FormatSpec
*
fs
)
{
FormatSpec
*
last
;
if
(
!
list
)
list
=
fs
;
else
{
for
(
last
=
list
;
last
->
next
;
last
=
last
->
next
);
last
->
next
=
fs
;
}
unset
=
0
;
len
+=
1
;
}
int
Format
::
numsections
()
{
return
(
*
section_hints
)[
0
];
}
int
Format
::
getsection
(
int
n
)
{
if
(
n
<
(
*
section_hints
)[
0
])
return
(
*
section_hints
)[
n
+
1
];
else
return
-
1
;
}
void
Format
::
newsection
()
{
if
((
*
section_hints
)[(
*
section_hints
)[
0
]]
!=
len
)
{
(
*
section_hints
)[
0
]
++
;
(
*
section_hints
)
<<
len
;
}
}
void
Format
::
internal_construct
(
TokenLink
*
row
)
{
FormatSpec
*
ele
;
FormatSpec
*
last
;
if
(
!
row
||
(
!*
row
->
token
&&
!
row
->
next
))
return
;
last
=
0
;
while
(
row
)
{
ele
=
format_guess
(
row
->
token
,
1
);
ele
->
add
(
row
->
token
);
if
(
!
list
)
last
=
list
=
ele
;
else
last
=
last
->
next
=
ele
;
len
+=
1
;
row
=
row
->
next
;
}
}
void
Format
::
internal_add
(
TokenLink
*
row
,
int
ln
)
{
FormatSpec
*
ele
;
FormatSpec
*
last
;
FormatSpec
*
ele2
;
if
(
!
row
||
(
!*
row
->
token
&&
!
row
->
next
))
return
;
ele
=
list
;
last
=
0
;
while
(
row
&&
ele
)
{
if
(
IsType
(
ele
,
FormatSpecUnknown
)
&&
((
ele2
=
format_guess
(
row
->
token
,
0
))
||
(((
FormatSpecUnknown
*
)
ele
)
->
nseen
>
500
&&
(
ele2
=
format_guess_unknown
((
FormatSpecUnknown
*
)
ele
)))))
{
ele2
->
usurp
((
FormatSpecUnknown
*
)
ele
);
ele2
->
add
(
row
->
token
);
ele2
->
next
=
ele
->
next
;
if
(
last
)
last
->
next
=
ele2
;
else
list
=
ele2
;
delete
ele
;
ele
=
ele2
;
}
else
{
ele
->
add
(
row
->
token
);
}
row
=
row
->
next
;
last
=
ele
;
ele
=
ele
->
next
;
}
if
(
ele
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too few values in input, on line %d
\n
"
,
ln
);
}
else
if
(
row
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many values in input, on line %d
\n
"
,
ln
);
}
}
void
Format
::
extract_header
(
TokenLink
*
row
)
{
size_t
l
;
FormatSpec
*
ele
=
list
;
while
(
ele
&&
row
)
{
if
(
row
->
token
&&
(
*
row
->
token
==
'"'
))
{
l
=
strlen
(
row
->
token
)
-
1
;
ele
->
name
=
new
char
[
l
+
1
];
strcpy
(
ele
->
name
,
row
->
token
+
1
);
if
(
ele
->
name
[
l
-
1
]
==
'"'
)
ele
->
name
[
l
-
1
]
=
0
;
}
else
if
(
row
->
token
)
{
l
=
strlen
(
row
->
token
);
ele
->
name
=
new
char
[
l
+
1
];
strcpy
(
ele
->
name
,
row
->
token
);
}
ele
=
ele
->
next
;
row
=
row
->
next
;
}
if
(
ele
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too few header names in input
\n
"
);
}
else
if
(
row
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many header names in input
\n
"
);
}
}
int
Format
::
check_header
(
TokenLink
*
row
)
{
int
ok
,
c_match
=
0
,
c_miss
=
0
;
FormatSpec
*
ele
=
list
;
while
(
ele
&&
row
)
{
if
(
!
is_unknown
(
row
->
token
)
&&
ele
->
type
()
!=
FormatSpecSymbolType
)
{
if
(
ele
->
type
()
==
FormatSpecDatetimeType
)
{
/*
if (check_datetime(row->token))
c_match++;
else
c_miss++;
*/
FormatSpecDatetime
*
dt
=
(
FormatSpecDatetime
*
)
ele
;
if
(
dt
->
unset
)
{
int
b
;
const
char
*
fs
=
0
;
ok
=
check_datetime
(
row
->
token
,
27
,
b
,
fs
);
if
(
ok
)
delete
[]
fs
;
}
else
{
ok
=
check_formated_datetime
(
row
->
token
,
dt
->
dtformbits
,
dt
->
dtformspec
);
}
if
(
ok
)
c_match
++
;
else
c_miss
++
;
}
else
{
if
(
is_cont
(
row
->
token
))
// Aproximation.
c_match
++
;
else
c_miss
++
;
}
}
ele
=
ele
->
next
;
row
=
row
->
next
;
}
return
(
c_miss
>
c_match
);
}
int
*
Format
::
get_translation
(
TokenLink
*
row
,
int
&
tl
)
{
int
*
trans
;
int
i
,
j
,
leng
;
FormatSpec
*
f
;
TokenLink
*
tok
;
for
(
leng
=
0
,
tok
=
row
;
tok
;
leng
++
,
tok
=
tok
->
next
);
trans
=
new
int
[
leng
];
for
(
i
=
0
,
tok
=
row
;
tok
;
i
++
,
tok
=
tok
->
next
)
{
for
(
f
=
list
,
j
=
0
;
f
&&
(
!
f
->
name
||
strcmp
(
tok
->
token
,
f
->
name
));
f
=
f
->
next
,
j
++
);
trans
[
i
]
=
(
f
?
j
:
-
1
);
}
tl
=
leng
;
return
trans
;
}
void
Format
::
internal_guess
()
{
FormatSpec
*
ele
;
FormatSpec
*
last
;
FormatSpec
*
ele2
;
ele
=
list
;
last
=
0
;
while
(
ele
)
{
if
(
IsType
(
ele
,
FormatSpecUnknown
))
{
if
((
ele2
=
format_guess_unknown
((
FormatSpecUnknown
*
)
ele
)))
{
ele2
->
usurp
((
FormatSpecUnknown
*
)
ele
);
ele2
->
next
=
ele
->
next
;
if
(
last
)
last
->
next
=
ele2
;
else
list
=
ele2
;
delete
ele
;
ele
=
ele2
;
}
}
last
=
ele
;
ele
=
ele
->
next
;
}
}
int
Format
::
simple_construct
(
const
char
*
formcodes
)
{
FormatSpec
*
ele
;
FormatSpec
*
last
;
const
char
*
c
;
if
(
!
formcodes
)
{
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"No format string provided.
\n
"
);
}
return
0
;
}
last
=
0
;
for
(
c
=
formcodes
;
*
c
;
c
++
)
{
ele
=
format_create
(
*
c
);
if
(
!
list
)
last
=
list
=
ele
;
else
last
=
last
->
next
=
ele
;
len
+=
1
;
}
unset
=
0
;
return
1
;
}
int
Format
::
construct
(
const
char
*
name
)
{
FILE
*
f
;
int
linenum
=
0
;
TokenLink
*
row
;
TokenLink
*
frow
=
0
;
char
*
tmp
;
if
((
f
=
fopen
(
name
,
"r"
))
==
0
)
return
0
;
if
(
unset
)
{
frow
=
readtokens
(
f
);
linenum
+=
1
;
tmp
=
freezebuf
();
row
=
readtokens
(
f
);
linenum
+=
1
;
internal_construct
(
row
);
freetokens
(
row
);
unset
=
0
;
row
=
readtokens
(
f
);
linenum
+=
1
;
}
else
{
row
=
readtokens
(
f
);
linenum
+=
1
;
if
(
check_header
(
row
))
{
freetokens
(
row
);
row
=
readtokens
(
f
);
linenum
+=
1
;
}
}
while
(
row
)
{
internal_add
(
row
,
linenum
);
freetokens
(
row
);
row
=
readtokens
(
f
);
linenum
+=
1
;
}
internal_guess
();
if
(
frow
)
{
if
(
check_header
(
frow
))
extract_header
(
frow
);
else
internal_add
(
frow
,
1
);
freetokens
(
frow
);
delete
[]
tmp
;
}
fclose
(
f
);
return
1
;
}
C_code Trajectory clustering TBI/dataformat/format.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#ifndef FORMAT_HH
#define FORMAT_HH
#include <stdio.h>
#include <string.h>
#include "intfloat.hh"
#include "growable.hh"
enum
{
FORMATSPEC_NYI
=
-
1
,
FORMATSPEC_UNKNOWN
=
0
,
FORMATSPEC_BINARY
=
1
,
FORMATSPEC_DISCR
=
2
,
FORMATSPEC_CONT
=
3
,
FORMATSPEC_SYMBOL
=
4
};
class
FormatSpec
{
friend
class
Format
;
public:
FormatSpec
(
const
char
*
nm
=
0
);
virtual
~
FormatSpec
()
{
if
(
name
)
delete
[]
name
;
};
virtual
FormatSpec
*
copy
()
{
return
0
;
};
virtual
void
init
(
struct
TokenLink
*
row
)
{};
virtual
void
setname
(
const
char
*
nm
);
virtual
char
*
stripname
(
struct
TokenLink
*&
row
);
virtual
void
printname
(
FILE
*
f
);
virtual
void
add
(
const
char
*
str
)
{};
virtual
void
save
(
FILE
*
f
)
{};
virtual
intfloat
interpret
(
const
char
*
str
)
{
intfloat
r
;
r
.
i
=
-
1
;
return
r
;
};
virtual
const
char
*
represent
(
intfloat
v
)
{
return
"?"
;
};
virtual
int
get_discr
(
int
val
)
{
return
-
1
;
};
virtual
const
char
*
get_symbol
(
int
val
)
{
return
0
;
};
virtual
void
usurp
(
class
FormatSpecUnknown
*
ele
)
{};
virtual
int
type
()
{
return
FORMATSPEC_NYI
;
};
virtual
int
getnum
()
{
return
-
1
;
};
char
*
name
;
FormatSpec
*
next
;
int
unset
;
};
class
Format
{
public:
Format
();
~
Format
();
int
read
(
const
char
*
name
);
int
save
(
const
char
*
name
);
int
simple_construct
(
const
char
*
formcodes
);
int
construct
(
const
char
*
name
);
int
check_header
(
TokenLink
*
row
);
void
extract_header
(
TokenLink
*
row
);
int
*
get_translation
(
TokenLink
*
row
,
int
&
tl
);
void
add
(
FormatSpec
*
formspec
);
int
length
()
{
return
(
unset
?
-
1
:
len
);
};
FormatSpec
*
nth
(
int
n
)
{
FormatSpec
*
f
;
for
(
f
=
list
;
f
&&
n
;
n
--
,
f
=
f
->
next
);
return
f
;
};
FormatSpec
*
findcolumn
(
const
char
*
name
)
{
FormatSpec
*
f
;
for
(
f
=
list
;
f
&&
(
!
f
->
name
||
strcmp
(
name
,
f
->
name
));
f
=
f
->
next
);
return
f
;
};
int
findcolumnnr
(
const
char
*
name
)
{
int
n
;
FormatSpec
*
f
;
for
(
f
=
list
,
n
=
0
;
f
&&
(
!
f
->
name
||
strcmp
(
name
,
f
->
name
));
f
=
f
->
next
,
n
+=
1
);
return
n
;
};
void
insert_input
(
TokenLink
*
tok
,
intfloat
*
vec
);
void
insert_input_translated
(
TokenLink
*
tok
,
intfloat
*
vec
,
int
*
trans
,
int
tl
);
void
save_output
(
FILE
*
f
,
intfloat
*
vec
);
void
save_header
(
FILE
*
f
);
int
numsections
();
int
getsection
(
int
n
);
void
newsection
();
protected:
void
internal_construct
(
TokenLink
*
row
);
void
internal_add
(
TokenLink
*
row
,
int
ln
);
void
internal_guess
();
int
len
;
int
unset
;
FormatSpec
*
list
;
class
Growable
<
int
>
*
section_hints
;
};
extern
int
global_format_quiet
;
#endif
C_code Trajectory clustering TBI/dataformat/formatbinary.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "readtokens.hh"
#include "format.hh"
#include "formatbinary.hh"
/*
class FormatSpecBinary : public FormatSpec {
public:
FormatSpecBinary(const char* nm = 0) : FormatSpec(nm) {};
virtual FormatSpec* copy() { FormatSpecBinary* fs = new FormatSpecBinary(name); fs->unset = unset; return fs; };
virtual void init(struct TokenLink* tok);
virtual void add(const char* str);
virtual void save(FILE* f);
virtual intfloat interpret(const char* str);
virtual const char* represent(intfloat v);
virtual int type() { return FORMATSPEC_BINARY; };
};
*/
void
FormatSpecBinary
::
init
(
struct
TokenLink
*
tok
)
{
name
=
stripname
(
tok
);
if
(
tok
->
next
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many binary format arguments: %s ...
\n
"
,
tok
->
token
);
}
}
void
FormatSpecBinary
::
save
(
FILE
*
f
)
{
printname
(
f
);
fprintf
(
f
,
"binary:
\n
"
);
}
void
FormatSpecBinary
::
add
(
const
char
*
str
)
{
IF_FLOAT
val
;
if
(
is_unknown
(
str
))
;
else
if
(
!
is_cont
(
str
)
||
((
val
=
get_cont
(
str
))
<
0.0
)
||
(
val
>
1.0
))
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad binary value: %s
\n
"
,
str
);
}
}
intfloat
FormatSpecBinary
::
interpret
(
const
char
*
str
)
{
intfloat
res
;
if
(
is_unknown
(
str
))
res
.
i
=
-
1
;
else
if
(
is_cont
(
str
))
res
.
f
=
get_cont
(
str
);
else
{
res
.
i
=
-
1
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad binary value: %s
\n
"
,
str
);
}
}
return
res
;
}
const
char
*
FormatSpecBinary
::
represent
(
intfloat
v
)
{
static
char
buf
[
12
];
if
(
v
.
i
==
-
1
)
return
"?"
;
else
return
sprintf
(
buf
,
"%.5f"
,
v
.
f
),
buf
;
}
C_code Trajectory clustering TBI/dataformat/formatbinary.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
class
FormatSpecBinary
:
public
FormatSpec
{
public:
FormatSpecBinary
(
const
char
*
nm
=
0
)
:
FormatSpec
(
nm
)
{};
virtual
FormatSpec
*
copy
()
{
FormatSpecBinary
*
fs
=
new
FormatSpecBinary
(
name
);
fs
->
unset
=
unset
;
return
fs
;
};
virtual
void
init
(
struct
TokenLink
*
tok
);
virtual
void
add
(
const
char
*
str
);
virtual
void
save
(
FILE
*
f
);
virtual
intfloat
interpret
(
const
char
*
str
);
virtual
const
char
*
represent
(
intfloat
v
);
virtual
int
type
()
{
return
FORMATSPEC_BINARY
;
};
};
C_code Trajectory clustering TBI/dataformat/formatcont.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "readtokens.hh"
#include "format.hh"
#include "formatunknown.hh"
#include "formatcont.hh"
/*
class FormatSpecCont : public FormatSpec {
public:
FormatSpecCont(const char* nm = 0) : FormatSpec(nm) { min = 0.0; max = 0.0; };
virtual FormatSpec* copy() { FormatSpecCont* fs = new FormatSpecCont(name); fs->min = min; fs->max = max; fs->unset = unset; return fs; };
virtual void init(struct TokenLink* row);
virtual void add(const char* str);
virtual void save(FILE* f);
virtual intfloat interpret(const char* str);
virtual const char* represent(intfloat v);
virtual void usurp(FormatSpecUnknown* ele);
virtual int type() { return FORMATSPEC_CONT; };
float min;
float max;
};
*/
void
FormatSpecCont
::
init
(
struct
TokenLink
*
tok
)
{
name
=
stripname
(
tok
);
tok
=
tok
->
next
;
if
(
!
tok
)
;
else
if
(
tok
->
next
&&
!
tok
->
next
->
next
)
{
if
(
is_cont
(
tok
->
token
)
&&
is_cont
(
tok
->
next
->
token
))
{
unset
=
0
;
min
=
get_cont
(
tok
->
token
);
max
=
get_cont
(
tok
->
next
->
token
);
}
else
{
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Strange cont format arguments: %s %s
\n
"
,
tok
->
token
,
tok
->
next
->
token
);
}
}
}
else
{
if
(
!
tok
->
next
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad number of cont format arguments: %s
\n
"
,
tok
->
token
);
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad number of cont format arguments: %s %s %s ...
\n
"
,
tok
->
token
,
tok
->
next
->
token
,
tok
->
next
->
next
->
token
);
}
}
}
void
FormatSpecCont
::
save
(
FILE
*
f
)
{
printname
(
f
);
if
(
unset
)
fprintf
(
f
,
"cont:
\n
"
);
else
fprintf
(
f
,
"cont:
\t
%g
\t
%g
\n
"
,
min
,
max
);
}
void
FormatSpecCont
::
add
(
const
char
*
str
)
{
IF_FLOAT
val
;
if
(
is_unknown
(
str
))
;
else
if
(
is_cont
(
str
))
{
val
=
get_cont
(
str
);
if
(
unset
)
{
min
=
val
;
max
=
val
;
unset
=
0
;
}
else
if
(
val
<
min
)
{
min
=
val
;
}
else
if
(
val
>
max
)
{
max
=
val
;
}
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad cont value: %s
\n
"
,
str
);
}
}
intfloat
FormatSpecCont
::
interpret
(
const
char
*
str
)
{
intfloat
res
;
if
(
unset
)
{
unset
=
0
;
}
if
(
is_unknown
(
str
))
res
.
i
=
-
1
;
else
if
(
is_cont
(
str
))
res
.
f
=
get_cont
(
str
);
else
{
res
.
i
=
-
1
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad cont value: %s
\n
"
,
str
);
}
}
return
res
;
}
const
char
*
FormatSpecCont
::
represent
(
intfloat
v
)
{
static
char
buf
[
20
];
if
(
v
.
i
==
-
1
)
return
"?"
;
else
return
sprintf
(
buf
,
"%f"
,
v
.
f
),
buf
;
}
void
FormatSpecCont
::
usurp
(
FormatSpecUnknown
*
ele
)
{
if
(
!
ele
->
unset
)
{
if
(
unset
)
{
unset
=
0
;
min
=
ele
->
min
;
max
=
ele
->
max
;
}
else
{
if
(
min
>
ele
->
min
)
min
=
ele
->
min
;
if
(
max
<
ele
->
max
)
max
=
ele
->
max
;
}
}
}
C_code Trajectory clustering TBI/dataformat/formatcont.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
class
FormatSpecCont
:
public
FormatSpec
{
public:
FormatSpecCont
(
const
char
*
nm
=
0
)
:
FormatSpec
(
nm
)
{
min
=
0.0
;
max
=
0.0
;
};
virtual
FormatSpec
*
copy
()
{
FormatSpecCont
*
fs
=
new
FormatSpecCont
(
name
);
fs
->
min
=
min
;
fs
->
max
=
max
;
fs
->
unset
=
unset
;
return
fs
;
};
virtual
void
init
(
struct
TokenLink
*
row
);
virtual
void
add
(
const
char
*
str
);
virtual
void
save
(
FILE
*
f
);
virtual
intfloat
interpret
(
const
char
*
str
);
virtual
const
char
*
represent
(
intfloat
v
);
virtual
void
usurp
(
FormatSpecUnknown
*
ele
);
virtual
int
type
()
{
return
FORMATSPEC_CONT
;
};
float
min
;
float
max
;
};
C_code Trajectory clustering TBI/dataformat/formatdiscr.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "readtokens.hh"
#include "format.hh"
#include "formatunknown.hh"
#include "formatdiscr.hh"
/*
class FormatSpecDiscr : public FormatSpec {
public:
FormatSpecDiscr(const char* nm = 0) : FormatSpec(nm) { offset = 0; num = 0; dynamic = 0; };
virtual FormatSpec* copy() { FormatSpecDiscr* fs = new FormatSpecDiscr(name); fs->offset = offset; fs->num = num; fs->dynamic = dynamic; fs->unset = unset; return fs; };
virtual void init(struct TokenLink* tok);
virtual void add(const char* str);
virtual void save(FILE* f);
virtual intfloat interpret(const char* str);
virtual const char* represent(intfloat v);
virtual int get_discr(int val);
virtual void usurp(FormatSpecUnknown* ele);
virtual int type() { return FORMATSPEC_DISCR; };
virtual int getnum() { return num; };
IF_INT offset;
IF_INT num;
int dynamic;
};
*/
void
FormatSpecDiscr
::
init
(
struct
TokenLink
*
tok
)
{
name
=
stripname
(
tok
);
tok
=
tok
->
next
;
if
(
!
tok
)
;
else
if
(
!
tok
->
next
)
{
if
(
is_posint
(
tok
->
token
))
{
unset
=
0
;
offset
=
0
;
num
=
get_posint
(
tok
->
token
);
}
else
{
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Strange discr format argument: %s
\n
"
,
tok
->
token
);
}
}
}
else
if
(
!
tok
->
next
->
next
)
{
if
((
is_posint
(
tok
->
token
)
||
is_negint
(
tok
->
token
))
&&
is_posint
(
tok
->
next
->
token
))
{
unset
=
0
;
offset
=
get_posint
(
tok
->
token
);
num
=
get_posint
(
tok
->
next
->
token
)
-
offset
+
1
;
}
else
{
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Strange discr format arguments: %s %s
\n
"
,
tok
->
token
,
tok
->
next
->
token
);
}
}
}
else
{
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"To many discr format arguments: %s %s %s ...
\n
"
,
tok
->
token
,
tok
->
next
->
token
,
tok
->
next
->
next
->
token
);
}
}
}
void
FormatSpecDiscr
::
save
(
FILE
*
f
)
{
printname
(
f
);
if
(
unset
)
fprintf
(
f
,
"discr:
\n
"
);
else
if
(
offset
==
0
)
fprintf
(
f
,
"discr:
\t
%d
\n
"
,
num
);
else
fprintf
(
f
,
"discr:
\t
%d
\t
%d
\n
"
,
offset
,
num
+
offset
-
1
);
}
void
FormatSpecDiscr
::
add
(
const
char
*
str
)
{
IF_INT
val
;
if
(
is_unknown
(
str
))
;
else
if
(
is_posint
(
str
)
||
is_negint
(
str
))
{
val
=
get_posint
(
str
);
if
(
unset
)
{
offset
=
val
;
num
=
1
;
unset
=
0
;
}
else
if
(
val
<
offset
&&
!
dynamic
)
{
num
+=
(
offset
-
val
);
offset
=
val
;
}
else
if
(
val
>=
num
+
offset
)
{
num
=
val
-
offset
+
1
;
}
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad discr value: %s
\n
"
,
str
);
}
}
intfloat
FormatSpecDiscr
::
interpret
(
const
char
*
str
)
{
intfloat
res
;
if
(
unset
)
{
unset
=
0
;
dynamic
=
1
;
}
if
(
is_unknown
(
str
))
res
.
i
=
-
1
;
else
if
(
is_posint
(
str
)
||
(
is_negint
(
str
)
&&
offset
<
0
))
{
res
.
i
=
get_posint
(
str
)
-
offset
;
if
(
dynamic
&&
res
.
i
>=
num
)
num
=
res
.
i
+
1
;
}
else
{
res
.
i
=
-
1
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad discr value: %s
\n
"
,
str
);
}
}
return
res
;
}
const
char
*
FormatSpecDiscr
::
represent
(
intfloat
v
)
{
static
char
buf
[
12
];
if
(
v
.
i
==
-
1
)
return
"?"
;
else
return
sprintf
(
buf
,
"%lld"
,
(
long
long
)(
v
.
i
+
offset
)),
buf
;
}
int
FormatSpecDiscr
::
get_discr
(
int
val
)
{
return
val
+
offset
;
}
void
FormatSpecDiscr
::
usurp
(
FormatSpecUnknown
*
ele
)
{
if
(
!
ele
->
unset
)
{
if
(
unset
)
{
unset
=
0
;
offset
=
(
int
)
ele
->
min
;
num
=
(
int
)
ele
->
max
-
offset
+
1
;
}
else
{
if
(
offset
>
(
int
)
ele
->
min
)
num
+=
(
offset
-
(
int
)
ele
->
min
),
offset
=
(
int
)
ele
->
min
;
if
(
num
+
offset
<=
(
int
)
ele
->
max
)
num
=
(
int
)
ele
->
max
-
offset
+
1
;
}
}
}
C_code Trajectory clustering TBI/dataformat/formatdiscr.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
class
FormatSpecDiscr
:
public
FormatSpec
{
public:
FormatSpecDiscr
(
const
char
*
nm
=
0
)
:
FormatSpec
(
nm
)
{
offset
=
0
;
num
=
0
;
dynamic
=
0
;
};
virtual
FormatSpec
*
copy
()
{
FormatSpecDiscr
*
fs
=
new
FormatSpecDiscr
(
name
);
fs
->
offset
=
offset
;
fs
->
num
=
num
;
fs
->
dynamic
=
dynamic
;
fs
->
unset
=
unset
;
return
fs
;
};
virtual
void
init
(
struct
TokenLink
*
tok
);
virtual
void
add
(
const
char
*
str
);
virtual
void
save
(
FILE
*
f
);
virtual
intfloat
interpret
(
const
char
*
str
);
virtual
const
char
*
represent
(
intfloat
v
);
virtual
int
get_discr
(
int
val
);
virtual
void
usurp
(
FormatSpecUnknown
*
ele
);
virtual
int
type
()
{
return
FORMATSPEC_DISCR
;
};
virtual
int
getnum
()
{
return
num
;
};
int
offset
;
int
num
;
int
dynamic
;
};
C_code Trajectory clustering TBI/dataformat/formatdispatch.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <string.h>
#include "format.hh"
#include "readtokens.hh"
#include "formatbinary.hh"
#include "formatdiscr.hh"
#include "formatcont.hh"
#include "formatsymbol.hh"
#include "formattime.hh"
#include "formatunknown.hh"
#include "formatignore.hh"
#include "formatdispatch.hh"
FormatSpec
*
format_create
(
TokenLink
*
tok
)
{
if
(
*
tok
->
token
==
'"'
&&
tok
->
next
)
tok
=
tok
->
next
;
if
(
!
strcmp
(
tok
->
token
,
"binary:"
))
return
new
FormatSpecBinary
;
else
if
(
!
strcmp
(
tok
->
token
,
"discr:"
))
return
new
FormatSpecDiscr
;
else
if
(
!
strcmp
(
tok
->
token
,
"cont:"
))
return
new
FormatSpecCont
;
else
if
(
!
strcmp
(
tok
->
token
,
"symbol:"
))
return
new
FormatSpecSymbol
;
else
if
(
!
strcmp
(
tok
->
token
,
"date:"
)
||
!
strcmp
(
tok
->
token
,
"time:"
)
||
!
strcmp
(
tok
->
token
,
"datetime:"
)
||
!
strcmp
(
tok
->
token
,
"seconds:"
)
||
!
strcmp
(
tok
->
token
,
"millisec:"
))
return
new
FormatSpecDatetime
;
else
if
(
!
strcmp
(
tok
->
token
,
"ignored:"
))
return
new
FormatSpecIgnore
;
else
return
new
FormatSpecUnknown
;
}
FormatSpec
*
format_create
(
char
ch
)
{
FormatSpecDatetime
*
ret
;
if
(
ch
==
'b'
)
return
new
FormatSpecBinary
;
else
if
(
ch
==
'd'
)
return
new
FormatSpecDiscr
;
else
if
(
ch
==
'c'
)
return
new
FormatSpecCont
;
else
if
(
ch
==
's'
)
return
new
FormatSpecSymbol
;
else
if
(
ch
==
'D'
)
{
ret
=
new
FormatSpecDatetime
;
ret
->
dtformbits
=
2
;
ret
->
dtformspec
=
0
;
return
ret
;
}
else
if
(
ch
==
't'
){
ret
=
new
FormatSpecDatetime
;
ret
->
dtformbits
=
1
;
ret
->
dtformspec
=
0
;
return
ret
;
}
else
if
(
ch
==
'T'
)
{
ret
=
new
FormatSpecDatetime
;
ret
->
dtformbits
=
3
;
ret
->
dtformspec
=
0
;
return
ret
;
}
else
if
(
ch
==
'-'
)
return
new
FormatSpecIgnore
;
else
return
new
FormatSpecUnknown
;
}
FormatSpec
*
format_guess
(
char
*
str
,
int
force
)
{
IF_FLOAT
val
;
/*
int fmt;
*/
int
b
;
const
char
*
fs
;
if
(
is_unknown
(
str
)
||
is_posint
(
str
))
{
if
(
force
)
return
new
FormatSpecUnknown
;
else
return
0
;
}
else
if
(
is_cont
(
str
))
{
if
((
val
=
get_cont
(
str
))
<
0.0
||
val
>
1.0
)
return
new
FormatSpecCont
;
else
if
(
force
)
return
new
FormatSpecUnknown
;
else
return
0
;
/*
} else if ((fmt = check_datetime(str))) {
FormatSpecDatetime* ret;
ret = new FormatSpecDatetime;
ret->dtform = fmt;
return ret;
*/
}
else
if
(
check_datetime
(
str
,
27
,
b
,
fs
))
{
FormatSpecDatetime
*
ret
;
ret
=
new
FormatSpecDatetime
;
ret
->
dtformbits
=
b
;
ret
->
dtformspec
=
fs
;
return
ret
;
}
else
return
new
FormatSpecSymbol
;
}
FormatSpec
*
format_guess_unknown
(
class
FormatSpecUnknown
*
ele
)
{
if
(
ele
->
unset
)
return
0
;
else
if
((
ele
->
min
==
0.0
)
&&
(
ele
->
max
==
1.0
))
return
new
FormatSpecBinary
;
else
if
((
ele
->
min
<=
1.0
)
&&
(
ele
->
min
==
(
int
)
ele
->
min
)
&&
(
ele
->
max
==
(
int
)
ele
->
max
)
&&
(
ele
->
max
-
ele
->
min
<=
ele
->
num
))
return
new
FormatSpecDiscr
;
else
if
((
ele
->
max
-
ele
->
min
<
1.0
)
||
((
ele
->
max
-
ele
->
min
>=
20
)
&&
(
ele
->
num
>=
15
)))
return
new
FormatSpecCont
;
else
if
(
ele
->
num
<
15
)
return
new
FormatSpecSymbol
;
else
return
0
;
}
C_code Trajectory clustering TBI/dataformat/formatdispatch.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
FormatSpec
*
format_create
(
struct
TokenLink
*
tok
);
FormatSpec
*
format_create
(
char
ch
);
FormatSpec
*
format_guess
(
char
*
str
,
int
force
);
FormatSpec
*
format_guess_unknown
(
class
FormatSpecUnknown
*
ele
);
C_code Trajectory clustering TBI/dataformat/formatignore.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
class
FormatSpecIgnore
:
public
FormatSpec
{
public:
FormatSpecIgnore
(
const
char
*
nm
=
0
)
:
FormatSpec
(
nm
)
{};
virtual
~
FormatSpecIgnore
()
{};
virtual
FormatSpec
*
copy
()
{
return
new
FormatSpecIgnore
(
name
);
};
virtual
void
init
(
struct
TokenLink
*
tok
)
{
name
=
stripname
(
tok
);
};
virtual
void
add
(
const
char
*
str
)
{
};
virtual
void
save
(
FILE
*
f
)
{
printname
(
f
);
fprintf
(
f
,
"ignored:
\n
"
);
};
virtual
intfloat
interpret
(
const
char
*
str
)
{
intfloat
res
;
res
.
i
=
-
1
;
return
res
;
};
virtual
int
type
()
{
return
99
;
};
};
C_code Trajectory clustering TBI/dataformat/formatsymbol.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "readtokens.hh"
#include "table.hh"
#include "format.hh"
#include "formatunknown.hh"
#include "formatsymbol.hh"
/*
class FormatSpecSymbol : public FormatSpec {
public:
FormatSpecSymbol(const char* nm = 0);
virtual ~FormatSpecSymbol();
virtual FormatSpec* copy();
virtual void init(struct TokenLink* tok);
virtual void add(const char* str);
virtual void save(FILE* f);
virtual intfloat interpret(const char* str);
virtual const char* represent(intfloat v);
virtual const char* get_symbol(int val);
virtual void usurp(class FormatSpecUnknown* ele);
virtual int type() { return FORMATSPEC_SYMBOL; };
virtual int getnum() { return num; };
int num;
class IndexTable* table;
int dynamic;
};
*/
FormatSpecSymbol
::
FormatSpecSymbol
(
const
char
*
nm
)
:
FormatSpec
(
nm
)
{
num
=
0
;
table
=
new
IndexTable
;
dynamic
=
0
;
}
FormatSpecSymbol
::~
FormatSpecSymbol
()
{
delete
table
;
}
FormatSpec
*
FormatSpecSymbol
::
copy
()
{
FormatSpecSymbol
*
fs
=
new
FormatSpecSymbol
(
name
);
fs
->
num
=
num
;
delete
fs
->
table
;
fs
->
table
=
table
->
copy
();
fs
->
dynamic
=
dynamic
;
fs
->
unset
=
unset
;
return
fs
;
}
void
FormatSpecSymbol
::
init
(
struct
TokenLink
*
tok
)
{
name
=
stripname
(
tok
);
tok
=
tok
->
next
;
if
(
tok
)
{
unset
=
0
;
num
=
0
;
while
(
tok
)
{
table
->
insert_last
(
tok
->
token
);
num
++
;
tok
=
tok
->
next
;
}
}
}
void
FormatSpecSymbol
::
save
(
FILE
*
f
)
{
printname
(
f
);
if
(
unset
)
fprintf
(
f
,
"symbol:
\n
"
);
else
{
char
*
str
;
fprintf
(
f
,
"symbol:"
);
table
->
reset
();
while
((
str
=
table
->
next
()))
fprintf
(
f
,
"
\t
%s"
,
str
);
fprintf
(
f
,
"
\n
"
);
}
}
void
FormatSpecSymbol
::
add
(
const
char
*
str
)
{
if
(
unset
)
unset
=
0
;
if
(
is_unknown
(
str
))
;
else
if
(
table
->
get
(
str
)
<
0
)
{
table
->
insert
(
str
);
num
+=
1
;
}
}
intfloat
FormatSpecSymbol
::
interpret
(
const
char
*
str
)
{
intfloat
res
;
if
(
unset
)
{
unset
=
0
;
dynamic
=
1
;
}
if
(
is_unknown
(
str
))
res
.
i
=
-
1
;
else
{
res
.
i
=
table
->
get
(
str
);
if
(
res
.
i
==
-
1
)
{
if
(
dynamic
)
{
table
->
insert_last
(
str
);
res
.
i
=
num
++
;
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Unknown symbol: %s
\n
"
,
str
);
}
}
}
return
res
;
}
const
char
*
FormatSpecSymbol
::
represent
(
intfloat
v
)
{
if
(
v
.
i
==
-
1
)
return
"?"
;
else
return
table
->
nth
((
int
)
v
.
i
);
}
const
char
*
FormatSpecSymbol
::
get_symbol
(
int
val
)
{
return
table
->
nth
(
val
);
}
void
FormatSpecSymbol
::
usurp
(
FormatSpecUnknown
*
ele
)
{
char
*
sym
;
if
(
!
ele
->
unset
)
{
if
(
unset
)
{
unset
=
0
;
num
=
0
;
table
=
new
IndexTable
;
}
ele
->
table
->
reset
();
while
((
sym
=
ele
->
table
->
next
()))
if
(
table
->
get
(
sym
)
<
0
)
{
table
->
insert
(
sym
);
num
+=
1
;
}
}
}
C_code Trajectory clustering TBI/dataformat/formatsymbol.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
class
FormatSpecSymbol
:
public
FormatSpec
{
public:
FormatSpecSymbol
(
const
char
*
nm
=
0
);
virtual
~
FormatSpecSymbol
();
virtual
FormatSpec
*
copy
();
virtual
void
init
(
struct
TokenLink
*
tok
);
virtual
void
add
(
const
char
*
str
);
virtual
void
save
(
FILE
*
f
);
virtual
intfloat
interpret
(
const
char
*
str
);
virtual
const
char
*
represent
(
intfloat
v
);
virtual
const
char
*
get_symbol
(
int
val
);
virtual
void
usurp
(
class
FormatSpecUnknown
*
ele
);
virtual
int
type
()
{
return
FORMATSPEC_SYMBOL
;
};
virtual
int
getnum
()
{
return
num
;
};
int
num
;
class
IndexTable
*
table
;
int
dynamic
;
};
C_code Trajectory clustering TBI/dataformat/formattime.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "readtokens.hh"
#include "table.hh"
#include "format.hh"
#include "formattime.hh"
#include "formatunknown.hh"
#include "formattime.hh"
/*
// date: -- days from 2000-01-01 inklusive
// time: -- seconds since midnight
// datetime: -- seconds since 2000-01-01 00:00:00
// typical timeforms h:m:s.c h:m [AM/PM] (sommartid?)
// typical dateforms Yy-m-d m/d/Yy d-m-Y d M Yy
// All has optional format-argument, interpreted by strptime/strftime:
// date/time/datetime: [format] [first last]
class FormatSpecDatetime : public FormatSpec {
public:
FormatSpecDatetime(const char* nm = 0) : FormatSpec(nm) { dtformbits = 0; dtformspec = 0; first.i = last.i = -1; };
virtual ~FormatSpecDatetime() { if (dtformspec) delete [] dtformspec; };
virtual FormatSpec* copy() { FormatSpecDatetime* fs = new FormatSpecDatetime(name); fs->dtformbits = dtformbits; fs->dtformspec = (dtformspec ? strcpy(new char[strlen(dtformspec)+1], dtformspec): 0); fs->first = first; fs->last = last; fs->unset = unset; return fs; };
virtual void init(struct TokenLink* tok);
virtual void add(const char* str);
virtual void save(FILE* f);
virtual intfloat interpret(const char* str);
virtual const char* represent(intfloat v);
virtual void get_time(int val, int& h, int& mi, int& s);
virtual void get_date(int val, int& y, int& mo, int& d);
virtual void usurp(FormatSpecUnknown* ele);
virtual int type() { return 5; };
int dtformbits;
const char* dtformspec;
intfloat first, last;
};
extern int check_datetime(const char* str, int trybits, int& dtfbits, const char*& dtfspec);
extern int check_formated_datetime(const char*str, int dtfbits, const char* dtfspec);
*/
#define STARTYEAR 1970
static
int
chunk_seconds
(
const
char
*
str
,
int
&
has_ms
,
int
&
sec
,
int
&
ms
)
{
int
i
;
long
s
;
const
char
*
p
=
str
;
if
(
*
p
!=
'1'
)
return
0
;
for
(
i
=
0
;
i
<
15
&&
*
p
>=
'0'
&&
*
p
<=
'9'
;
i
++
,
p
++
);
if
(
*
p
||
(
i
!=
10
&&
i
!=
13
))
return
0
;
s
=
atol
(
str
);
if
(
i
==
13
)
{
has_ms
=
1
;
ms
=
s
%
1000
;
sec
=
s
/
1000
;
}
else
{
has_ms
=
0
;
ms
=
0
;
sec
=
s
;
}
return
1
;
}
static
int
validate_datetime_format
(
const
char
*
form
,
int
&
bits
)
{
// kolla att formatet finns och inneh�ller %
// generera str�ng fr�n l�mplig tid och l�s tillbaka, kolla om tiderna st�mmer
// returnera dtformbits beroende p� om tid och/eller datum finns
char
buf
[
80
];
size_t
n
;
const
char
*
tmp
;
struct
tm
tst1
;
struct
tm
tst2
;
int
ret
;
// 2001-02-03 04:05:06
tst1
.
tm_year
=
101
,
tst1
.
tm_mon
=
1
,
tst1
.
tm_mday
=
3
,
tst1
.
tm_hour
=
4
,
tst1
.
tm_min
=
5
,
tst1
.
tm_sec
=
6
,
tst1
.
tm_wday
=
-
1
,
tst1
.
tm_yday
=
-
1
,
tst1
.
tm_isdst
=
0
;
tst2
.
tm_year
=
0
,
tst2
.
tm_mon
=
0
,
tst2
.
tm_mday
=
0
,
tst2
.
tm_hour
=
0
,
tst2
.
tm_min
=
0
,
tst2
.
tm_sec
=
0
,
tst2
.
tm_wday
=
-
1
,
tst2
.
tm_yday
=
-
1
,
tst2
.
tm_isdst
=
0
;
n
=
strftime
(
buf
,
80
,
form
,
&
tst1
);
if
(
!
n
)
return
0
;
tmp
=
0
;
//strptime(buf, form, &tst2);
if
(
!
tmp
)
return
0
;
ret
=
0
;
if
(
tst1
.
tm_hour
==
tst2
.
tm_hour
&&
tst1
.
tm_min
==
tst2
.
tm_min
&&
(
tst1
.
tm_sec
==
tst2
.
tm_sec
||
tst2
.
tm_sec
==
0
))
ret
|=
5
;
if
(
tst1
.
tm_year
==
tst2
.
tm_year
&&
tst1
.
tm_mon
==
tst2
.
tm_mon
&&
(
tst1
.
tm_mday
==
tst2
.
tm_mday
||
(
ret
==
0
&&
tst2
.
tm_mday
==
0
)))
ret
|=
6
;
bits
=
ret
;
return
1
;
}
static
int
chunk_formated_datetime
(
const
char
*
str
,
int
bits
,
const
char
*
form
,
int
&
year
,
int
&
month
,
int
&
day
,
int
&
hour
,
int
&
min
,
int
&
sec
)
{
const
char
*
tmp
;
struct
tm
tst
;
tst
.
tm_year
=
0
,
tst
.
tm_mon
=
0
,
tst
.
tm_mday
=
1
,
tst
.
tm_hour
=
0
,
tst
.
tm_min
=
0
,
tst
.
tm_sec
=
0
,
tst
.
tm_wday
=
-
1
,
tst
.
tm_yday
=
-
1
,
tst
.
tm_isdst
=
0
;
tmp
=
0
;
//strptime(str, form, &tst);
if
(
!
tmp
)
return
0
;
if
(
bits
&
1
)
{
hour
=
tst
.
tm_hour
,
min
=
tst
.
tm_min
,
sec
=
tst
.
tm_sec
;
}
if
(
bits
&
2
)
{
year
=
tst
.
tm_year
-
(
STARTYEAR
-
1900
),
month
=
tst
.
tm_mon
+
1
,
day
=
tst
.
tm_mday
;
}
return
1
;
}
static
char
*
string_formated_datetime
(
char
*
buf
,
int
bits
,
const
char
*
form
,
int
year
,
int
month
,
int
day
,
int
hour
,
int
min
,
int
sec
)
{
size_t
n
;
struct
tm
tst
;
if
(
bits
&
1
)
{
tst
.
tm_hour
=
hour
,
tst
.
tm_min
=
min
,
tst
.
tm_sec
=
sec
;
}
else
{
tst
.
tm_hour
=
0
,
tst
.
tm_min
=
0
,
tst
.
tm_sec
=
0
;
}
if
(
bits
&
2
)
{
tst
.
tm_year
=
year
+
(
STARTYEAR
-
1900
),
tst
.
tm_mon
=
month
-
1
,
tst
.
tm_mday
=
day
;
}
else
{
tst
.
tm_year
=
0
,
tst
.
tm_mon
=
0
,
tst
.
tm_mday
=
0
;
}
tst
.
tm_wday
=
-
1
,
tst
.
tm_yday
=
-
1
,
tst
.
tm_isdst
=
0
;
n
=
strftime
(
buf
,
80
,
form
,
&
tst
);
return
buf
+
n
;
}
static
int
check_datetime_internal_match
(
const
char
*&
str
,
const
char
*
form
,
char
*&
dtfspec
)
{
const
char
*
tmp
;
struct
tm
tst
;
tmp
=
0
;
// strptime(str, form, &tst);
if
(
!
tmp
)
return
0
;
str
=
tmp
;
strcpy
(
dtfspec
,
form
);
dtfspec
+=
strlen
(
form
);
return
1
;
}
static
int
check_datetime_internal_transfer
(
const
char
*&
str
,
char
*&
dtfspec
,
int
begin
,
int
simulate
)
{
// Move verbatim characters in ranges 33-47, 58-63, 91-96, 123-126
// Runs of spaces are replaced by one space, or zero at the end and beginning
// More than 12 separator characters in a run (in format) is considered unreasonable
const
char
*
p
;
char
*
q
;
int
n
,
m
,
inspace
=
0
;
for
(
p
=
str
,
n
=
0
,
m
=
0
;
m
<=
12
&&
((
*
p
>=
32
&&
*
p
<=
47
)
||
(
*
p
>=
58
&&
*
p
<=
63
)
||
(
*
p
>=
91
&&
*
p
<=
96
)
||
(
*
p
>=
123
&&
*
p
<=
126
));
n
++
,
p
++
)
if
(
*
p
==
32
)
{
if
(
!
inspace
)
{
inspace
=
1
;
}
}
else
{
if
(
inspace
)
{
inspace
=
0
;
if
(
!
(
begin
&&
m
==
0
))
m
++
;
}
m
++
;
}
if
(
inspace
&&
(
*
p
!=
0
)
&&
!
(
begin
&&
m
==
0
))
m
++
;
if
(
m
>
12
||
m
==
0
)
return
0
;
if
(
simulate
)
{
str
+=
n
;
dtfspec
+=
m
;
return
1
;
}
else
{
inspace
=
0
;
for
(
p
=
str
,
q
=
dtfspec
;
n
;
n
--
,
p
++
)
if
(
*
p
==
32
)
{
if
(
!
inspace
)
{
inspace
=
1
;
}
}
else
{
if
(
inspace
)
{
inspace
=
0
;
if
(
!
(
begin
&&
m
==
0
))
*
q
++
=
32
;
}
*
q
++
=
*
p
;
}
if
(
inspace
&&
(
*
p
!=
0
)
&&
!
(
begin
&&
q
==
dtfspec
))
*
q
++
=
32
;
*
q
=
0
;
str
=
p
;
dtfspec
=
q
;
return
1
;
}
}
// Checks a number of standard date and time formats, and if it finds a match returns the
// format that matched
int
check_datetime
(
const
char
*
str
,
int
trybits
,
int
&
dtfbits
,
const
char
*&
dtfspec
)
{
// l�tsas flytta �ver lite icke-alphanumeriska
// while 1
// bryt om inte hittat datum och andra varvet
// om vi ska kolla klockslag:
// bryt om redan hittat klockslag
// kolla lite klockslagsformat och flytta �ver
// om hittat, flytta �ver lite icke-alphanumeriska
// bryt om andra varvet
// om vi ska kolla datum:
// kolla lite datumformat
// om hittat, flytta �ver lite icke-alphanumeriska
// s�tt andra varvet
// om n�got hittat, flytta inledande alphanumeriska
//
// %Y-%m-%d %y-%m-%d %Y%m%d %y%m%d
// %m/%d/%Y %m/%d/%y
// %H:%M:%S %H:%M
const
char
*
p
=
str
;
int
leadin
,
first
,
found_time
,
found_date
,
has_ms
,
d2
,
d3
;
char
*
formspec
,
*
fs
;
if
(
trybits
&
3
)
{
formspec
=
fs
=
new
char
[
80
];
leadin
=
check_datetime_internal_transfer
(
p
,
fs
,
1
,
1
);
found_date
=
0
;
found_time
=
0
;
first
=
1
;
// Some spagetti-code here, all to avoid writing the formats several times
while
(
1
)
{
if
(
!
found_date
&&
!
first
)
break
;
if
(
trybits
&
1
)
{
if
(
found_time
)
break
;
if
(
check_datetime_internal_match
(
p
,
"%I:%M:%S %p"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%I:%M %p"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%H:%M:%S"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%H:%M"
,
fs
))
{
found_time
=
1
;
if
(
fs
[
-
1
]
==
'S'
&&
*
p
==
'.'
)
{
// Hack to get rid of fraction part of seconds
p
++
;
while
(
*
p
>=
'0'
&&
*
p
<=
'9'
)
p
++
;
}
else
check_datetime_internal_transfer
(
p
,
fs
,
0
,
0
);
}
}
if
(
!
first
)
break
;
if
(
trybits
&
2
)
{
if
(
found_date
)
break
;
if
(
check_datetime_internal_match
(
p
,
"%y-%m-%d"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%Y-%m-%d"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%m/%d/%y"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%m/%d/%Y"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%d/%m %Y"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%b %d %Y"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%d %b %Y"
,
fs
))
{
found_date
=
1
;
check_datetime_internal_transfer
(
p
,
fs
,
0
,
0
);
}
}
first
=
0
;
}
if
((
*
p
==
0
)
&&
(
found_time
||
found_date
))
{
if
(
leadin
)
check_datetime_internal_transfer
(
str
,
formspec
,
1
,
0
);
dtfspec
=
formspec
;
dtfbits
=
(
found_time
?
1
:
0
)
|
(
found_date
?
2
:
0
);
return
1
;
}
else
{
delete
[]
formspec
;
}
}
if
(
trybits
&
24
)
{
if
(
chunk_seconds
(
str
,
has_ms
,
d2
,
d3
))
{
dtfspec
=
0
;
dtfbits
=
(
has_ms
?
16
:
8
);
return
1
;
}
}
return
0
;
}
int
check_formated_datetime
(
const
char
*
str
,
int
dtfbits
,
const
char
*
dtfspec
)
{
const
char
*
p
;
int
has_ms
,
d2
,
d3
,
b
;
struct
tm
tst
;
if
(
dtfbits
&
3
)
{
p
=
0
;
//strptime(str, dtfspec, &tst);
if
(
!
p
)
return
0
;
if
(
dtfspec
[
strlen
(
dtfspec
)
-
1
]
==
'S'
&&
*
p
==
'.'
)
{
// Hack to get rid of fraction part of seconds
p
++
;
while
(
*
p
>=
'0'
&&
*
p
<=
'9'
)
p
++
;
}
while
(
*
p
==
32
)
p
++
;
if
(
*
p
)
return
0
;
}
else
if
(
dtfbits
&
24
)
{
b
=
chunk_seconds
(
str
,
has_ms
,
d2
,
d3
);
if
(
!
b
||
dtfbits
!=
(
has_ms
?
16
:
8
))
return
0
;
}
else
return
0
;
return
1
;
}
void
FormatSpecDatetime
::
init
(
struct
TokenLink
*
tok
)
{
int
b
;
const
char
*
fs
;
name
=
stripname
(
tok
);
if
(
!
strcmp
(
tok
->
token
,
"date:"
))
dtformbits
=
2
;
else
if
(
!
strcmp
(
tok
->
token
,
"time:"
))
dtformbits
=
1
;
else
if
(
!
strcmp
(
tok
->
token
,
"seconds:"
))
dtformbits
=
8
;
else
if
(
!
strcmp
(
tok
->
token
,
"millisec:"
))
dtformbits
=
16
;
else
dtformbits
=
3
;
dtformspec
=
0
;
tok
=
tok
->
next
;
if
(
!
tok
)
// No arguments, we are done
;
else
if
(
!
tok
->
next
)
{
// format
if
(
dtformbits
&
24
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too few %s format arguments: %s
\n
"
,
(
dtformbits
&
8
?
"seconds"
:
"millisec"
),
tok
->
token
);
}
else
if
(
validate_datetime_format
(
tok
->
token
,
b
)
&&
(
b
&
3
)
==
dtformbits
)
{
dtformbits
=
b
;
dtformspec
=
strcpy
(
new
char
[
strlen
(
tok
->
token
)
+
1
],
tok
->
token
);
unset
=
0
;
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad %s format form specification argument: %s
\n
"
,
(
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
);
}
}
else
if
(
!
tok
->
next
->
next
)
{
// first last
fs
=
0
;
if
(
check_datetime
(
tok
->
token
,
dtformbits
,
b
,
fs
)
&&
(
b
&
27
)
==
dtformbits
&&
check_formated_datetime
(
tok
->
next
->
token
,
b
,
fs
))
{
dtformbits
=
b
;
dtformspec
=
fs
;
unset
=
0
;
first
=
interpret
(
tok
->
token
);
last
=
interpret
(
tok
->
next
->
token
);
}
else
{
if
(
fs
)
delete
[]
fs
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Strange %s format arguments: %s %s
\n
"
,
(
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
,
tok
->
next
->
token
);
}
}
}
else
if
(
!
tok
->
next
->
next
->
next
)
{
// format first last
if
(
dtformbits
&
24
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many %s format arguments: %s %s %s
\n
"
,
(
dtformbits
&
8
?
"seconds"
:
"millisec"
),
tok
->
token
,
tok
->
next
->
token
,
tok
->
next
->
next
->
token
);
}
else
if
(
validate_datetime_format
(
tok
->
token
,
b
)
&&
(
b
&
3
)
==
dtformbits
)
{
if
(
check_formated_datetime
(
tok
->
next
->
token
,
b
,
tok
->
token
)
&&
check_formated_datetime
(
tok
->
next
->
next
->
token
,
b
,
tok
->
token
))
{
dtformbits
=
b
;
dtformspec
=
strcpy
(
new
char
[
strlen
(
tok
->
token
)
+
1
],
tok
->
token
);
unset
=
0
;
first
=
interpret
(
tok
->
next
->
token
);
last
=
interpret
(
tok
->
next
->
next
->
token
);
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Non-matching %s format arguments: %s %s %s
\n
"
,
(
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
,
tok
->
next
->
token
,
tok
->
next
->
next
->
token
);
}
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad %s format form specification argument: %s
\n
"
,
(
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
);
}
}
else
{
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many %s format arguments: %s %s %s %s ...
\n
"
,
(
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
,
tok
->
next
->
token
,
tok
->
next
->
next
->
token
,
tok
->
next
->
next
->
next
->
token
);
}
}
}
void
FormatSpecDatetime
::
save
(
FILE
*
f
)
{
const
char
*
tp
=
(
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
);
printname
(
f
);
if
(
first
.
i
==
-
1
||
last
.
i
==
-
1
)
{
if
(
dtformbits
&
4
&&
dtformspec
)
fprintf
(
f
,
"%s:
\t
%s
\n
"
,
tp
,
dtformspec
);
else
fprintf
(
f
,
"%s:
\n
"
,
tp
);
}
else
{
fprintf
(
f
,
"%s:"
,
tp
);
if
(
dtformbits
&
4
&&
dtformspec
)
fprintf
(
f
,
"
\t
%s"
,
dtformspec
);
fprintf
(
f
,
"
\t
%s"
,
represent
(
first
));
fprintf
(
f
,
"
\t
%s
\n
"
,
represent
(
last
));
}
}
void
FormatSpecDatetime
::
add
(
const
char
*
str
)
{
intfloat
val
;
if
(
is_unknown
(
str
))
return
;
val
=
interpret
(
str
);
if
(
val
.
i
!=
-
1
)
{
if
(
val
.
i
<
first
.
i
||
first
.
i
==
-
1
)
first
=
val
;
if
(
val
.
i
>
last
.
i
)
last
=
val
;
}
}
intfloat
FormatSpecDatetime
::
interpret
(
const
char
*
str
)
{
int
b
;
const
char
*
fs
;
intfloat
res
;
int
ok
=
1
;
int
y
,
mo
,
d
,
h
,
mi
,
s
,
dnum
,
leap
,
nleaps
,
sec
,
ms
,
has_ms
;
int
mv1
[]
=
{
0
,
31
,
59
,
90
,
120
,
151
,
181
,
212
,
243
,
273
,
304
,
334
};
int
mv2
[]
=
{
0
,
31
,
60
,
91
,
121
,
152
,
182
,
213
,
244
,
274
,
305
,
335
};
if
(
is_unknown
(
str
))
{
res
.
i
=
-
1
;
return
res
;
}
if
(
unset
)
{
fs
=
0
;
if
(
check_datetime
(
str
,
dtformbits
,
b
,
fs
)
&&
(
b
&
27
)
==
dtformbits
)
{
dtformbits
=
b
;
dtformspec
=
fs
;
unset
=
0
;
}
else
{
if
(
fs
)
delete
[]
fs
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Unrecognized %s form: %s
\n
"
,
(
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
str
);
}
res
.
i
=
-
1
;
return
res
;
}
}
if
(
dtformbits
&
24
)
{
ok
=
chunk_seconds
(
str
,
has_ms
,
sec
,
ms
);
if
(
ok
)
res
.
i
=
sec
+
(
has_ms
&&
ms
>
500
?
1
:
0
);
}
else
{
ok
=
chunk_formated_datetime
(
str
,
dtformbits
,
dtformspec
,
y
,
mo
,
d
,
h
,
mi
,
s
);
if
(
ok
&&
(
dtformbits
&
2
))
{
if
(
y
<
0
||
y
>
67
||
mo
<
1
||
mo
>
12
||
d
<
1
||
d
>
31
)
ok
=
0
;
else
{
leap
=
((
y
+
STARTYEAR
)
%
4
==
0
);
nleaps
=
(
y
+
((
STARTYEAR
+
3
)
%
4
))
/
4
;
// 3,0,1,2
dnum
=
d
+
(
leap
?
mv2
:
mv1
)[
mo
-
1
]
+
y
*
365
+
nleaps
-
1
;
}
}
if
(
ok
&&
(
dtformbits
&
1
))
{
if
(
h
<
0
||
h
>
23
||
mi
<
0
||
mi
>
59
||
s
<
0
||
s
>
59
)
ok
=
0
;
else
sec
=
h
*
3600
+
mi
*
60
+
s
;
}
if
(
ok
)
res
.
i
=
(
dtformbits
&
2
?
(
dtformbits
&
1
?
dnum
*
86400
+
sec
:
dnum
)
:
sec
);
}
if
(
!
ok
)
{
res
.
i
=
-
1
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad %s value: %s
\n
"
,
(
dtformbits
&
6
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
"time"
),
str
);
}
}
return
res
;
}
const
char
*
FormatSpecDatetime
::
represent
(
intfloat
v
)
{
static
char
buf
[
80
];
int
y
,
mo
,
d
,
h
,
mi
,
s
;
if
(
v
.
i
==
-
1
)
return
"?"
;
if
(
dtformbits
&
24
)
{
if
(
dtformbits
&
16
)
sprintf
(
buf
,
"%lld000"
,
(
long
long
)
v
.
i
);
// Not really milliseconds yet, fix it!!!
else
sprintf
(
buf
,
"%d"
,
(
int
)
v
.
i
);
}
else
{
get_date
(
v
.
i
,
y
,
mo
,
d
);
get_time
(
v
.
i
,
h
,
mi
,
s
);
string_formated_datetime
(
buf
,
dtformbits
,
dtformspec
,
y
,
mo
,
d
,
h
,
mi
,
s
);
}
return
buf
;
}
void
FormatSpecDatetime
::
get_time
(
int
val
,
int
&
h
,
int
&
mi
,
int
&
s
)
{
int
sec
;
if
(
!
(
dtformbits
&
1
))
{
h
=
mi
=
s
=
0
;
}
else
{
sec
=
val
%
86400
;
h
=
sec
/
3600
,
mi
=
(
sec
/
60
)
%
60
,
s
=
sec
%
60
;
}
}
void
FormatSpecDatetime
::
get_date
(
int
val
,
int
&
y
,
int
&
mo
,
int
&
d
)
{
int
dnum
,
leap
;
int
mv1
[]
=
{
0
,
31
,
59
,
90
,
120
,
151
,
181
,
212
,
243
,
273
,
304
,
334
};
int
mv2
[]
=
{
0
,
31
,
60
,
91
,
121
,
152
,
182
,
213
,
244
,
274
,
305
,
335
};
if
(
!
(
dtformbits
&
2
))
{
y
=
mo
=
d
=
0
;
}
else
{
if
(
dtformbits
&
1
)
dnum
=
val
/
86400
;
else
dnum
=
val
;
leap
=
((
STARTYEAR
)
%
4
==
0
);
for
(
y
=
0
;
dnum
>=
(
leap
?
366
:
365
);
dnum
-=
(
leap
?
366
:
365
),
y
++
)
leap
=
((
y
+
STARTYEAR
)
%
4
==
0
);
for
(
mo
=
12
;
dnum
<
(
leap
?
mv2
:
mv1
)[
mo
-
1
];
mo
--
);
dnum
-=
(
leap
?
mv2
:
mv1
)[
mo
-
1
];
d
=
dnum
+
1
;
}
}
void
FormatSpecDatetime
::
usurp
(
FormatSpecUnknown
*
ele
)
{
char
*
str
;
const
char
*
dtfspec
;
int
dtfbits
;
if
(
!
ele
->
unset
)
{
ele
->
table
->
reset
();
while
((
str
=
ele
->
table
->
next
()))
{
if
(
unset
&&
check_datetime
(
str
,
27
,
dtfbits
,
dtfspec
))
{
dtformbits
=
dtfbits
;
dtformspec
=
dtfspec
;
unset
=
0
;
}
add
(
str
);
}
}
}
C_code Trajectory clustering TBI/dataformat/formattime.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#ifndef FORMAT_TIME_HH
#define FORMAT_TIME_HH
class
FormatSpecDatetime
:
public
FormatSpec
{
public:
FormatSpecDatetime
(
const
char
*
nm
=
0
)
:
FormatSpec
(
nm
)
{
dtformbits
=
0
;
dtformspec
=
0
;
first
.
i
=
last
.
i
=
-
1
;
};
virtual
~
FormatSpecDatetime
()
{
if
(
dtformspec
)
delete
[]
dtformspec
;
};
virtual
FormatSpec
*
copy
()
{
FormatSpecDatetime
*
fs
=
new
FormatSpecDatetime
(
name
);
fs
->
dtformbits
=
dtformbits
;
fs
->
dtformspec
=
(
dtformspec
?
strcpy
(
new
char
[
strlen
(
dtformspec
)
+
1
],
dtformspec
)
:
0
);
fs
->
first
=
first
;
fs
->
last
=
last
;
fs
->
unset
=
unset
;
return
fs
;
};
virtual
void
init
(
struct
TokenLink
*
tok
);
virtual
void
add
(
const
char
*
str
);
virtual
void
save
(
FILE
*
f
);
virtual
intfloat
interpret
(
const
char
*
str
);
virtual
const
char
*
represent
(
intfloat
v
);
virtual
void
get_time
(
int
val
,
int
&
h
,
int
&
mi
,
int
&
s
);
virtual
void
get_date
(
int
val
,
int
&
y
,
int
&
mo
,
int
&
d
);
virtual
void
usurp
(
FormatSpecUnknown
*
ele
);
virtual
int
type
()
{
return
5
;
};
int
dtformbits
;
const
char
*
dtformspec
;
intfloat
first
,
last
;
};
extern
int
check_datetime
(
const
char
*
str
,
int
trybits
,
int
&
dtfbits
,
const
char
*&
dtfspec
);
extern
int
check_formated_datetime
(
const
char
*
str
,
int
dtfbits
,
const
char
*
dtfspec
);
#endif
C_code Trajectory clustering TBI/dataformat/formattime_old.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _XOPEN_SOURCE
#include <time.h>
#include "readtokens.hh"
#include "table.hh"
#include "format.hh"
#include "formattime.hh"
#include "formatunknown.hh"
#include "formattime.hh"
/*
// date: -- days from 2000-01-01 inklusive
// time: -- seconds since midnight
// datetime: -- seconds since 2000-01-01 00:00:00
// typical timeforms h:m:s.c h:m [AM/PM] (sommartid?)
// typical dateforms Yy-m-d m/d/Yy d-m-Y d M Yy
// All has optional format-argument, interpreted by strptime/strftime:
// date/time/datetime: [format] [first last]
class FormatSpecDatetime : public FormatSpec {
public:
FormatSpecDatetime(const char* nm = 0) : FormatSpec(nm) { dtformbits = 0; dtformspec = 0; first.i = last.i = -1; };
virtual ~FormatSpecDatetime() { if (dtformspec) delete [] dtformspec; };
virtual FormatSpec* copy() { FormatSpecDatetime* fs = new FormatSpecDatetime(name); fs->dtformbits = dtformbits; fs->dtformspec = (dtformspec ? strcpy(new char[strlen(dtformspec)+1], dtformspec): 0); fs->first = first; fs->last = last; fs->unset = unset; return fs; };
virtual void init(struct TokenLink* tok);
virtual void add(const char* str);
virtual void save(FILE* f);
virtual intfloat interpret(const char* str);
virtual const char* represent(intfloat v);
virtual void get_time(int val, int& h, int& mi, int& s);
virtual void get_date(int val, int& y, int& mo, int& d);
virtual void usurp(FormatSpecUnknown* ele);
virtual int type() { return 5; };
int dtformbits;
const char* dtformspec;
intfloat first, last;
};
extern int check_datetime(const char* str, int trybits, int& dtfbits, const char*& dtfspec);
extern int check_formated_datetime(const char*str, int dtfbits, const char* dtfspec);
*/
#define STARTYEAR 1970
static
int
chunk_seconds
(
const
char
*
str
,
int
&
has_ms
,
int
&
sec
,
int
&
ms
)
{
int
i
;
long
s
;
const
char
*
p
=
str
;
if
(
*
p
!=
'1'
)
return
0
;
for
(
i
=
0
;
i
<
15
&&
*
p
>=
'0'
&&
*
p
<=
'9'
;
i
++
,
p
++
);
if
(
*
p
||
(
i
!=
10
&&
i
!=
13
))
return
0
;
s
=
atol
(
str
);
if
(
i
==
13
)
{
has_ms
=
1
;
ms
=
s
%
1000
;
sec
=
s
/
1000
;
}
else
{
has_ms
=
0
;
ms
=
0
;
sec
=
s
;
}
return
1
;
}
static
int
validate_datetime_format
(
const
char
*
form
,
int
&
bits
)
{
// kolla att formatet finns och inneh�ller %
// generera str�ng fr�n l�mplig tid och l�s tillbaka, kolla om tiderna st�mmer
// returnera dtformbits beroende p� om tid och/eller datum finns
char
buf
[
80
];
size_t
n
;
const
char
*
tmp
;
struct
tm
tst1
;
struct
tm
tst2
;
int
ret
;
// 2001-02-03 04:05:06
tst1
.
tm_year
=
101
,
tst1
.
tm_mon
=
1
,
tst1
.
tm_mday
=
3
,
tst1
.
tm_hour
=
4
,
tst1
.
tm_min
=
5
,
tst1
.
tm_sec
=
6
,
tst1
.
tm_wday
=
-
1
,
tst1
.
tm_yday
=
-
1
,
tst1
.
tm_isdst
=
0
;
tst2
.
tm_year
=
0
,
tst2
.
tm_mon
=
0
,
tst2
.
tm_mday
=
0
,
tst2
.
tm_hour
=
0
,
tst2
.
tm_min
=
0
,
tst2
.
tm_sec
=
0
,
tst2
.
tm_wday
=
-
1
,
tst2
.
tm_yday
=
-
1
,
tst2
.
tm_isdst
=
0
;
n
=
strftime
(
buf
,
80
,
form
,
&
tst1
);
if
(
!
n
)
return
0
;
tmp
=
0
;
//strptime(buf, form, &tst2);
if
(
!
tmp
)
return
0
;
ret
=
0
;
if
(
tst1
.
tm_hour
==
tst2
.
tm_hour
&&
tst1
.
tm_min
==
tst2
.
tm_min
&&
(
tst1
.
tm_sec
==
tst2
.
tm_sec
||
tst2
.
tm_sec
==
0
))
ret
|=
5
;
if
(
tst1
.
tm_year
==
tst2
.
tm_year
&&
tst1
.
tm_mon
==
tst2
.
tm_mon
&&
(
tst1
.
tm_mday
==
tst2
.
tm_mday
||
(
ret
==
0
&&
tst2
.
tm_mday
==
0
)))
ret
|=
6
;
bits
=
ret
;
return
1
;
}
static
int
chunk_formated_datetime
(
const
char
*
str
,
int
bits
,
const
char
*
form
,
int
&
year
,
int
&
month
,
int
&
day
,
int
&
hour
,
int
&
min
,
int
&
sec
)
{
const
char
*
tmp
;
struct
tm
tst
;
tst
.
tm_year
=
0
,
tst
.
tm_mon
=
0
,
tst
.
tm_mday
=
1
,
tst
.
tm_hour
=
0
,
tst
.
tm_min
=
0
,
tst
.
tm_sec
=
0
,
tst
.
tm_wday
=
-
1
,
tst
.
tm_yday
=
-
1
,
tst
.
tm_isdst
=
0
;
tmp
=
0
;
//strptime(str, form, &tst);
if
(
!
tmp
)
return
0
;
if
(
bits
&
1
)
{
hour
=
tst
.
tm_hour
,
min
=
tst
.
tm_min
,
sec
=
tst
.
tm_sec
;
}
if
(
bits
&
2
)
{
year
=
tst
.
tm_year
-
(
STARTYEAR
-
1900
),
month
=
tst
.
tm_mon
+
1
,
day
=
tst
.
tm_mday
;
}
return
1
;
}
static
char
*
string_formated_datetime
(
char
*
buf
,
int
bits
,
const
char
*
form
,
int
year
,
int
month
,
int
day
,
int
hour
,
int
min
,
int
sec
)
{
size_t
n
;
struct
tm
tst
;
if
(
bits
&
1
)
{
tst
.
tm_hour
=
hour
,
tst
.
tm_min
=
min
,
tst
.
tm_sec
=
sec
;
}
else
{
tst
.
tm_hour
=
0
,
tst
.
tm_min
=
0
,
tst
.
tm_sec
=
0
;
}
if
(
bits
&
2
)
{
tst
.
tm_year
=
year
+
(
STARTYEAR
-
1900
),
tst
.
tm_mon
=
month
-
1
,
tst
.
tm_mday
=
day
;
}
else
{
tst
.
tm_year
=
0
,
tst
.
tm_mon
=
0
,
tst
.
tm_mday
=
0
;
}
tst
.
tm_wday
=
-
1
,
tst
.
tm_yday
=
-
1
,
tst
.
tm_isdst
=
0
;
n
=
strftime
(
buf
,
80
,
form
,
&
tst
);
return
buf
+
n
;
}
static
int
check_datetime_internal_match
(
const
char
*&
str
,
const
char
*
form
,
char
*&
dtfspec
)
{
const
char
*
tmp
;
struct
tm
tst
;
tmp
=
0
;
// strptime(str, form, &tst);
if
(
!
tmp
)
return
0
;
str
=
tmp
;
strcpy
(
dtfspec
,
form
);
dtfspec
+=
strlen
(
form
);
return
1
;
}
static
int
check_datetime_internal_transfer
(
const
char
*&
str
,
char
*&
dtfspec
,
int
begin
,
int
simulate
)
{
// Move verbatim characters in ranges 33-47, 58-63, 91-96, 123-126
// Runs of spaces are replaced by one space, or zero at the end and beginning
// More than 12 separator characters in a run (in format) is considered unreasonable
const
char
*
p
;
char
*
q
;
int
n
,
m
,
inspace
=
0
;
for
(
p
=
str
,
n
=
0
,
m
=
0
;
m
<=
12
&&
((
*
p
>=
32
&&
*
p
<=
47
)
||
(
*
p
>=
58
&&
*
p
<=
63
)
||
(
*
p
>=
91
&&
*
p
<=
96
)
||
(
*
p
>=
123
&&
*
p
<=
126
));
n
++
,
p
++
)
if
(
*
p
==
32
)
{
if
(
!
inspace
)
{
inspace
=
1
;
}
}
else
{
if
(
inspace
)
{
inspace
=
0
;
if
(
!
(
begin
&&
m
==
0
))
m
++
;
}
m
++
;
}
if
(
inspace
&&
(
*
p
!=
0
)
&&
!
(
begin
&&
m
==
0
))
m
++
;
if
(
m
>
12
||
m
==
0
)
return
0
;
if
(
simulate
)
{
str
+=
n
;
dtfspec
+=
m
;
return
1
;
}
else
{
inspace
=
0
;
for
(
p
=
str
,
q
=
dtfspec
;
n
;
n
--
,
p
++
)
if
(
*
p
==
32
)
{
if
(
!
inspace
)
{
inspace
=
1
;
}
}
else
{
if
(
inspace
)
{
inspace
=
0
;
if
(
!
(
begin
&&
m
==
0
))
*
q
++
=
32
;
}
*
q
++
=
*
p
;
}
if
(
inspace
&&
(
*
p
!=
0
)
&&
!
(
begin
&&
q
==
dtfspec
))
*
q
++
=
32
;
*
q
=
0
;
str
=
p
;
dtfspec
=
q
;
return
1
;
}
}
// Checks a number of standard date and time formats, and if it finds a match returns the
// format that matched
int
check_datetime
(
const
char
*
str
,
int
trybits
,
int
&
dtfbits
,
const
char
*&
dtfspec
)
{
// l�tsas flytta �ver lite icke-alphanumeriska
// while 1
// bryt om inte hittat datum och andra varvet
// om vi ska kolla klockslag:
// bryt om redan hittat klockslag
// kolla lite klockslagsformat och flytta �ver
// om hittat, flytta �ver lite icke-alphanumeriska
// bryt om andra varvet
// om vi ska kolla datum:
// kolla lite datumformat
// om hittat, flytta �ver lite icke-alphanumeriska
// s�tt andra varvet
// om n�got hittat, flytta inledande alphanumeriska
//
// %Y-%m-%d %y-%m-%d %Y%m%d %y%m%d
// %m/%d/%Y %m/%d/%y
// %H:%M:%S %H:%M
const
char
*
p
=
str
;
int
leadin
,
first
,
found_time
,
found_date
,
has_ms
,
d2
,
d3
;
char
*
formspec
,
*
fs
;
if
(
trybits
&
3
)
{
formspec
=
fs
=
new
char
[
80
];
leadin
=
check_datetime_internal_transfer
(
p
,
fs
,
1
,
1
);
found_date
=
0
;
found_time
=
0
;
first
=
1
;
// Some spagetti-code here, all to avoid writing the formats several times
while
(
1
)
{
if
(
!
found_date
&&
!
first
)
break
;
if
(
trybits
&
1
)
{
if
(
found_time
)
break
;
if
(
check_datetime_internal_match
(
p
,
"%I:%M:%S %p"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%I:%M %p"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%H:%M:%S"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%H:%M"
,
fs
))
{
found_time
=
1
;
if
(
fs
[
-
1
]
==
'S'
&&
*
p
==
'.'
)
{
// Hack to get rid of fraction part of seconds
p
++
;
while
(
*
p
>=
'0'
&&
*
p
<=
'9'
)
p
++
;
}
else
check_datetime_internal_transfer
(
p
,
fs
,
0
,
0
);
}
}
if
(
!
first
)
break
;
if
(
trybits
&
2
)
{
if
(
found_date
)
break
;
if
(
check_datetime_internal_match
(
p
,
"%y-%m-%d"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%Y-%m-%d"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%m/%d/%y"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%m/%d/%Y"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%d/%m %Y"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%b %d %Y"
,
fs
)
||
check_datetime_internal_match
(
p
,
"%d %b %Y"
,
fs
))
{
found_date
=
1
;
check_datetime_internal_transfer
(
p
,
fs
,
0
,
0
);
}
}
first
=
0
;
}
if
((
*
p
==
0
)
&&
(
found_time
||
found_date
))
{
if
(
leadin
)
check_datetime_internal_transfer
(
str
,
formspec
,
1
,
0
);
dtfspec
=
formspec
;
dtfbits
=
(
found_time
?
1
:
0
)
|
(
found_date
?
2
:
0
);
return
1
;
}
else
{
delete
[]
formspec
;
}
}
if
(
trybits
&
24
)
{
if
(
chunk_seconds
(
str
,
has_ms
,
d2
,
d3
))
{
dtfspec
=
0
;
dtfbits
=
(
has_ms
?
16
:
8
);
return
1
;
}
}
return
0
;
}
int
check_formated_datetime
(
const
char
*
str
,
int
dtfbits
,
const
char
*
dtfspec
)
{
const
char
*
p
;
int
has_ms
,
d2
,
d3
,
b
;
struct
tm
tst
;
if
(
dtfbits
&
3
)
{
p
=
0
;
//strptime(str, dtfspec, &tst);
if
(
!
p
)
return
0
;
if
(
dtfspec
[
strlen
(
dtfspec
)
-
1
]
==
'S'
&&
*
p
==
'.'
)
{
// Hack to get rid of fraction part of seconds
p
++
;
while
(
*
p
>=
'0'
&&
*
p
<=
'9'
)
p
++
;
}
while
(
*
p
==
32
)
p
++
;
if
(
*
p
)
return
0
;
}
else
if
(
dtfbits
&
24
)
{
b
=
chunk_seconds
(
str
,
has_ms
,
d2
,
d3
);
if
(
!
b
||
dtfbits
!=
(
has_ms
?
16
:
8
))
return
0
;
}
else
return
0
;
return
1
;
}
void
FormatSpecDatetime
::
init
(
struct
TokenLink
*
tok
)
{
int
b
;
const
char
*
fs
;
name
=
stripname
(
tok
);
if
(
!
strcmp
(
tok
->
token
,
"date:"
))
dtformbits
=
2
;
else
if
(
!
strcmp
(
tok
->
token
,
"time:"
))
dtformbits
=
1
;
else
if
(
!
strcmp
(
tok
->
token
,
"seconds:"
))
dtformbits
=
8
;
else
if
(
!
strcmp
(
tok
->
token
,
"millisec:"
))
dtformbits
=
16
;
else
dtformbits
=
3
;
dtformspec
=
0
;
tok
=
tok
->
next
;
if
(
!
tok
)
// No arguments, we are done
;
else
if
(
!
tok
->
next
)
{
// format
if
(
dtformbits
&
24
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too few %s format arguments: %s
\n
"
,
(
dtformbits
&
8
?
"seconds"
:
"millisec"
),
tok
->
token
);
}
else
if
(
validate_datetime_format
(
tok
->
token
,
b
)
&&
(
b
&
3
)
==
dtformbits
)
{
dtformbits
=
b
;
dtformspec
=
strcpy
(
new
char
[
strlen
(
tok
->
token
)
+
1
],
tok
->
token
);
unset
=
0
;
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad %s format form specification argument: %s
\n
"
,
(
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
);
}
}
else
if
(
!
tok
->
next
->
next
)
{
// first last
fs
=
0
;
if
(
check_datetime
(
tok
->
token
,
dtformbits
,
b
,
fs
)
&&
(
b
&
27
)
==
dtformbits
&&
check_formated_datetime
(
tok
->
next
->
token
,
b
,
fs
))
{
dtformbits
=
b
;
dtformspec
=
fs
;
unset
=
0
;
first
=
interpret
(
tok
->
token
);
last
=
interpret
(
tok
->
next
->
token
);
}
else
{
if
(
fs
)
delete
[]
fs
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Strange %s format arguments: %s %s
\n
"
,
(
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
,
tok
->
next
->
token
);
}
}
}
else
if
(
!
tok
->
next
->
next
->
next
)
{
// format first last
if
(
dtformbits
&
24
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many %s format arguments: %s %s %s
\n
"
,
(
dtformbits
&
8
?
"seconds"
:
"millisec"
),
tok
->
token
,
tok
->
next
->
token
,
tok
->
next
->
next
->
token
);
}
else
if
(
validate_datetime_format
(
tok
->
token
,
b
)
&&
(
b
&
3
)
==
dtformbits
)
{
if
(
check_formated_datetime
(
tok
->
next
->
token
,
b
,
tok
->
token
)
&&
check_formated_datetime
(
tok
->
next
->
next
->
token
,
b
,
tok
->
token
))
{
dtformbits
=
b
;
dtformspec
=
strcpy
(
new
char
[
strlen
(
tok
->
token
)
+
1
],
tok
->
token
);
unset
=
0
;
first
=
interpret
(
tok
->
next
->
token
);
last
=
interpret
(
tok
->
next
->
next
->
token
);
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Non-matching %s format arguments: %s %s %s
\n
"
,
(
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
,
tok
->
next
->
token
,
tok
->
next
->
next
->
token
);
}
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad %s format form specification argument: %s
\n
"
,
(
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
);
}
}
else
{
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Too many %s format arguments: %s %s %s %s ...
\n
"
,
(
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
tok
->
token
,
tok
->
next
->
token
,
tok
->
next
->
next
->
token
,
tok
->
next
->
next
->
next
->
token
);
}
}
}
void
FormatSpecDatetime
::
save
(
FILE
*
f
)
{
const
char
*
tp
=
(
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
);
printname
(
f
);
if
(
first
.
i
==
-
1
||
last
.
i
==
-
1
)
{
if
(
dtformbits
&
4
&&
dtformspec
)
fprintf
(
f
,
"%s:
\t
%s
\n
"
,
tp
,
dtformspec
);
else
fprintf
(
f
,
"%s:
\n
"
,
tp
);
}
else
{
fprintf
(
f
,
"%s:"
,
tp
);
if
(
dtformbits
&
4
&&
dtformspec
)
fprintf
(
f
,
"
\t
%s"
,
dtformspec
);
fprintf
(
f
,
"
\t
%s"
,
represent
(
first
));
fprintf
(
f
,
"
\t
%s
\n
"
,
represent
(
last
));
}
}
void
FormatSpecDatetime
::
add
(
const
char
*
str
)
{
intfloat
val
;
if
(
is_unknown
(
str
))
return
;
val
=
interpret
(
str
);
if
(
val
.
i
!=
-
1
)
{
if
(
val
.
i
<
first
.
i
||
first
.
i
==
-
1
)
first
=
val
;
if
(
val
.
i
>
last
.
i
)
last
=
val
;
}
}
intfloat
FormatSpecDatetime
::
interpret
(
const
char
*
str
)
{
int
b
;
const
char
*
fs
;
intfloat
res
;
int
ok
=
1
;
int
y
,
mo
,
d
,
h
,
mi
,
s
,
dnum
,
leap
,
nleaps
,
sec
,
ms
,
has_ms
;
int
mv1
[]
=
{
0
,
31
,
59
,
90
,
120
,
151
,
181
,
212
,
243
,
273
,
304
,
334
};
int
mv2
[]
=
{
0
,
31
,
60
,
91
,
121
,
152
,
182
,
213
,
244
,
274
,
305
,
335
};
if
(
is_unknown
(
str
))
{
res
.
i
=
-
1
;
return
res
;
}
if
(
unset
)
{
fs
=
0
;
if
(
check_datetime
(
str
,
dtformbits
,
b
,
fs
)
&&
(
b
&
27
)
==
dtformbits
)
{
dtformbits
=
b
;
dtformspec
=
fs
;
unset
=
0
;
}
else
{
if
(
fs
)
delete
[]
fs
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Unrecognized %s form: %s
\n
"
,
(
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
dtformbits
&
2
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
"time"
),
str
);
}
res
.
i
=
-
1
;
return
res
;
}
}
if
(
dtformbits
&
24
)
{
ok
=
chunk_seconds
(
str
,
has_ms
,
sec
,
ms
);
if
(
ok
)
res
.
i
=
sec
+
(
has_ms
&&
ms
>
500
?
1
:
0
);
}
else
{
ok
=
chunk_formated_datetime
(
str
,
dtformbits
,
dtformspec
,
y
,
mo
,
d
,
h
,
mi
,
s
);
if
(
ok
&&
(
dtformbits
&
2
))
{
if
(
y
<
0
||
y
>
67
||
mo
<
1
||
mo
>
12
||
d
<
1
||
d
>
31
)
ok
=
0
;
else
{
leap
=
((
y
+
STARTYEAR
)
%
4
==
0
);
nleaps
=
(
y
+
((
STARTYEAR
+
3
)
%
4
))
/
4
;
// 3,0,1,2
dnum
=
d
+
(
leap
?
mv2
:
mv1
)[
mo
-
1
]
+
y
*
365
+
nleaps
-
1
;
}
}
if
(
ok
&&
(
dtformbits
&
1
))
{
if
(
h
<
0
||
h
>
23
||
mi
<
0
||
mi
>
59
||
s
<
0
||
s
>
59
)
ok
=
0
;
else
sec
=
h
*
3600
+
mi
*
60
+
s
;
}
if
(
ok
)
res
.
i
=
(
dtformbits
&
2
?
(
dtformbits
&
1
?
dnum
*
86400
+
sec
:
dnum
)
:
sec
);
}
if
(
!
ok
)
{
res
.
i
=
-
1
;
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Bad %s value: %s
\n
"
,
(
dtformbits
&
6
?
(
dtformbits
&
1
?
"datetime"
:
"date"
)
:
dtformbits
&
24
?
(
dtformbits
&
8
?
"seconds"
:
"millisec"
)
:
"time"
),
str
);
}
}
return
res
;
}
const
char
*
FormatSpecDatetime
::
represent
(
intfloat
v
)
{
static
char
buf
[
80
];
int
y
,
mo
,
d
,
h
,
mi
,
s
;
if
(
v
.
i
==
-
1
)
return
"?"
;
if
(
dtformbits
&
24
)
{
if
(
dtformbits
&
16
)
sprintf
(
buf
,
"%lld000"
,
(
long
long
)
v
.
i
);
// Not really milliseconds yet, fix it!!!
else
sprintf
(
buf
,
"%d"
,
(
int
)
v
.
i
);
}
else
{
get_date
(
v
.
i
,
y
,
mo
,
d
);
get_time
(
v
.
i
,
h
,
mi
,
s
);
string_formated_datetime
(
buf
,
dtformbits
,
dtformspec
,
y
,
mo
,
d
,
h
,
mi
,
s
);
}
return
buf
;
}
void
FormatSpecDatetime
::
get_time
(
int
val
,
int
&
h
,
int
&
mi
,
int
&
s
)
{
int
sec
;
if
(
!
(
dtformbits
&
1
))
{
h
=
mi
=
s
=
0
;
}
else
{
sec
=
val
%
86400
;
h
=
sec
/
3600
,
mi
=
(
sec
/
60
)
%
60
,
s
=
sec
%
60
;
}
}
void
FormatSpecDatetime
::
get_date
(
int
val
,
int
&
y
,
int
&
mo
,
int
&
d
)
{
int
dnum
,
leap
;
int
mv1
[]
=
{
0
,
31
,
59
,
90
,
120
,
151
,
181
,
212
,
243
,
273
,
304
,
334
};
int
mv2
[]
=
{
0
,
31
,
60
,
91
,
121
,
152
,
182
,
213
,
244
,
274
,
305
,
335
};
if
(
!
(
dtformbits
&
2
))
{
y
=
mo
=
d
=
0
;
}
else
{
if
(
dtformbits
&
1
)
dnum
=
val
/
86400
;
else
dnum
=
val
;
leap
=
((
y
+
STARTYEAR
)
%
4
==
0
);
for
(
y
=
0
;
dnum
>=
(
leap
?
366
:
365
);
dnum
-=
(
leap
?
366
:
365
),
y
++
);
for
(
mo
=
12
;
dnum
<
(
leap
?
mv2
:
mv1
)[
mo
-
1
];
mo
--
);
dnum
-=
(
leap
?
mv2
:
mv1
)[
mo
-
1
];
d
=
dnum
+
1
;
}
}
void
FormatSpecDatetime
::
usurp
(
FormatSpecUnknown
*
ele
)
{
char
*
str
;
const
char
*
dtfspec
;
int
dtfbits
;
if
(
!
ele
->
unset
)
{
ele
->
table
->
reset
();
while
((
str
=
ele
->
table
->
next
()))
{
if
(
unset
&&
check_datetime
(
str
,
27
,
dtfbits
,
dtfspec
))
{
dtformbits
=
dtfbits
;
dtformspec
=
dtfspec
;
unset
=
0
;
}
add
(
str
);
}
}
}
C_code Trajectory clustering TBI/dataformat/formattypes.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#define FormatSpecType -1
#define FormatSpecUnknownType 0
#define FormatSpecBinaryType 1
#define FormatSpecDiscrType 2
#define FormatSpecContType 3
#define FormatSpecSymbolType 4
#define FormatSpecDatetimeType 5
#define IsType(form, tp) (form->type() == tp##Type)
C_code Trajectory clustering TBI/dataformat/formatunknown.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "readtokens.hh"
#include "table.hh"
#include "format.hh"
#include "formatunknown.hh"
/*
class FormatSpecUnknown : public FormatSpec {
public:
FormatSpecUnknown(const char* nm = 0) : FormatSpec(nm) {};
virtual ~FormatSpecUnknown();
virtual FormatSpec* copy();
virtual void init(struct TokenLink* tok);
virtual void add(const char* str);
virtual void save(FILE* f);
virtual intfloat interpret(const char* str);
virtual int type() { return 0; };
IF_FLOAT min;
IF_FLOAT max;
int num, nseen;
class IndexTable* table;
};
*/
FormatSpecUnknown
::~
FormatSpecUnknown
()
{
delete
table
;
}
FormatSpec
*
FormatSpecUnknown
::
copy
()
{
FormatSpecUnknown
*
fs
=
new
FormatSpecUnknown
(
name
);
fs
->
min
=
min
;
fs
->
max
=
max
;
fs
->
num
=
num
;
fs
->
nseen
=
nseen
;
fs
->
table
=
table
->
copy
();
fs
->
unset
=
unset
;
return
fs
;
}
void
FormatSpecUnknown
::
init
(
struct
TokenLink
*
tok
)
{
name
=
stripname
(
tok
);
if
(
tok
!=
NULL
)
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Unknown format type: %s
\n
"
,
tok
->
token
);
}
else
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Unknown format type: %s
\n
"
,
name
);
}
}
void
FormatSpecUnknown
::
save
(
FILE
*
f
)
{
printname
(
f
);
if
(
unset
)
fprintf
(
f
,
"unknown:
\n
"
);
else
fprintf
(
f
,
"unknown:
\t
%g
\t
%g
\t
%d
\n
"
,
min
,
max
,
num
);
{
if
(
!
global_format_quiet
)
fprintf
(
stderr
,
"Unknown format type saved
\n
"
);
}
}
void
FormatSpecUnknown
::
add
(
const
char
*
str
)
{
IF_FLOAT
val
;
if
(
is_unknown
(
str
))
;
else
if
(
unset
)
{
unset
=
0
;
min
=
max
=
get_cont
(
str
);
num
=
nseen
=
1
;
table
=
new
IndexTable
;
table
->
insert
(
str
);
}
else
{
val
=
get_cont
(
str
);
if
(
val
<
min
)
min
=
val
;
else
if
(
val
>
max
)
max
=
val
;
nseen
+=
1
;
if
(
table
->
get
(
str
)
<
0
)
{
table
->
insert
(
str
);
num
+=
1
;
}
}
}
intfloat
FormatSpecUnknown
::
interpret
(
const
char
*
str
)
{
intfloat
res
;
res
.
i
=
-
1
;
return
res
;
}
C_code Trajectory clustering TBI/dataformat/formatunknown.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
class
FormatSpecUnknown
:
public
FormatSpec
{
public:
FormatSpecUnknown
(
const
char
*
nm
=
0
)
:
FormatSpec
(
nm
)
{
table
=
NULL
;};
virtual
~
FormatSpecUnknown
();
virtual
FormatSpec
*
copy
();
virtual
void
init
(
struct
TokenLink
*
tok
);
virtual
void
add
(
const
char
*
str
);
virtual
void
save
(
FILE
*
f
);
virtual
intfloat
interpret
(
const
char
*
str
);
virtual
int
type
()
{
return
0
;
};
float
min
;
float
max
;
int
num
,
nseen
;
class
IndexTable
*
table
;
};
C_code Trajectory clustering TBI/dataformat/growable.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#ifndef TEMPLATE_GROWABLE
#define TEMPLATE_GROWABLE
template
<
class
T
>
class
Growable
{
public:
Growable
(
int
sz
=
0
,
int
chunk
=
64
){
num
=
0
;
_size
=
0
;
_chunk
=
chunk
;
vec
=
curr
=
0
;
resize
(
sz
);
num
=
sz
;
};
Growable
(
const
Growable
<
T
>
&
grow
,
int
chunk
=
64
){
T
*
vec1
,
*
vec2
;
int
i
;
num
=
0
;
_size
=
0
;
vec
=
curr
=
0
;
resize
(
grow
.
num
);
num
=
grow
.
num
;
for
(
i
=
0
,
vec1
=
grow
.
vec
,
vec2
=
vec
;
i
<
num
;
i
++
,
vec1
++
,
vec2
++
)
*
vec2
=
*
vec1
;
};
~
Growable
()
{
delete
[]
vec
;}
Growable
<
T
>&
operator
=
(
Growable
<
T
>
&
grow
){
T
*
vec1
,
*
vec2
;
int
i
;
if
(
grow
.
num
>
_size
)
{
num
=
0
;
delete
[]
vec
;
vec
=
0
;
resize
(
grow
.
num
);
}
curr
=
vec
;
num
=
grow
.
num
;
for
(
i
=
0
,
vec1
=
grow
.
vec
,
vec2
=
vec
;
i
<
num
;
i
++
,
vec1
++
,
vec2
++
)
*
vec2
=
*
vec1
;
return
*
this
;
};
T
*
vect
()
{
return
vec
;}
Growable
<
T
>&
operator
<<
(
T
val
)
{
int
i
=
add
(
1
);
vec
[
i
]
=
val
;
return
*
this
;};
T
&
operator
[](
int
i
)
{
if
(
i
>=
num
)
add
(
i
-
num
+
1
);
return
*
((
curr
=
vec
+
i
+
1
)
-
1
);}
T
&
operator
()()
{
return
*
curr
++
;}
Growable
<
T
>&
reset
()
{
curr
=
vec
;
return
*
this
;}
int
end
()
{
return
(
curr
>=
vec
+
num
);}
int
current
()
{
return
curr
-
vec
;
}
int
size
()
{
return
num
;}
int
operator
!
()
{
return
(
num
==
0
);}
operator
int
()
{
return
(
num
>
0
);}
int
add
(
int
n
=
1
){
int
tn
=
num
;
if
(
num
+
n
>
_size
)
resize
(
num
+
n
);
num
+=
n
;
return
tn
;
};
int
remove
(
int
n
=
1
){
num
-=
n
;
if
(
num
<
0
)
num
=
0
;
if
(
num
<
_size
-
2
*
_chunk
)
resize
(
num
);
return
num
;
};
private:
void
resize
(
int
sz
){
T
*
newvec
,
*
vec1
,
*
vec2
;
int
i
,
tcurr
;
sz
=
((
sz
-
1
)
/
_chunk
+
1
)
*
_chunk
;
if
(
_size
!=
sz
)
{
_size
=
sz
;
if
(
_size
>
10
*
_chunk
)
_chunk
*=
2
;
newvec
=
new
T
[
_size
];
num
=
(
num
>
_size
?
_size
:
num
);
tcurr
=
(
curr
-
vec
);
tcurr
=
(
tcurr
>
_size
?
_size
:
tcurr
);
for
(
i
=
0
,
vec1
=
newvec
,
vec2
=
vec
;
i
<
num
;
i
++
,
vec1
++
,
vec2
++
)
*
vec1
=
*
vec2
;
delete
[]
vec
;
vec
=
newvec
;
curr
=
vec
+
tcurr
;
}
};
int
_size
;
int
_chunk
;
int
num
;
T
*
curr
;
T
*
vec
;
};
#define FORVEC( grow, var) for((grow).reset(); !((grow).end())&&((var=(grow)()),1); )
// #define FORGROW( grow, var, gop) for( gop=(grow).current(),(grow).reset(); ((grow).end()?((grow)[gop-1],0):((var=(grow)()),1)); )
#define FORGROW( grow, var, gop) for( gop=0; gop<(grow).size() && (var=(grow)[gop], 1); gop++ )
#endif
C_code Trajectory clustering TBI/dataformat/hashtable.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#ifndef TEMPLATE_HASHTABLE
#define TEMPLATE_HASHTABLE
template
<
class
T
>
class
HTEle
{
public:
HTEle
(
const
char
*
str
)
{
ind
=
strcpy
(
new
char
[
strlen
(
str
)
+
1
],
str
);
};
~
HTEle
()
{
delete
[]
ind
;
};
char
*
ind
;
T
val
;
HTEle
<
T
>*
next
;
};
template
<
class
T
>
class
HashTable
{
public:
HashTable
()
{
num
=
0
;
_size
=
0
;
_blockresize
=
0
;
vec
=
0
;
currele
=
0
;
/* def = 0; */
resize
(
32
);
};
HashTable
(
T
d
)
{
num
=
0
;
_size
=
0
;
_blockresize
=
0
;
vec
=
0
;
currele
=
0
;
def
=
d
;
resize
(
32
);
};
~
HashTable
()
{
HTEle
<
T
>
*
ele
,
*
ele2
;
int
i
;
for
(
i
=
0
;
i
<
_size
;
i
++
)
for
(
ele
=
vec
[
i
];
ele
;
ele
=
ele2
)
{
ele2
=
ele
->
next
;
delete
ele
;
}
delete
[]
vec
;
};
T
&
operator
()(
const
char
*
str
)
{
HTEle
<
T
>*
ele
;
int
h
=
hash
(
str
);
for
(
ele
=
vec
[
h
];
ele
&&
strcmp
(
ele
->
ind
,
str
);
ele
=
ele
->
next
);
if
(
!
ele
)
ele
=
add
(
h
,
str
);
return
ele
->
val
;
};
void
reset
()
{
currele
=
0
;
currhind
=
-
1
;
_blockresize
=
1
;
};
int
end
()
{
if
(
currele
&&
currele
->
next
)
currele
=
currele
->
next
;
else
for
(
currele
=
0
;
currhind
<
_size
-
1
&&
!
(
currele
=
vec
[
++
currhind
]);
);
if
(
currele
)
return
0
;
else
{
_blockresize
=
0
;
return
1
;
}
};
T
&
currval
()
{
return
currele
->
val
;
}
char
*
currind
()
{
return
currele
->
ind
;
}
int
size
()
{
return
num
;
}
int
operator
!
()
{
return
(
num
==
0
);
}
operator
int
()
{
return
(
num
>
0
);
}
void
setdefault
(
T
d
)
{
def
=
d
;
};
private:
int
hash
(
const
char
*
str
)
{
register
int
h
=
0
;
const
unsigned
char
*
p
;
for
(
p
=
(
const
unsigned
char
*
)
str
;
*
p
;
p
++
)
{
h
^=
*
p
;
h
<<=
1
;
h
=
(
h
>>
_bits
)
^
(
h
&
(
_size
-
1
));
}
return
h
;
}
HTEle
<
T
>*
add
(
int
h
,
const
char
*
str
)
{
HTEle
<
T
>*
ele
=
new
HTEle
<
T
>
(
str
);
ele
->
val
=
def
;
ele
->
next
=
vec
[
h
];
vec
[
h
]
=
ele
;
num
++
;
if
(
!
_blockresize
&&
num
>
_size
*
4
)
resize
(
_size
*
8
);
return
ele
;
};
void
resize
(
int
sz
)
{
HTEle
<
T
>**
oldvec
;
HTEle
<
T
>
*
ele
,
*
ele2
;
int
hind
;
int
i
;
int
oldsize
;
oldsize
=
_size
;
oldvec
=
vec
;
_size
=
sz
;
for
(
_bits
=-
1
;
sz
;
_bits
++
,
sz
>>=
1
);
vec
=
new
HTEle
<
T
>*
[
_size
];
num
=
0
;
for
(
i
=
0
;
i
<
_size
;
i
++
)
vec
[
i
]
=
0
;
if
(
oldvec
)
{
for
(
i
=
0
;
i
<
oldsize
;
i
++
)
for
(
ele
=
oldvec
[
i
];
ele
;
ele
=
ele2
)
{
ele2
=
ele
->
next
;
hind
=
hash
(
ele
->
ind
);
ele
->
next
=
vec
[
hind
];
vec
[
hind
]
=
ele
;
++
num
;
}
delete
[]
oldvec
;
}
};
int
_size
;
int
_bits
;
int
_blockresize
;
int
num
;
T
def
;
HTEle
<
T
>
*
currele
;
int
currhind
;
HTEle
<
T
>**
vec
;
};
#ifndef FORHASH
#define FORHASH( ht, var, str) for( (ht).reset(); !((ht).end())&&((var=(ht).currval()),(str=(ht).currind()),1); )
#endif
#endif
C_code Trajectory clustering TBI/dataformat/intfloat.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#ifndef INTFLOAT
#define INTFLOAT
#ifdef IF_FLOAT
#error "Don't use IF_FLOAT, use INTFLOAT_LONG"
#endif
#ifdef INTFLOAT_LONG
#define IF_FLOAT double
#define IF_INT long long
#else
#define IF_FLOAT float
#define IF_INT int
#endif
union
intfloat
{
IF_INT
i
;
IF_FLOAT
f
;
intfloat
()
{};
intfloat
(
IF_FLOAT
ff
)
{
f
=
ff
;
};
intfloat
(
IF_INT
ii
)
{
i
=
ii
;
};
operator
IF_FLOAT
()
{
return
f
;
};
operator
IF_INT
()
{
return
i
;
};
};
#endif
C_code Trajectory clustering TBI/dataformat/libdataformat.a
0 → 100644
View file @
63b3a308
File added
C_code Trajectory clustering TBI/dataformat/make.depend
0 → 100644
View file @
63b3a308
data.o: data.cc readtokens.hh format.hh intfloat.hh growable.hh data.hh
datadispatch.o: datadispatch.cc format.hh intfloat.hh growable.hh data.hh
format.o: format.cc table.hh readtokens.hh format.hh intfloat.hh \
growable.hh formatunknown.hh formatdispatch.hh formattypes.hh
formatbinary.o: formatbinary.cc readtokens.hh format.hh intfloat.hh \
growable.hh formatbinary.hh
formatcont.o: formatcont.cc readtokens.hh format.hh intfloat.hh \
growable.hh formatunknown.hh formatcont.hh
formatdiscr.o: formatdiscr.cc readtokens.hh format.hh intfloat.hh \
growable.hh formatunknown.hh formatdiscr.hh
formatdispatch.o: formatdispatch.cc format.hh intfloat.hh growable.hh \
readtokens.hh formatbinary.hh formatdiscr.hh formatcont.hh \
formatsymbol.hh formatunknown.hh
formatsymbol.o: formatsymbol.cc readtokens.hh table.hh format.hh \
intfloat.hh growable.hh formatunknown.hh formatsymbol.hh
formatunknown.o: formatunknown.cc readtokens.hh table.hh format.hh \
intfloat.hh growable.hh formatunknown.hh
makeformat.o: makeformat.cc format.hh intfloat.hh growable.hh
readtokens.o: readtokens.cc readtokens.hh
selectcolumns.o: selectcolumns.cc readtokens.hh
table.o: table.cc table.hh
C_code Trajectory clustering TBI/dataformat/makeformat
0 → 100755
View file @
63b3a308
File added
C_code Trajectory clustering TBI/dataformat/makeformat.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include "readtokens.hh"
#include "format.hh"
static
int
handle_readtoken_flags
(
int
&
argc
,
char
**
argv
)
{
int
ok
=
1
;
for
(
int
i
=
1
;
i
<
argc
;
i
++
)
{
if
(
!
strcmp
(
argv
[
i
],
"-s"
))
{
if
(
i
+
1
<
argc
&&
strlen
(
argv
[
i
+
1
])
==
1
)
{
set_readtoken_separator_char
(
argv
[
i
+
1
][
0
]);
argc
-=
2
;
for
(
int
j
=
i
;
j
<
argc
;
j
++
)
argv
[
j
]
=
argv
[
j
+
2
];
i
--
;
}
else
ok
=
0
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"-q"
))
{
if
(
i
+
1
<
argc
&&
strlen
(
argv
[
i
+
1
])
==
1
)
{
set_readtoken_quote_delim
(
argv
[
i
+
1
][
0
]);
argc
-=
2
;
for
(
int
j
=
i
;
j
<
argc
;
j
++
)
argv
[
j
]
=
argv
[
j
+
2
];
i
--
;
}
else
ok
=
0
;
}
else
if
(
!
strcmp
(
argv
[
i
],
"-u"
))
{
if
(
i
+
1
<
argc
)
{
set_readtoken_unknown_tag
(
argv
[
i
+
1
]);
argc
-=
2
;
for
(
int
j
=
i
;
j
<
argc
;
j
++
)
argv
[
j
]
=
argv
[
j
+
2
];
i
--
;
}
else
ok
=
0
;
}
}
return
ok
;
}
int
main
(
int
argc
,
char
**
argv
)
{
char
*
datafile
=
0
;
char
*
formatfile
=
0
;
Format
*
format
;
int
ok
;
ok
=
handle_readtoken_flags
(
argc
,
argv
);
for
(
int
i
=
1
;
i
<
argc
;
i
++
)
{
if
(
!
datafile
)
{
datafile
=
argv
[
i
];
}
else
if
(
!
formatfile
)
{
formatfile
=
argv
[
i
];
}
else
ok
=
0
;
}
if
(
!
datafile
)
ok
=
0
;
if
(
!
ok
)
{
fprintf
(
stdout
,
"Usage: makeformat [-s separator][-q quotechar][-u unknowntag] <datafile> [<formatfile>]
\n
"
);
exit
(
0
);
}
if
(
!
formatfile
)
{
int
i
,
len
=
strlen
(
datafile
);
formatfile
=
strcpy
(
new
char
[
len
+
6
],
datafile
);
for
(
i
=
len
-
1
;
i
>
0
&&
formatfile
[
i
]
!=
'.'
;
i
--
);
if
(
i
<
1
||
!
(
!
strcmp
(
formatfile
+
i
,
".data"
)
||
!
strcmp
(
formatfile
+
i
,
".txt"
)
||
!
strcmp
(
formatfile
+
i
,
".csv"
)))
i
=
len
;
strcpy
(
formatfile
+
i
,
".form"
);
}
format
=
new
Format
();
ok
=
format
->
read
(
formatfile
);
if
(
ok
)
{
FormatSpec
*
fs
;
for
(
fs
=
format
->
nth
(
0
);
fs
&&
fs
->
type
()
==
0
;
fs
=
fs
->
next
);
if
(
!
fs
)
{
fprintf
(
stdout
,
"Error: malformed format file
\"
%s
\"\n
"
,
formatfile
);
exit
(
1
);
}
}
ok
=
format
->
construct
(
datafile
);
if
(
!
ok
)
{
fprintf
(
stdout
,
"Error: Failed to open data file
\"
%s
\"\n
"
,
datafile
);
exit
(
1
);
}
format
->
save
(
formatfile
);
delete
format
;
return
0
;
}
C_code Trajectory clustering TBI/dataformat/our-g++
0 → 100755
View file @
63b3a308
#! /bin/sh
# These warnings are not supported in g++ 4.2
WARNINGS44
=
"-Wlogical-op -Wmissing-declarations"
# "uname -o" doesn't exist on Darwin.
OS
=
`
uname
`
if
[
"
$OS
"
=
"Darwin"
]
;
then
WARNINGS44
=
XCXXFLAGS
=
"-DPLATFORM_MAC"
elif
[
`
expr
substr
$OS
1 6
`
=
"CYGWIN"
]
;
then
WARNINGS44
=
XCXXFLAGS
=
"-DPLATFORM_MSW"
elif
[
"
$OS
"
=
"Linux"
]
;
then
WARNINGS44
=
"-Wmissing-declarations"
XCXXFLAGS
=
"-DUSE_WCHAR -DPLATFORM_GTK"
elif
[
"
$OS
"
=
"FreeBSD"
]
;
then
WARNINGS44
=
XCXXFLAGS
=
"-DPLATFORM_GTK"
else
echo
"Error: unknown platform
$OS
- modifying our-g++ may help"
exit
1
fi
CXX
=
g++
#CXX=clang++
#CXX=/home/arndt/clang/llvm-3.3.src/Release+Asserts/bin/clang++
CXXFLAGS
=
"-Wall -Wno-write-strings -Wextra -Wno-unused-parameter -Wmissing-noreturn -Wshadow -Wswitch-default
$WARNINGS44
-g
$XCXXFLAGS
"
# Sometimes interesting: -O -fdiagnostics-show-option
# -O causes more semantic analysis to be done, so we may get more warnings
# -Wredundant-decls -Waggregate-return -Wunreachable-code react to wx.h
# -Wfloat-equal and -Wconversion triggers warnings in wx
# -Winline together with -O give a lot of warnings from wxWidgets
#echo $CXX $CXXFLAGS $@
if
[
"
$OS
"
=
"Darwin"
]
;
then
# Mac OS: wxWidgets or others may not be compiled for 64 bits architecture
$CXX
-m32
$CXXFLAGS
$@
else
$CXX
$CXXFLAGS
$@
fi
C_code Trajectory clustering TBI/dataformat/readtokens.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <stdio.h>
#ifdef GZLIB
#include <zlib.h>
#endif
#include <string.h>
#include "readtokens.hh"
/*
struct TokenLink {
char* token;
TokenLink* next;
};
TokenLink* readtokens(FILE* f);
void freetokens(TokenLink* list);
int length(TokenLink* list);
int is_posint(const char* str);
int is_cont(const char* str);
int is_unknown(const char* str);
int get_posint(const char* str);
float get_cont(const char* str);
void set_readtoken_separator_char(char sep);
char get_readtoken_separator_char();
void set_readtoken_unknown_tag(const char* unk);
const char* get_readtoken_unknown_tag();
void set_readtoken_quote_delim(char delim);
char get_readtoken_quote_delim();
*/
static
int
buflen
=
0
;
static
char
*
buf
=
0
;
static
char
*
pos
;
static
char
curr
=
0
;
static
TokenLink
*
worklist
;
char
global_separator_char
=
'\t'
;
const
char
*
global_unknown_tag
=
0
;
char
global_quote_delim
=
0
;
#ifdef GZLIB
FILE
*
gz_file
;
gzFile
gz_gzfile
;
static
int
xgetc
(
FILE
*
f
)
{
int
c
;
if
(
f
==
gz_file
)
return
gzgetc
(
gz_gzfile
);
else
{
c
=
getc
(
f
);
return
c
;
}
}
static
char
*
xgets
(
char
*
buf1
,
int
siz
,
FILE
*
f
)
{
int
c
;
if
(
f
==
gz_file
)
return
gzgets
(
gz_gzfile
,
buf1
,
siz
);
else
{
return
fgets
(
buf1
,
siz
,
f
);
}
}
static
int
xfeof
(
FILE
*
f
)
{
if
(
f
==
gz_file
)
return
gzeof
(
gz_gzfile
);
else
return
feof
(
f
);
}
#else
#define xgetc fgetc
#define xgets fgets
#define xfeof feof
#endif
static
void
resetbuf
(
FILE
*
f
)
{
if
(
!
buf
)
{
buflen
=
1024
;
buf
=
new
char
[
buflen
];
}
pos
=
buf
;
curr
=
xgetc
(
f
);
if
(
xfeof
(
f
))
curr
=
0
;
}
char
*
freezebuf
()
{
char
*
tmp
=
buf
;
buf
=
0
;
buflen
=
0
;
return
tmp
;
}
static
void
growbuf
(
char
*&
s1
,
char
*&
s2
)
{
int
i
;
char
*
p1
,
*
p2
;
TokenLink
*
l
;
int
nlen
=
buflen
*
2
;
char
*
nbuf
=
new
char
[
nlen
];
for
(
i
=
0
,
p1
=
buf
,
p2
=
nbuf
;
i
<
buflen
;
*
p2
++=*
p1
++
,
i
++
);
for
(
l
=
worklist
;
l
;
l
=
l
->
next
)
l
->
token
=
nbuf
+
(
l
->
token
-
buf
);
s1
=
nbuf
+
(
s1
-
buf
);
s2
=
nbuf
+
(
s2
-
buf
);
delete
[]
buf
;
buf
=
nbuf
;
buflen
=
nlen
;
}
void
set_readtoken_separator_char
(
char
sep
)
{
global_separator_char
=
sep
;
}
char
get_readtoken_separator_char
()
{
return
global_separator_char
;
}
void
set_readtoken_unknown_tag
(
const
char
*
unk
)
{
global_unknown_tag
=
unk
;
}
const
char
*
get_readtoken_unknown_tag
()
{
return
global_unknown_tag
;
}
void
set_readtoken_quote_delim
(
char
delim
)
{
global_quote_delim
=
delim
;
}
char
get_readtoken_quote_delim
()
{
return
global_quote_delim
;
}
static
int
readsep
(
FILE
*
f
,
int
first
)
{
int
tres
=
first
;
while
(
global_separator_char
?
(
!
tres
&&
curr
==
global_separator_char
)
:
(
curr
==
' '
||
(
!
tres
&&
(
curr
==
','
||
curr
==
';'
||
curr
==
'\t'
))))
{
if
(
curr
!=
' '
)
tres
=
1
;
curr
=
xgetc
(
f
);
if
(
xfeof
(
f
))
curr
=
0
;
}
if
(
curr
==
'\r'
)
{
curr
=
xgetc
(
f
);
if
(
xfeof
(
f
))
curr
=
0
;
}
if
(
curr
==
0
&&
(
first
||
!
tres
))
return
1
;
else
if
(
curr
==
'\n'
&&
!
tres
)
return
1
;
else
return
0
;
}
static
char
*
readtoken
(
FILE
*
f
)
{
char
*
str
;
int
inquote
=
0
;
str
=
pos
;
#if 0
// Comments start with # and make up the rest of the line
while (curr == '#') {
for (;;) {
curr = getc(f);
if (curr == '\n') {
curr = getc(f);
if (feof(f))
curr = 0;
break;
}
}
}
#endif
if
(
global_quote_delim
&&
curr
==
global_quote_delim
)
{
curr
=
xgetc
(
f
);
if
(
xfeof
(
f
))
curr
=
0
;
inquote
=
1
;
}
/*
while (curr != 0 && curr != '\n' && curr != '\r' &&
(inquote ? (curr != global_quote_delim) :
global_separator_char ? (curr != global_separator_char) :
(curr != ' ' && curr != ',' && curr != ';' && curr != '\t'))) {
*pos++ = curr;
if (pos >= buf+buflen)
growbuf(pos, str);
curr = xgetc(f);
if (xfeof(f)) curr = 0;
}
if (inquote) {
inquote = 0;
if (curr == global_quote_delim) {
curr = xgetc(f);
if (xfeof(f)) curr = 0;
} else if (curr != 0 && curr != '\n' && curr != '\r' &&
(global_separator_char ? (curr != global_separator_char) :
(curr != ' ' && curr != ',' && curr != ';' && curr != '\t'))) {
// malformed quoted token - end quote must be at token end - but nowhere to complain
while (curr != 0 && curr != '\n' && curr != '\r' &&
(global_separator_char ? (curr != global_separator_char) :
(curr != ' ' && curr != ',' && curr != ';' && curr != '\t'))) {
curr = xgetc(f);
if (xfeof(f)) curr = 0;
}
}
}
*/
while
(
curr
!=
0
&&
(
inquote
||
(
curr
!=
'\n'
&&
curr
!=
'\r'
&&
(
global_separator_char
?
(
curr
!=
global_separator_char
)
:
(
curr
!=
' '
&&
curr
!=
','
&&
curr
!=
';'
&&
curr
!=
'\t'
)))))
{
if
(
inquote
&&
(
curr
==
global_quote_delim
))
{
curr
=
xgetc
(
f
);
if
(
xfeof
(
f
))
curr
=
0
;
if
(
curr
==
global_quote_delim
)
{
*
pos
++
=
curr
;
// double quite in quote translates to single quote
if
(
pos
>=
buf
+
buflen
)
growbuf
(
pos
,
str
);
curr
=
xgetc
(
f
);
if
(
xfeof
(
f
))
curr
=
0
;
}
else
{
inquote
=
0
;
}
}
else
{
*
pos
++
=
curr
;
if
(
pos
>=
buf
+
buflen
)
growbuf
(
pos
,
str
);
curr
=
xgetc
(
f
);
if
(
xfeof
(
f
))
curr
=
0
;
}
}
if
(
inquote
)
{
inquote
=
0
;
// runaway quote - but nowhere to complain
}
*
pos
++
=
0
;
if
(
pos
>=
buf
+
buflen
)
growbuf
(
pos
,
str
);
return
str
;
}
TokenLink
*
readtokens
(
FILE
*
f
)
{
char
*
tok
;
TokenLink
*
link
;
TokenLink
*
list
=
0
;
TokenLink
*
last
=
0
;
worklist
=
0
;
resetbuf
(
f
);
if
(
readsep
(
f
,
1
))
return
0
;
do
{
tok
=
readtoken
(
f
);
link
=
new
TokenLink
;
link
->
token
=
tok
;
link
->
next
=
0
;
if
(
!
last
)
worklist
=
last
=
list
=
link
;
else
last
=
last
->
next
=
link
;
}
while
(
!
readsep
(
f
,
0
));
return
list
;
}
void
freetokens
(
TokenLink
*
list
)
{
TokenLink
*
link
=
list
;
while
(
link
)
{
list
=
link
->
next
;
delete
link
;
link
=
list
;
}
}
int
length
(
TokenLink
*
list
)
{
int
len
=
0
;
while
(
list
)
{
list
=
list
->
next
;
len
++
;
}
return
len
;
}
int
is_posint
(
const
char
*
str
)
{
if
(
!*
str
)
return
0
;
while
(
*
str
>=
'0'
&&
*
str
<=
'9'
)
str
++
;
return
(
!*
str
);
}
int
is_negint
(
const
char
*
str
)
{
if
(
!*
str
)
return
0
;
if
(
*
str
==
'-'
)
str
++
;
else
return
0
;
if
(
!*
str
)
return
0
;
while
(
*
str
>=
'0'
&&
*
str
<=
'9'
)
str
++
;
return
(
!*
str
);
}
int
is_cont
(
const
char
*
str
)
{
if
(
!*
str
)
return
0
;
if
(
*
str
==
'-'
)
str
++
;
if
(
!*
str
)
return
0
;
while
(
*
str
>=
'0'
&&
*
str
<=
'9'
)
str
++
;
if
(
*
str
==
'.'
)
str
++
;
while
(
*
str
>=
'0'
&&
*
str
<=
'9'
)
str
++
;
if
(
*
str
==
'e'
||
*
str
==
'E'
)
{
str
++
;
if
(
*
str
==
'-'
||
*
str
==
'+'
)
str
++
;
while
(
*
str
>=
'0'
&&
*
str
<=
'9'
)
str
++
;
}
return
(
!*
str
);
}
int
is_unknown
(
const
char
*
str
)
{
if
(
!*
str
||
(
global_unknown_tag
?
!
strcmp
(
str
,
global_unknown_tag
)
:
0
)
||
((
*
str
==
'*'
||
*
str
==
'-'
||
*
str
==
'?'
)
&&
!*
(
str
+
1
)))
return
1
;
else
return
0
;
}
IF_INT
get_posint
(
const
char
*
str
)
{
long
long
res
;
if
(
sscanf
(
str
,
"%lld"
,
&
res
)
==
1
)
return
res
;
else
return
-
1
;
}
IF_FLOAT
get_cont
(
const
char
*
str
)
{
static
IF_INT
i
=
-
1
;
double
res
;
if
(
sscanf
(
str
,
"%lf"
,
&
res
)
==
1
)
return
res
;
else
return
*
(
IF_FLOAT
*
)
&
i
;
}
C_code Trajectory clustering TBI/dataformat/readtokens.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include "intfloat.hh"
struct
TokenLink
{
char
*
token
;
TokenLink
*
next
;
};
TokenLink
*
readtokens
(
FILE
*
f
);
void
freetokens
(
TokenLink
*
list
);
char
*
freezebuf
();
int
length
(
TokenLink
*
list
);
int
is_posint
(
const
char
*
str
);
int
is_negint
(
const
char
*
str
);
int
is_cont
(
const
char
*
str
);
int
is_unknown
(
const
char
*
str
);
IF_INT
get_posint
(
const
char
*
str
);
IF_FLOAT
get_cont
(
const
char
*
str
);
void
set_selected_separator
(
char
sep
);
void
set_readtoken_separator_char
(
char
sep
);
char
get_readtoken_separator_char
();
void
set_readtoken_unknown_tag
(
const
char
*
unk
);
const
char
*
get_readtoken_unknown_tag
();
void
set_readtoken_quote_delim
(
char
delim
);
char
get_readtoken_quote_delim
();
C_code Trajectory clustering TBI/dataformat/table.cc
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
#include <string.h>
#include "table.hh"
/*
struct IndexTableSlot {
char* key;
int index;
IndexTableSlot* next;
IndexTableSlot* hnext;
};
class IndexTable {
public:
IndexTable();
~IndexTable();
IndexTable* copy();
void insert(const char* str);
void insert_last(const char* str);
void sort(int (*func)(const char*, const char*), int off = 0);
int get(const char* str);
char* nth(int n);
void reset() { curr = first; };
char* next() { if (curr) {char* str=curr->key; curr=curr->next; return str;} return 0; };
int length() { return num; };
protected:
int hash(const char* str);
IndexTableSlot* lookup(const char* str, int insert, int resort);
void resize(int n);
void inlast(IndexTableSlot* ele);
void sortin(int (*func)(const char*, const char*), IndexTableSlot* ele, int reindex);
int size;
int bsize;
int num;
int offset;
int (*sortfunc)(const char*, const char*);
IndexTableSlot** table;
IndexTableSlot* first;
IndexTableSlot* last;
IndexTableSlot* curr;
};
#define FORTABLE( tab, key) for((tab).reset(); (key=(tab).next()); )
*/
IndexTable
::
IndexTable
()
{
int
i
;
IndexTableSlot
**
slot
;
size
=
64
;
bsize
=
6
;
num
=
0
;
offset
=
0
;
sortfunc
=
strcmp
;
table
=
new
IndexTableSlot
*
[
size
];
for
(
i
=
0
,
slot
=
table
;
i
<
size
;
i
++
,
slot
++
)
*
slot
=
0
;
first
=
0
;
last
=
0
;
curr
=
0
;
}
IndexTable
::~
IndexTable
()
{
IndexTableSlot
*
ele
,
*
ele2
;
int
i
;
for
(
i
=
0
;
i
<
size
;
i
++
)
for
(
ele
=
table
[
i
];
ele
;
ele
=
ele2
)
{
ele2
=
ele
->
hnext
;
delete
[]
ele
->
key
;
delete
ele
;
}
delete
[]
table
;
}
IndexTable
*
IndexTable
::
copy
()
{
IndexTable
*
tab
=
new
IndexTable
();
char
*
str
;
FORTABLE
(
*
this
,
str
)
tab
->
insert_last
(
str
);
return
tab
;
}
int
IndexTable
::
hash
(
const
char
*
str
)
{
int
work
=
0
;
unsigned
char
*
p
;
for
(
p
=
(
unsigned
char
*
)
str
;
*
p
;
p
++
)
{
work
^=
*
p
;
work
<<=
1
;
work
=
(
work
>>
bsize
)
^
(
work
&
(
size
-
1
));
}
return
work
;
}
IndexTableSlot
*
IndexTable
::
lookup
(
const
char
*
str
,
int
ins
,
int
resort
)
{
int
index
;
IndexTableSlot
*
ele
;
index
=
hash
(
str
);
for
(
ele
=
table
[
index
];
ele
&&
strcmp
(
str
,
ele
->
key
);
ele
=
ele
->
hnext
);
if
(
ele
)
return
ele
;
else
if
(
ins
)
{
ele
=
new
IndexTableSlot
;
ele
->
key
=
strcpy
(
new
char
[
strlen
(
str
)
+
1
],
str
);
ele
->
hnext
=
table
[
index
];
if
(
resort
)
sortin
(
sortfunc
,
ele
,
1
);
else
inlast
(
ele
);
table
[
index
]
=
ele
;
++
num
;
if
(
num
>
size
*
4
)
{
resize
(
size
*
8
);
}
return
ele
;
}
else
return
0
;
}
void
IndexTable
::
resize
(
int
n
)
{
IndexTableSlot
**
oldtable
;
IndexTableSlot
*
ele
,
*
ele2
;
int
index
;
int
i
;
int
oldsize
;
oldsize
=
size
;
size
=
n
;
for
(
bsize
=
0
;
n
;
bsize
++
,
n
>>=
1
);
oldtable
=
table
;
table
=
new
IndexTableSlot
*
[
size
];
num
=
0
;
for
(
i
=
0
;
i
<
size
;
i
++
)
table
[
i
]
=
0
;
for
(
i
=
0
;
i
<
oldsize
;
i
++
)
for
(
ele
=
oldtable
[
i
];
ele
;
ele
=
ele2
)
{
ele2
=
ele
->
hnext
;
index
=
hash
(
ele
->
key
);
ele
->
hnext
=
table
[
index
];
table
[
index
]
=
ele
;
++
num
;
}
delete
[]
oldtable
;
}
void
IndexTable
::
insert
(
const
char
*
str
)
{
if
(
str
)
lookup
(
str
,
1
,
1
);
}
void
IndexTable
::
insert_last
(
const
char
*
str
)
{
if
(
str
)
lookup
(
str
,
1
,
0
);
}
char
*
IndexTable
::
nth
(
int
n
)
{
IndexTableSlot
*
ele
;
int
i
;
n
-=
offset
;
if
(
n
<
num
&&
n
>=
0
)
{
ele
=
first
;
for
(
i
=
0
;
i
<
n
;
i
++
,
ele
=
ele
->
next
);
return
ele
->
key
;
}
else
return
0
;
}
int
IndexTable
::
get
(
const
char
*
str
)
{
IndexTableSlot
*
ele
;
if
(
str
&&
(
ele
=
lookup
(
str
,
0
,
0
)))
return
ele
->
index
;
else
return
-
1
;
}
void
IndexTable
::
inlast
(
IndexTableSlot
*
ele
)
{
if
(
!
last
)
first
=
last
=
ele
;
else
{
last
->
next
=
ele
;
last
=
ele
;
}
ele
->
next
=
0
;
ele
->
index
=
num
;
}
void
IndexTable
::
sortin
(
int
(
*
func
)(
const
char
*
,
const
char
*
),
IndexTableSlot
*
ele
,
int
reindex
)
{
IndexTableSlot
*
ele2
;
int
ind
;
// Bah, away with efficiency
if
(
!
first
||
func
(
ele
->
key
,
first
->
key
)
<=
0
)
{
ele
->
next
=
first
;
first
=
ele
;
if
(
!
last
)
last
=
first
;
if
(
reindex
)
ele
->
index
=
offset
;
}
else
{
for
(
ele2
=
first
;
ele2
->
next
&&
(
func
(
ele
->
key
,
ele2
->
next
->
key
)
>
0
);
ele2
=
ele2
->
next
);
ele
->
next
=
ele2
->
next
;
ele2
->
next
=
ele
;
if
(
!
ele
->
next
)
last
=
ele
;
if
(
reindex
)
ele
->
index
=
ele2
->
index
+
1
;
}
if
(
reindex
)
for
(
ele2
=
ele
->
next
,
ind
=
ele
->
index
+
1
;
ele2
;
ele2
=
ele2
->
next
,
ind
++
)
ele2
->
index
=
ind
;
}
void
IndexTable
::
sort
(
int
(
*
func
)(
const
char
*
,
const
char
*
),
int
off
)
{
IndexTableSlot
*
ele
;
int
i
,
ind
;
first
=
0
;
offset
=
off
;
sortfunc
=
func
;
for
(
i
=
0
;
i
<
size
;
i
++
)
for
(
ele
=
table
[
i
];
ele
;
ele
=
ele
->
hnext
)
sortin
(
func
,
ele
,
0
);
for
(
ele
=
first
,
ind
=
off
;
ele
;
ele
=
ele
->
next
,
ind
++
)
ele
->
index
=
ind
;
}
C_code Trajectory clustering TBI/dataformat/table.hh
0 → 100644
View file @
63b3a308
/*
--------------------------------------------------------------------------
Copyright (C) 1996, 2012, 2015 Anders Holst <aho@sics.se>
This code is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
struct
IndexTableSlot
{
char
*
key
;
int
index
;
IndexTableSlot
*
next
;
IndexTableSlot
*
hnext
;
};
class
IndexTable
{
public:
IndexTable
();
~
IndexTable
();
IndexTable
*
copy
();
void
insert
(
const
char
*
str
);
void
insert_last
(
const
char
*
str
);
void
sort
(
int
(
*
func
)(
const
char
*
,
const
char
*
),
int
off
=
0
);
int
get
(
const
char
*
str
);
char
*
nth
(
int
n
);
void
reset
()
{
curr
=
first
;
};
char
*
next
()
{
if
(
curr
)
{
char
*
str
=
curr
->
key
;
curr
=
curr
->
next
;
return
str
;}
return
0
;
};
int
length
()
{
return
num
;
};
protected:
int
hash
(
const
char
*
str
);
IndexTableSlot
*
lookup
(
const
char
*
str
,
int
insert
,
int
resort
);
void
resize
(
int
n
);
void
inlast
(
IndexTableSlot
*
ele
);
void
sortin
(
int
(
*
func
)(
const
char
*
,
const
char
*
),
IndexTableSlot
*
ele
,
int
reindex
);
int
size
;
int
bsize
;
int
num
;
int
offset
;
int
(
*
sortfunc
)(
const
char
*
,
const
char
*
);
IndexTableSlot
**
table
;
IndexTableSlot
*
first
;
IndexTableSlot
*
last
;
IndexTableSlot
*
curr
;
};
#define FORTABLE( tab, key) for((tab).reset(); (key=(tab).next()); )
Clustervars_TS.R
0 → 100644
View file @
63b3a308
# (Only) Script used to create clustervars for timeseries clustering
rm
(
list
=
ls
())
library
(
ggplot2
)
library
(
gridExtra
)
library
(
dplyr
)
library
(
tidyr
)
library
(
zoo
)
nas
<-
c
(
""
,
NA
,
NaN
,
Inf
,
-
Inf
)
f2
<-
"/Users/ceciliamac/OneDrive - Karolinska Institutet/"
#Moved all data to onedrive 2110027
f
<-
paste0
(
f2
,
"Clustering CENTER-TBI/Neurobot data/Core3.0/"
)
f3
<-
paste0
(
f2
,
"Clustering Time-series CENTER-TBI/Clustervars_TS/"
)
TP
<-
c
(
0
:
7
)
# Prehosp: --------------------
Prehosp
<-
read.csv
(
paste0
(
f
,
"Prehosp.csv"
),
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
# https://center-tbi.incf.org/_5ee1dcf042f2945a46d94b74
# https://center-tbi.incf.org/_5fde723fb40a4d09be7c0eba
colnames
(
Prehosp
)
<-
gsub
(
".*\\."
,
""
,
colnames
(
Prehosp
))
colnames
(
Prehosp
)
<-
gsub
(
"ED"
,
""
,
colnames
(
Prehosp
))
colnames
(
Prehosp
)
<-
gsub
(
"ArrivalArt"
,
""
,
colnames
(
Prehosp
))
Prehosp
<-
Prehosp
%>%
rename
(
gupi
=
subjectId
,
DateTimeInj
=
TimeInj
,
ASAClass
=
MedHxPreInjASAPSClass
,
Anticoags
=
MedHxAnticoagulantsOrPlatelet
,
HeadISS
=
BestOfHeadBrainCervicalISS
,
Lactate
=
ArrivalLactate
,
LactateUnit
=
ArrivalLactateUnit
,
HRMean
=
ArrHR
,
TempMean
=
ArrTempCelsius
,
SpO2Mean
=
ArrSpO2
,
pHMean
=
ArrivalpH
,
BE
=
ArrivalBaseExcess
,
BMI
=
ArrivalBMIKgCm
,
Height
=
ArrivalHeightCm
,
GOSE6mo.Derived
=
GOSE6monthEndpointDerived
)
#%>%
Prehosp
<-
Prehosp
%>%
mutate
(
GCSMotorBaselineDerived
=
as.integer
(
GCSMotorBaselineDerived
),
GCSScoreBaselineDerived
=
as.integer
(
GCSScoreBaselineDerived
),
ArrSBP
=
ifelse
(
ArrSBP
>
305
|
ArrSBP
<
30
,
NA
,
ArrSBP
),
ArrDBP
=
ifelse
(
ArrDBP
<
10
,
NA
,
ArrDBP
))
Prehosp
<-
Prehosp
%>%
group_by
(
gupi
)
%>%
mutate
(
DateTimeInj
=
as.POSIXct
(
paste0
(
"1970-01-01 "
,
DateTimeInj
)),
MAPMean
=
(
ArrSBP
+2
*
ArrDBP
)
/
3
,
ExtracranISSHighest
=
max
(
BestOfFaceISS
,
BestOfAbdomenPelvicLumbarISS
,
BestOfChestSpineISS
,
BestOfExternaISS
,
BestOfExtremitiesISS
),
Lactate
=
round
(
ifelse
(
as.character
(
LactateUnit
)
%in%
c
(
"mg/dl"
,
"mg/dL"
),
Lactate
*
0.111
,
Lactate
),
1
),
PaO2Mean
=
ifelse
(
!
is.na
(
pO2kPa
),
pO2kPa
,
pO2mmhg
),
PaCO2Mean
=
ifelse
(
!
is.na
(
pCO2kPa
),
pCO2kPa
,
pCO2mmhg
),
Weight
=
ifelse
(
!
is.na
(
ArrivalBodyWeightKg
),
ArrivalBodyWeightKg
,
round
(
ArrivalBodyWeightLbs
*
0.45
,
0
)),
Height
=
ifelse
(
!
is.na
(
Height
),
Height
,
round
(
ArrivalHeightInches
*
2.54
,
0
)))
%>%
select
(
-
(
BestOfChestSpineISS
:
BestOfExtremitiesISS
),
-
LactateUnit
,
-
(
pO2kPa
:
pCO2unit
),
-
ArrivalBodyWeightKg
,
-
ArrivalBodyWeightLbs
,
-
ArrivalHeightInches
,
-
ArrSBP
,
-
ArrDBP
)
Prehosp
<-
Prehosp
%>%
mutate
(
PaO2Mean
=
round
(
ifelse
(
PaO2Mean
>
55
,
PaO2Mean
/
7.5
,
PaO2Mean
),
1
),
PaCO2Mean
=
round
(
ifelse
(
PaCO2Mean
>
15
,
PaCO2Mean
/
7.5
,
PaCO2Mean
),
1
),
BMI
=
ifelse
(
BMI
==
0
|
BMI
==
Inf
,
NA
,
BMI
),
Weight.temp
=
ifelse
(
Weight
<
Height
,
Weight
,
Height
),
Height.temp
=
ifelse
(
Weight
>
Height
,
Weight
,
Height
),
BMI.temp
=
Weight.temp
/
(
Height.temp
/
100
)
^
2
)
%>%
select
(
gupi
:
GCSScoreBaselineDerived
,
GOSE6mo.Derived
,
HeadISS
,
ExtracranISSHighest
,
TotalISS
:
BE
,
PaO2Mean
,
PaCO2Mean
,
MAPMean
,
Height
,
BMI
,
Weight
,
Weight.temp
,
Height.temp
,
BMI.temp
)
Prehosp
<-
Prehosp
%>%
mutate
(
SpO2Mean
=
ifelse
(
SpO2Mean
<
50
,
NA
,
SpO2Mean
),
PaO2Mean
=
round
(
ifelse
(
PaO2Mean
>
100
,
NA
,
PaO2Mean
),
digits
=
1
),
PaCO2Mean
=
ifelse
(
PaCO2Mean
>
30
,
NA
,
PaCO2Mean
),
Height.temp
=
ifelse
(
Height.temp
<
105
&
Age
>=
10
|
Height.temp
<
50
,
NA
,
Height.temp
),
Weight.temp
=
ifelse
(
BMI.temp
<
10
,
NA
,
Weight.temp
),
BMI.temp
=
round
(
Weight.temp
/
(
Height.temp
/
100
)
^
2
,
2
))
Prehosp
<-
Prehosp
%>%
select
(
-
Height
,
-
BMI
,
-
Weight
)
%>%
rename
(
Weight
=
Weight.temp
,
Height
=
Height.temp
,
BMI
=
BMI.temp
,
GOSE6mo
=
GOSE6mo.Derived
)
GCSPrehosp
<-
read.csv
(
paste0
(
f
,
"GCSprehosp.csv"
),
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
#https://center-tbi.incf.org/_60526674d1ac305a9430ff84
colnames
(
GCSPrehosp
)
<-
gsub
(
"Hospital."
,
""
,
colnames
(
GCSPrehosp
))
colnames
(
GCSPrehosp
)
<-
gsub
(
"InjuryHx."
,
""
,
colnames
(
GCSPrehosp
))
colnames
(
GCSPrehosp
)
<-
c
(
"gupi"
,
"GCSPreHospBestEyes"
,
"GCSPreHospBestVerbal"
,
"ICPDevice"
,
"ICPDeviceOther"
,
"ICPStartDate"
,
"ICPStopDate"
,
"EDAirway"
,
"Seizures.Arr"
)
#colnames(GCSPrehosp) <- c("gupi", "GCSEyes", "GCSVerbal", "ICPDevice", "ICPDeviceOther", "ICPStartDate", "ICPStopDate", "EDAirway", "Seizures")
Prehosp
<-
left_join
(
Prehosp
,
GCSPrehosp
%>%
select
(
gupi
,
GCSPreHospBestEyes
,
GCSPreHospBestVerbal
,
EDAirway
,
Seizures.Arr
),
by
=
"gupi"
)
# Prehosp <- left_join(Prehosp, GCSPrehosp %>% select(gupi, GCSEyes, GCSVerbal, EDAirway, Seizures), by="gupi")
Prehosp
<-
Prehosp
%>%
mutate
(
GCSPreHospBestEyes
=
as.integer
(
GCSPreHospBestEyes
),
GCSPreHospBestVerbal
=
as.integer
(
GCSPreHospBestVerbal
),
IntubPrehosp
=
ifelse
(
EDAirway
%in%
c
(
5
,
6
),
1
,
0
),
Seizures.Arr
=
ifelse
(
Seizures.Arr
==
88
,
NA
,
ifelse
(
Seizures.Arr
%in%
c
(
1
:
3
),
1
,
Seizures.Arr
)))
Prehosp
<-
Prehosp
%>%
select
(
-
EDAirway
,
-
DateTimeInj
)
GCSPrehosp
<-
GCSPrehosp
%>%
mutate
(
ICPDevice
=
ifelse
(
grepl
(
"codman"
,
ICPDeviceOther
),
3
,
ifelse
(
grepl
(
"EVD"
,
ICPDeviceOther
),
1
,
ifelse
(
grepl
(
"dural"
,
ICPDeviceOther
),
3
,
ifelse
(
grepl
(
"entricular"
,
ICPDeviceOther
),
2
,
ICPDevice
)))))
Prehosp
<-
Prehosp
%>%
mutate
(
Timepoint
=
0
)
Prehosp
<-
Prehosp
%>%
filter
(
PatientType
==
3
)
%>%
select
(
-
PatientType
)
Prehosp
<-
Prehosp
%>%
filter
(
Age
>=
18
)
Prehosp
<-
Prehosp
%>%
filter
(
!
is.na
(
GOSE6mo
))
Prehosp
[
Prehosp
==
-
Inf
|
Prehosp
==
Inf
]
<-
NA
# Outcome, GOS-E score: ---------------------------------------
GOSE.score
<-
read.csv
(
paste0
(
f
,
"GOSE_all_timepoints.csv"
),
stringsAsFactors
=
FALSE
)
# https://center-tbi.incf.org/_5ee1e46342f2945a46d94b75
# https://center-tbi.incf.org/_5fdf5288b40a4d09be7c0ec1
colnames
(
GOSE.score
)
<-
c
(
"gupi"
,
"Timepoint"
,
"GOSEScore"
)
Prehosp
<-
left_join
(
Prehosp
,
GOSE.score
%>%
filter
(
Timepoint
==
"6mo"
)
%>%
select
(
gupi
,
GOSEScore
),
by
=
"gupi"
)
TP
<-
rep
(
TP
,
each
=
length
(
unique
(
Prehosp
$
gupi
)))
CV
<-
data.frame
(
Timepoint
=
TP
,
gupi
=
unique
(
Prehosp
$
gupi
))
%>%
select
(
gupi
,
Timepoint
)
CV
<-
left_join
(
CV
,
Prehosp
,
by
=
c
(
"gupi"
,
"Timepoint"
))
CV
<-
left_join
(
CV
,
GCSPrehosp
%>%
select
(
gupi
,
contains
(
"ICP"
)))
# CT:----------------------
CT
<-
read.csv
(
paste0
(
f
,
"CT.csv"
),
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
# https://center-tbi.incf.org/_5ee12abd42f2945a46d94b6f (+Fisher)
colnames
(
CT
)
<-
gsub
(
".*\\."
,
""
,
colnames
(
CT
))
# https://center-tbi.incf.org/_5fde73a8b40a4d09be7c0ebb
CT
<-
CT
%>%
mutate
(
ExperimentTime
=
ifelse
(
nchar
(
ExperimentTime
)
==
7
,
paste0
(
"0"
,
ExperimentTime
),
ExperimentTime
))
%>%
rename
(
gupi
=
subjectId
,
EDH
=
EpiduralHematoma
,
aSDH
=
SubduralHematomaAcute
,
tSAH
=
TraumaticSubarachnoidHemorrhage
,
MidlineShiftmm
=
CRFCTMidlineShiftMeasure
)
CT
<-
CT
%>%
mutate
(
CTDateTime
=
paste0
(
ExperimentDate
,
" "
,
ExperimentTime
))
CT
<-
CT
%>%
mutate
(
CTDateTime
=
as.POSIXct
(
CTDateTime
,
format
=
"%Y-%m-%d %H:%M:%S"
),
Timepoint
=
as.numeric
(
difftime
(
strftime
(
as.POSIXct
(
CTDateTime
),
format
=
"%Y-%m-%d"
),
"1969-12-31"
,
unit
=
"days"
)))
CT
<-
CT
%>%
mutate
(
MidlineShiftmm
=
ifelse
(
MidlineShiftmm
>
30
,
NA
,
MidlineShiftmm
))
CT
<-
distinct
(
CT
)
CT
<-
data.frame
(
lapply
(
CT
,
function
(
x
)
{
gsub
(
"absent"
,
0
,
x
)}))
CT
<-
data.frame
(
lapply
(
CT
,
function
(
x
)
{
gsub
(
"present"
,
1
,
x
)}))
CT
<-
data.frame
(
lapply
(
CT
,
function
(
x
)
{
gsub
(
"indeteminate"
,
NA
,
x
)}))
CT
<-
data.frame
(
lapply
(
CT
,
function
(
x
)
{
gsub
(
"uninterpretable"
,
NA
,
x
)}))
CT
[,
c
(
4
:
12
,
14
)]
<-
lapply
(
CT
[,
c
(
4
:
12
,
14
)],
function
(
x
)
{
as.numeric
(
as.character
(
x
))})
CT
<-
CT
%>%
mutate
(
CTDateTime
=
as.POSIXct
(
CTDateTime
))
%>%
select
(
-
ExperimentDate
,
-
ExperimentTime
)
CT
<-
CT
%>%
mutate
(
MidlineShift
=
ifelse
(
MidlineShiftmm
>
5
,
1
,
ifelse
(
is.na
(
MidlineShift
),
0
,
MidlineShift
)))
CT
<-
CT
%>%
group_by
(
gupi
,
Timepoint
)
%>%
summarise_all
(
max
,
na.rm
=
TRUE
)
%>%
select
(
-
CTDateTime
)
CT
[
CT
==-
Inf
]
<-
NA
CV
<-
left_join
(
CV
,
CT
,
by
=
c
(
"gupi"
,
"Timepoint"
))
# Vitals: ------------------------------------
Vitals
<-
read.csv
(
paste0
(
f
,
"Vitals.csv"
),
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
# https://center-tbi.incf.org/_5ee1ebd542f2945a46d94b76
# https://center-tbi.incf.org/_5fde772cb40a4d09be7c0ebc
colnames
(
Vitals
)
<-
gsub
(
".*\\."
,
""
,
colnames
(
Vitals
))
colnames
(
Vitals
)
<-
gsub
(
"DV"
,
""
,
colnames
(
Vitals
))
colnames
(
Vitals
)
<-
gsub
(
"HosComplEvent"
,
""
,
colnames
(
Vitals
))
Vitals
<-
Vitals
%>%
rename
(
gupi
=
subjectId
)
Vitals
<-
Vitals
%>%
filter
(
!
is.na
(
Date
))
Vitals
<-
Vitals
%>%
mutate
(
Timepoint
=
as.numeric
(
difftime
(
strftime
(
as.POSIXct
(
Date
),
format
=
"%Y-%m-%d"
),
"1969-12-31"
,
unit
=
"days"
)),
SBP
=
ifelse
(
SBP
>
300
|
SBP
<
30
,
NA
,
SBP
),
DBP
=
ifelse
(
DBP
<
10
|
is.na
(
SBP
),
NA
,
DBP
),
SBPLow
=
ifelse
(
SBPLow
>
300
|
SBPLow
<
30
,
NA
,
SBPLow
),
DBPLow
=
ifelse
(
DBPLow
<
10
|
is.na
(
SBPLow
),
NA
,
DBPLow
))
Vitals
<-
Vitals
%>%
mutate
(
LowPaO2
=
ifelse
(
!
is.na
(
LowestPaO2kPa
),
LowestPaO2kPa
,
LowestPaO2
),
HighPaO2
=
ifelse
(
!
is.na
(
HighestPaO2kPa
),
HighestPaO2kPa
,
HighestPaO2
),
LowPaCO2
=
ifelse
(
!
is.na
(
LowestPaCO2kPa
),
LowestPaCO2kPa
,
LowestPaCO2
),
HighPaCO2
=
ifelse
(
!
is.na
(
HighestPaCO2kPa
),
HighestPaCO2kPa
,
HighestPaCO2
))
Vitals
<-
Vitals
%>%
mutate
(
LowPaO2
=
round
(
ifelse
(
LowPaO2
<
55
,
LowPaO2
,
LowPaO2
/
7.5
),
digits
=
2
),
HighPaO2
=
round
(
ifelse
(
HighPaO2
<
55
,
HighPaO2
,
HighPaO2
/
7.5
),
digits
=
2
),
LowPaCO2
=
round
(
ifelse
(
LowPaCO2
<
15
,
LowPaCO2
,
LowPaCO2
/
7.5
),
digits
=
2
),
HighPaCO2
=
round
(
ifelse
(
HighPaCO2
<
15
,
HighPaCO2
,
HighPaCO2
/
7.5
),
digits
=
2
))
Vitals
<-
Vitals
%>%
mutate
(
VSBPMax
=
ifelse
(
SBP
<
SBPLow
,
SBPLow
,
SBP
),
VSBPMin
=
ifelse
(
VSBPMax
==
SBPLow
,
SBP
,
SBPLow
),
VDBPMax
=
ifelse
(
SBP
<
SBPLow
,
DBPLow
,
DBP
),
VDBPMin
=
ifelse
(
VSBPMax
==
SBPLow
,
DBP
,
DBPLow
),
HRMin
=
ifelse
(
HRLow
<
HR
,
HRLow
,
HR
),
HRMax
=
ifelse
(
HRLow
<
HR
,
HR
,
HRLow
),
TempMin
=
ifelse
(
TempLowC
<
TempHighC
,
TempLowC
,
TempHighC
),
TempMax
=
ifelse
(
TempLowC
<
TempHighC
,
TempHighC
,
TempLowC
),
PaO2Min
=
ifelse
(
LowPaO2
<
HighPaO2
,
LowPaO2
,
HighPaO2
),
PaO2Max
=
ifelse
(
LowPaO2
<
HighPaO2
,
HighPaO2
,
LowPaO2
),
PaCO2Min
=
ifelse
(
LowPaCO2
<
HighPaCO2
,
LowPaCO2
,
HighPaCO2
),
PaCO2Max
=
ifelse
(
LowPaCO2
<
HighPaCO2
,
HighPaCO2
,
LowPaCO2
),
SpO2Min
=
ifelse
(
SpO2Low
<
SpO2
,
SpO2Low
,
SpO2
),
SpO2Max
=
ifelse
(
SpO2Low
<
SpO2
,
SpO2
,
SpO2Low
),
pHMin
=
ifelse
(
LowestpH
<
HighestpH
,
LowestpH
,
HighestpH
),
pHMax
=
ifelse
(
LowestpH
<
HighestpH
,
HighestpH
,
LowestpH
),
PupilReactivityWorst
=
ifelse
(
WorstPupilReactivityLghtLftEyeResult
%in%
c
(
1
,
2
)
&
WorstPupilReactivityLghtRghtEyeResult
%in%
c
(
1
,
2
),
0
,
ifelse
(
WorstPupilReactivityLghtLftEyeResult
%in%
c
(
1
,
2
)
&
WorstPupilReactivityLghtRghtEyeResult
==
3
,
1
,
ifelse
(
WorstPupilReactivityLghtLftEyeResult
==
3
&
WorstPupilReactivityLghtRghtEyeResult
%in%
c
(
1
,
2
),
1
,
ifelse
(
WorstPupilReactivityLghtLftEyeResult
==
3
&
WorstPupilReactivityLghtRghtEyeResult
==
3
,
2
,
NA
)))),
PupilReactivityBest
=
ifelse
(
PupilReactivityLghtLftEyeReslt
%in%
c
(
1
,
2
)
&
PupilReactivityLghtRtEyeReslt
%in%
c
(
1
,
2
),
0
,
ifelse
(
PupilReactivityLghtLftEyeReslt
%in%
c
(
1
,
2
)
&
PupilReactivityLghtRtEyeReslt
==
3
,
1
,
ifelse
(
PupilReactivityLghtLftEyeReslt
==
3
&
PupilReactivityLghtRtEyeReslt
%in%
c
(
1
,
2
),
1
,
ifelse
(
PupilReactivityLghtLftEyeReslt
==
3
&
PupilReactivityLghtRtEyeReslt
==
3
,
2
,
NA
)))))
Vitals
<-
Vitals
%>%
mutate
(
VMAPMax
=
round
((
VSBPMax
+
2
*
VDBPMax
)
/
3
,
2
),
VMAPMin
=
round
((
VSBPMin
+
2
*
VDBPMin
)
/
3
,
2
),)
Vitals
<-
Vitals
%>%
select
(
gupi
,
Timepoint
,
Seizures
:
Hypocapnia
,
VSBPMax
:
VMAPMin
,
GCSEyes
:
GCSScore
,
GCSWorstEyes
:
GCSWorstScore
)
%>%
mutate
(
GCSEyes
=
as.numeric
(
GCSEyes
),
GCSMotor
=
as.numeric
(
GCSMotor
),
GCSVerbal
=
as.numeric
(
GCSVerbal
),
GCSScore
=
as.numeric
(
GCSScore
),
GCSWorstEyes
=
as.numeric
(
GCSWorstEyes
),
GCSWorstMotor
=
as.numeric
(
GCSWorstMotor
),
GCSWorstVerbal
=
as.numeric
(
GCSWorstVerbal
),
GCSWorstScore
=
as.numeric
(
GCSWorstScore
))
Vitals.max
<-
Vitals
%>%
select
(
gupi
,
Timepoint
,
Seizures
:
Hypocapnia
,
contains
(
"Max"
),
PupilReactivityWorst
,
GCSEyes
:
GCSWorstScore
)
%>%
group_by
(
gupi
,
Timepoint
)
%>%
summarise_all
(
max
,
na.rm
=
TRUE
)
Vitals.min
<-
Vitals
%>%
select
(
gupi
,
Timepoint
,
contains
(
"Min"
),
PupilReactivityBest
)
%>%
group_by
(
gupi
,
Timepoint
)
%>%
summarise_all
(
min
,
na.rm
=
TRUE
)
Vitals
<-
full_join
(
Vitals.max
,
Vitals.min
,
by
=
c
(
"gupi"
,
"Timepoint"
))
rm
(
Vitals.max
,
Vitals.min
)
Vitals
<-
Vitals
%>%
rename
(
PupilResponse
=
PupilReactivityBest
)
Vitals
[
Vitals
==
Inf
]
<-
NA
Vitals
[
Vitals
==-
Inf
]
<-
NA
View
(
Vitals
%>%
filter
(
Timepoint
<=
7
))
CV
<-
left_join
(
CV
,
Vitals
,
by
=
c
(
"gupi"
,
"Timepoint"
))
# Labs: -----------------------
Labs
<-
read.csv
(
paste0
(
f
,
"Labs.csv"
),
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
# https://center-tbi.incf.org/_5ee1da5842f2945a46d94b71
# https://center-tbi.incf.org/_5fdf4db8b40a4d09be7c0ebd
colnames
(
Labs
)
<-
gsub
(
".*\\.DL"
,
""
,
colnames
(
Labs
))
colnames
(
Labs
)
<-
gsub
(
".*\\."
,
""
,
colnames
(
Labs
))
Labs
<-
Labs
%>%
mutate
(
DateTime
=
as.POSIXct
(
paste0
(
Date
,
" "
,
Time
),
format
=
"%Y-%m-%d %H:%M:%S"
))
%>%
rename
(
gupi
=
subjectId
)
Labs
<-
Labs
%>%
mutate
(
Timepoint
=
as.integer
(
difftime
(
strftime
(
as.POSIXct
(
DateTime
),
format
=
"%Y-%m-%d"
),
"1969-12-31"
,
unit
=
"days"
)),
SodiummmolL
=
ifelse
(
is.na
(
SodiummmolL
)
&
!
is.na
(
SodiumOther
),
SodiumOther
,
SodiummmolL
),
HbgL
=
ifelse
(
!
is.na
(
HemoglobingdL
),
HemoglobingdL
*
10
,
ifelse
(
HemoglobinOtherUnit
==
"g/L"
,
HemoglobinOther
,
NA
)),
AlbumingL
=
ifelse
(
AlbumingL
>
100
,
AlbuminOther
,
AlbumingL
))
Labs
<-
Labs
%>%
mutate
(
HbgL
=
ifelse
(
HbgL
<
20
|
HbgL
>
210
,
NA
,
HbgL
),
SodiummmolL
=
ifelse
(
SodiummmolL
>
185
,
NA
,
SodiummmolL
),
AlbumingL
=
ifelse
(
AlbumingL
>
100
|
AlbumingL
==
0
,
NA
,
AlbumingL
),
CreatinineumolL
=
ifelse
(
CreatinineumolL
<
10
,
NA
,
CreatinineumolL
))
%>%
select
(
gupi
,
DateTime
,
Timepoint
,
SodiummmolL
,
GlucosemmolL
,
S100BugL
,
AlbumingL
,
Platelet10_5L
,
CreatinineumolL
,
HbgL
)
Labs
<-
Labs
%>%
group_by
(
gupi
,
Timepoint
)
%>%
summarise
(
SodiumMax
=
max
(
SodiummmolL
,
na.rm
=
TRUE
),
SodiumMin
=
min
(
SodiummmolL
,
na.rm
=
TRUE
),
SodiumMean
=
mean
(
SodiummmolL
,
na.rm
=
TRUE
),
GlucoseMax
=
max
(
GlucosemmolL
,
na.rm
=
TRUE
),
GlucoseMin
=
min
(
GlucosemmolL
,
na.rm
=
TRUE
),
GlucoseMean
=
mean
(
GlucosemmolL
,
na.rm
=
TRUE
),
AlbuminMax
=
max
(
AlbumingL
,
na.rm
=
TRUE
),
AlbuminMin
=
min
(
AlbumingL
,
na.rm
=
TRUE
),
AlbuminMean
=
mean
(
AlbumingL
,
na.rm
=
TRUE
),
PlateletMax
=
max
(
Platelet10_5L
,
na.rm
=
TRUE
),
PlateletMin
=
round
(
min
(
Platelet10_5L
,
na.rm
=
TRUE
),
0
),
PlateletMean
=
round
(
mean
(
Platelet10_5L
,
na.rm
=
TRUE
),
0
),
CreatinineMax
=
round
(
max
(
CreatinineumolL
,
na.rm
=
TRUE
),
0
),
CreatinineMin
=
round
(
max
(
CreatinineumolL
,
na.rm
=
TRUE
)),
CreatinineMean
=
round
(
mean
(
CreatinineumolL
,
na.rm
=
TRUE
),
0
),
HbMax
=
round
(
max
(
HbgL
,
na.rm
=
TRUE
),
0
),
HbMin
=
round
(
min
(
HbgL
,
na.rm
=
TRUE
),
0
),
HbMean
=
round
(
mean
(
HbgL
,
na.rm
=
TRUE
),
0
))
Labs
<-
Labs
%>%
mutate
(
SodiumDiff
=
SodiumMax
-
SodiumMin
,
GlucoseDiff
=
GlucoseMax
-
GlucoseMin
)
Labs
[
Labs
==-
Inf
]
<-
NA
Labs
[
Labs
==
NaN
]
<-
NA
Labs
[
Labs
==
Inf
]
<-
NA
CV
<-
left_join
(
CV
,
Labs
,
by
=
c
(
"gupi"
,
"Timepoint"
))
# DailyTIL: --------------------
# Craniectomy:
DecrCraniectomy
<-
read.csv
(
paste0
(
f
,
"Decompr_craniectomy.csv"
),
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
# https://center-tbi.incf.org/_5ee1dadb42f2945a46d94b72
# https://center-tbi.incf.org/_5fdf4e46b40a4d09be7c0ebe
colnames
(
DecrCraniectomy
)
<-
gsub
(
".*\\."
,
""
,
colnames
(
DecrCraniectomy
))
DecrCraniectomy
<-
DecrCraniectomy
%>%
mutate
(
SurgeryStartDateTime
=
as.POSIXct
(
paste0
(
SurgeryStartDate
,
" "
,
SurgeryStartTime
),
format
=
"%Y-%m-%d %H:%M:%S"
),
Timepoint
=
as.integer
(
difftime
(
strftime
(
as.POSIXct
(
SurgeryStartDateTime
),
format
=
"%Y-%m-%d"
),
"1969-12-31"
)))
%>%
rename
(
gupi
=
subjectId
)
DecrCraniectomy
<-
DecrCraniectomy
%>%
mutate
(
DecompressiveCran
=
ifelse
(
SurgeryDescCranial
%in%
c
(
7
,
71
,
72
)
|
!
is.na
(
DecompressiveSize
)
|
!
is.na
(
DecompressiveCranLocation
)
|
!
is.na
(
DecompressiveCranReason
),
1
,
DecompressiveCran
))
%>%
filter
(
!
is.na
(
DecompressiveCran
))
%>%
select
(
gupi
,
Timepoint
,
DecompressiveCran
,
SurgeryStartDateTime
)
DecrCraniectomy
<-
DecrCraniectomy
%>%
mutate
(
CraniectomyDateTime
=
ifelse
(
DecompressiveCran
==
0
,
NA
,
SurgeryStartDateTime
))
DecrCraniectomy
<-
DecrCraniectomy
%>%
mutate
(
CraniectomyDateTime
=
as.POSIXct
(
CraniectomyDateTime
,
origin
=
"1970-01-01"
))
DecrCraniectomy
<-
DecrCraniectomy
%>%
group_by
(
gupi
)
%>%
filter
(
CraniectomyDateTime
==
min
(
CraniectomyDateTime
))
DecrCraniectomy
<-
distinct
(
DecrCraniectomy
)
%>%
select
(
-
SurgeryStartDateTime
)
# DailyTIL: ----------------------------------------
DailyTIL
<-
read.csv
(
paste0
(
f
,
"DailyTIL.csv"
),
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
# https://center-tbi.incf.org/_5ee1efa042f2945a46d94b79
# https://center-tbi.incf.org/_5fdf4fa4b40a4d09be7c0ebf
colnames
(
DailyTIL
)
<-
gsub
(
".*\\.TIL"
,
""
,
colnames
(
DailyTIL
))
colnames
(
DailyTIL
)
<-
gsub
(
".*\\."
,
""
,
colnames
(
DailyTIL
))
DailyTIL
<-
DailyTIL
%>%
rename
(
gupi
=
subjectId
,
CSFDrainageVol
=
CCSFDrainageVolume
,
Vasopressor
=
FluidLoadingVasopressor
,
TotalTIL
=
TotalTIL
,
DecomprCran
=
ICPSurgeryDecomCranectomy
)
DailyTIL
<-
DailyTIL
%>%
mutate
(
DateTime
=
as.POSIXct
(
paste0
(
Date
,
" "
,
Time
),
format
=
"%Y-%m-%d %H:%M:%S"
))
%>%
filter
(
!
is.na
(
DateTime
))
%>%
filter
(
TherapyIntensityNotDone
!=
1
)
%>%
mutate
(
FluidOutUrine
=
ifelse
(
is.na
(
FluidOutUrine
),
0
,
FluidOutUrine
),
FluidOutGastric
=
ifelse
(
is.na
(
FluidOutGastric
),
0
,
FluidOutGastric
),
FluidOutCSFDrain
=
ifelse
(
is.na
(
FluidOutCSFDrain
),
0
,
FluidOutCSFDrain
),
FluidOutOther
=
ifelse
(
is.na
(
FluidOutOther
),
0
,
FluidOutOther
),
FluidColloidsType
=
ifelse
(
FluidColloidsType
==
1
,
"Albumin 5%"
,
ifelse
(
FluidColloidsType
==
2
,
"Albumin 20%"
,
ifelse
(
FluidColloidsType
==
3
,
"Dextran"
,
ifelse
(
FluidColloidsType
==
4
,
"Gelatin"
,
ifelse
(
FluidColloidsType
==
5
,
"HES"
,
ifelse
(
FluidColloidsType
==
6
,
"Tetrastarches"
,
NA
)))))))
DailyTIL
<-
DailyTIL
%>%
mutate
(
Timepoint
=
as.integer
(
difftime
(
strftime
(
as.POSIXct
(
DateTime
),
format
=
"%Y-%m-%d"
),
"1969-12-31"
)))
DailyTIL
<-
DailyTIL
%>%
mutate
(
FluidBal
=
FluidIn
-
(
FluidOutUrine
+
FluidOutGastric
+
FluidOutCSFDrain
+
FluidOutOther
))
%>%
select
(
-
(
FluidOutUrine
:
FluidIn
),
-
Date
,
-
Time
)
%>%
group_by
(
gupi
,
Timepoint
)
%>%
summarise_all
(
max
,
na.rm
=
TRUE
)
DailyTIL
[
DailyTIL
==
-
Inf
]
<-
NA
# Propagation of decompressive craniectomy:
Craniectomy
<-
DailyTIL
%>%
filter
(
DecomprCran
==
1
)
%>%
select
(
gupi
,
Timepoint
,
DecomprCran
,
DateTime
)
%>%
group_by
(
gupi
)
%>%
filter
(
Timepoint
==
min
(
Timepoint
))
Craniectomy.temp
<-
left_join
(
Craniectomy
,
DecrCraniectomy
,
by
=
c
(
"gupi"
,
"Timepoint"
))
DailyTIL
$
DecomprCran.New
<-
NA
for
(
i
in
unique
(
Craniectomy
$
gupi
))
{
min.timepoint
<-
Craniectomy
$
Timepoint
[
which
(
Craniectomy
$
gupi
==
i
)]
DailyTIL
$
DecomprCran.New
[
which
(
DailyTIL
$
gupi
==
i
&
DailyTIL
$
Timepoint
>=
min.timepoint
)]
<-
1
}
rm
(
Craniectomy
,
Craniectomy.temp
)
DailyTIL
<-
DailyTIL
%>%
mutate
(
DecomprCran.New
=
ifelse
(
is.na
(
DecomprCran.New
),
0
,
DecomprCran.New
))
DailyTIL
<-
DailyTIL
%>%
mutate
(
TotalTIL.New
=
TotalTIL
-
5
*
DecomprCran
+
5
*
DecomprCran.New
)
%>%
select
(
gupi
,
Timepoint
,
TotalTIL
,
TotalTIL.New
,
Position
:
DecomprCran
,
DecomprCran.New
,
everything
())
DailyTIL
<-
DailyTIL
%>%
mutate
(
SedationTherapy
=
ifelse
(
SedationMetabolic
==
1
,
3
,
ifelse
(
SedationHigher
==
1
,
2
,
ifelse
(
Sedation
==
1
,
1
,
0
))),
CSFDrainageTherapy
=
ifelse
(
CSFDrainage
==
0
,
0
,
ifelse
(
CSFDrainageVol
<
120
,
1
,
2
)),
HyperventTherapy
=
ifelse
(
HyperventilationIntensive
==
1
,
3
,
ifelse
(
HyperventilationModerate
==
1
,
2
,
ifelse
(
Hyperventilation
==
1
,
1
,
0
))),
Mannitol
=
ifelse
(
HyperosmolarThearpy
==
1
,
1
,
ifelse
(
HyperosomolarTherapyMannitolGreater2g
,
2
,
0
)),
Saline
=
ifelse
(
HyperosomolarTherapyHigher
==
1
,
2
,
ifelse
(
HyperosomolarTherapyHypertonicLow
==
1
,
1
,
0
)),
Tempcontrol
=
ifelse
(
FeverHypothermia
==
1
,
3
,
ifelse
(
FeverMildHypothermia
==
1
,
2
,
ifelse
(
Fever
==
1
,
1
,
0
))))
DailyTIL
<-
DailyTIL
%>%
select
(
gupi
,
Timepoint
,
TotalTIL.New
,
Position
,
PositionNursedFlat
,
SedationTherapy
,
SedationNeuromuscular
,
CSFDrainageTherapy
,
CSFDrainageVol
,
FluidLoading
,
FluidBal
,
Vasopressor
,
HyperventTherapy
,
Mannitol
,
Saline
,
Tempcontrol
,
ICPSurgery
,
DecomprCran.New
)
colnames
(
DailyTIL
)
<-
gsub
(
".New"
,
""
,
colnames
(
DailyTIL
))
DailyTIL
<-
DailyTIL
%>%
rename
(
NeuromuscBlock
=
SedationNeuromuscular
)
CV
<-
left_join
(
CV
,
DailyTIL
,
by
=
c
(
"gupi"
,
"Timepoint"
))
# CV <- full_join(CV, DailyTIL, by=c("gupi", "Timepoint"))
# Intub: -----------------
intub
<-
read.csv
(
paste0
(
f
,
"intubicpdrugs.csv"
),
na.strings
=
nas
,
stringsAsFactors
=
FALSE
)
# https://center-tbi.incf.org/_6030b7ab683ae72286c7c75d
colnames
(
intub
)
<-
gsub
(
".*\\."
,
""
,
colnames
(
intub
))
intub
<-
intub
%>%
rename
(
gupi
=
subjectId
)
%>%
select
(
gupi
,
Intubation
,
IntubationStartDate
,
IntubationStopDate
)
intub
$
IntubationStartDate
<-
gsub
(
"1971"
,
"1970"
,
intub
$
IntubationStartDate
)
intub
$
IntubationStopDate
<-
gsub
(
"1971"
,
"1970"
,
intub
$
IntubationStopDate
)
CV
<-
left_join
(
CV
,
intub
,
by
=
"gupi"
)
CV
<-
CV
%>%
mutate
(
Intubation2
=
ifelse
(
Intubation
==
1
&
IntubationStartDate
<=
(
as.Date
(
"1969-12-31"
)
+
Timepoint
),
1
,
0
))
CV
<-
CV
%>%
mutate
(
Intubation3
=
ifelse
(
Intubation2
==
1
&
is.na
(
IntubationStopDate
),
1
,
ifelse
(
Intubation2
==
1
&
IntubationStopDate
>=
(
as.Date
(
"1969-12-31"
)
+
Timepoint
),
1
,
0
)))
CV
<-
CV
%>%
mutate
(
Intubation3
=
ifelse
(
is.na
(
Intubation3
),
0
,
Intubation3
))
CV
<-
CV
%>%
mutate
(
Intubation3
=
ifelse
(
Timepoint
==
0
&
IntubPrehosp
==
1
,
1
,
Intubation3
))
CV
<-
CV
%>%
select
(
-
Intubation
,
-
Intubation2
,
-
IntubationStartDate
,
-
IntubationStopDate
,
-
IntubPrehosp
)
%>%
rename
(
Intub
=
Intubation3
)
# HourlyMeasurements: ----------------------
HV
<-
read.csv
(
paste0
(
f
,
"HV.csv"
),
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
# https://center-tbi.incf.org/_5ee1db3e42f2945a46d94b73
# https://center-tbi.incf.org/_5fdf501db40a4d09be7c0ec0
colnames
(
HV
)
<-
gsub
(
".*\\.HV"
,
""
,
colnames
(
HV
))
colnames
(
HV
)
<-
gsub
(
".*\\."
,
""
,
colnames
(
HV
))
HV
<-
HV
%>%
rename
(
gupi
=
subjectId
)
HV
<-
HV
%>%
mutate
(
MeasurementDateTime
=
as.POSIXct
(
MeasurementDateTime
),
SBPtemp
=
ifelse
(
SBP
>
DBP
,
SBP
,
DBP
),
DBPtemp
=
ifelse
(
DBP
<
SBP
,
DBP
,
SBP
),
Timepoint
=
as.integer
(
difftime
(
strftime
(
as.POSIXct
(
MeasurementDateTime
),
format
=
"%Y-%m-%d"
),
"1969-12-31"
)),
ICP
=
ifelse
(
gupi
==
"8whv597"
,
round
(
ICP
/
1.3595
,
0
),
ICP
))
HV
<-
HV
%>%
select
(
-
SBP
,
-
DBP
)
%>%
rename
(
SBP
=
SBPtemp
,
DBP
=
DBPtemp
)
HV
<-
HV
%>%
mutate
(
ICP
=
ifelse
(
ICP
>
140
|
ICP
<
-10
,
NA
,
ICP
),
SBP
=
ifelse
(
SBP
>
305
|
SBP
<
30
,
NA
,
SBP
),
DBP
=
ifelse
(
DBP
<
10
,
NA
,
DBP
),
PP
=
SBP
-
DBP
,
MAP
=
round
((
SBP
+
2
*
DBP
)
/
3
,
0
))
%>%
filter
(
!
is.na
(
Timepoint
))
HV
<-
HV
%>%
group_by
(
gupi
,
Timepoint
,
MeasurementDateTime
)
%>%
summarise
(
ICP
=
mean
(
ICP
,
na.rm
=
TRUE
),
SBP
=
mean
(
SBP
,
na.rm
=
TRUE
),
DBP
=
mean
(
DBP
,
na.rm
=
TRUE
),
PP
=
mean
(
PP
,
na.rm
=
TRUE
),
MAP
=
mean
(
MAP
,
na.rm
=
TRUE
))
%>%
# Take care of patients with multiple forms for each day
select
(
-
MeasurementDateTime
)
HV
<-
HV
%>%
group_by
(
gupi
,
Timepoint
)
%>%
summarise
(
HVMAPMean
=
round
(
mean
(
MAP
,
na.rm
=
TRUE
),
digits
=
1
),
HVMAPMin
=
min
(
MAP
,
na.rm
=
TRUE
),
HVMAPMax
=
max
(
MAP
,
na.rm
=
TRUE
),
HVMAPSD
=
round
(
sd
(
MAP
,
na.rm
=
TRUE
),
digits
=
1
),
HVMAP25
=
quantile
(
MAP
,
0.25
,
na.rm
=
TRUE
),
HVMAP75
=
quantile
(
MAP
,
0.75
,
na.rm
=
TRUE
),
HVSBPMean
=
round
(
mean
(
SBP
,
na.rm
=
TRUE
),
digits
=
1
),
HVSBPMin
=
min
(
SBP
,
na.rm
=
TRUE
),
HVSBPMax
=
max
(
SBP
,
na.rm
=
TRUE
),
HVSBPSD
=
round
(
sd
(
SBP
,
na.rm
=
TRUE
),
digits
=
1
),
HVSBP25
=
quantile
(
SBP
,
0.25
,
na.rm
=
TRUE
),
HVSBP75
=
quantile
(
SBP
,
0.75
,
na.rm
=
TRUE
),
HVDBPMean
=
round
(
mean
(
DBP
,
na.rm
=
TRUE
),
digits
=
1
),
HVDBPMin
=
min
(
DBP
,
na.rm
=
TRUE
),
HVDBPMax
=
max
(
DBP
,
na.rm
=
TRUE
),
HVDBPSD
=
round
(
sd
(
DBP
,
na.rm
=
TRUE
),
digits
=
1
),
HVDBP25
=
quantile
(
DBP
,
0.25
,
na.rm
=
TRUE
),
HVDBP75
=
quantile
(
DBP
,
0.75
,
na.rm
=
TRUE
),
ICPMean
=
round
(
mean
(
ICP
,
na.rm
=
TRUE
),
digits
=
1
),
ICPMin
=
min
(
ICP
,
na.rm
=
TRUE
),
ICPMax
=
max
(
ICP
,
na.rm
=
TRUE
),
ICPSD
=
round
(
sd
(
ICP
,
na.rm
=
TRUE
),
digits
=
1
),
ICP25
=
quantile
(
ICP
,
0.25
,
na.rm
=
TRUE
),
ICP75
=
quantile
(
ICP
,
0.75
,
na.rm
=
TRUE
),
ICPabove20
=
sum
(
ICP
>=
20
,
na.rm
=
TRUE
)
/
12
,
ICPabove25
=
sum
(
ICP
>=
25
,
na.rm
=
TRUE
)
/
12
,
ICPabove30
=
sum
(
ICP
>=
30
,
na.rm
=
TRUE
)
/
12
)
HV
<-
HV
%>%
mutate
(
HVMAPDiff
=
HVMAPMax
-
HVMAPMin
,
HVSBPDiff
=
HVSBPMax
-
HVSBPMin
,
HVDBPDiff
=
HVDBPMax
-
HVDBPMin
,
ICPDiff
=
ICPMax
-
ICPMin
)
CV
<-
left_join
(
CV
,
HV
,
by
=
c
(
"gupi"
,
"Timepoint"
))
#CV <- full_join(CV, HV, by=c("gupi", "Timepoint"))
# Death or discharge: ---------------------
deathdisch
<-
read.csv
(
paste0
(
f
,
"deathdischarge.csv"
),
na.strings
=
nas
,
stringsAsFactors
=
FALSE
)
colnames
(
deathdisch
)
<-
c
(
"gupi"
,
"ICUDischDate"
,
"DeathDate"
)
CV
<-
left_join
(
CV
,
deathdisch
,
by
=
"gupi"
)
CV
<-
CV
%>%
mutate
(
Discharged
=
ifelse
(
ICUDischDate
<=
as.Date
(
"1969-12-31"
)
+
Timepoint
,
1
,
0
),
Dead
=
ifelse
(
DeathDate
<=
as.Date
(
"1969-12-31"
)
+
Timepoint
,
1
,
0
))
CV
<-
CV
%>%
mutate
(
Dead
=
ifelse
(
is.na
(
DeathDate
),
0
,
Dead
),
Discharged
=
ifelse
(
is.na
(
ICUDischDate
),
0
,
Discharged
))
%>%
select
(
-
ICUDischDate
,
-
DeathDate
)
CV
<-
CV
%>%
mutate
(
Discharged
=
ifelse
(
Dead
==
1
,
0
,
Discharged
))
# Biomarkers: ----------------------------
Biomarkers
<-
read.csv
(
"/Users/ceciliamac/OneDrive - Karolinska Institutet/Clustering CENTER-TBI/Neurobot data/Biomarkers.csv"
,
stringsAsFactors
=
FALSE
,
na.strings
=
nas
)
Biomarkers
<-
Biomarkers
%>%
select
(
GUPI
,
CollectionDate
,
CollectionTime
,
S100B
:
NFL
)
%>%
rename
(
gupi
=
GUPI
)
Biomarkers
<-
Biomarkers
%>%
mutate
(
Timepoint
=
as.integer
(
difftime
(
strftime
(
as.POSIXct
(
CollectionDate
),
format
=
"%Y-%m-%d"
),
"1969-12-31"
)))
# Remove biomarker outliers:
q
<-
Biomarkers
%>%
select
(
S100B
:
NFL
)
%>%
summarise_all
(
funs
(
quantile
(
.
,
.975
,
na.rm
=
TRUE
)))
Biomarkers
<-
Biomarkers
%>%
mutate
(
S100B
=
ifelse
(
S100B
>=
q
$
S100B
,
NA
,
S100B
),
NSE
=
ifelse
(
NSE
>=
q
$
NSE
,
NA
,
NSE
),
GFAP
=
ifelse
(
GFAP
>=
q
$
GFAP
,
NA
,
GFAP
),
UCH.L1
=
ifelse
(
UCH.L1
>=
q
$
UCH.L1
,
NA
,
UCH.L1
),
Tau
=
ifelse
(
Tau
>=
q
$
Tau
,
NA
,
Tau
),
NFL
=
ifelse
(
NFL
>=
q
$
NFL
,
NA
,
NFL
))
rm
(
q
)
Biomarkers
<-
Biomarkers
%>%
group_by
(
gupi
,
Timepoint
)
%>%
mutate
(
S100B
=
mean
(
S100B
,
na.rm
=
TRUE
),
NSE
=
mean
(
NSE
,
na.rm
=
TRUE
),
GFAP
=
mean
(
GFAP
,
na.rm
=
TRUE
),
UCH.L1
=
mean
(
UCH.L1
,
na.rm
=
TRUE
),
Tau
=
mean
(
Tau
,
na.rm
=
TRUE
),
NFL
=
mean
(
NFL
,
na.rm
=
TRUE
))
%>%
select
(
-
CollectionDate
,
-
CollectionTime
)
Biomarkers
<-
distinct
(
Biomarkers
)
Biomarkers
[
Biomarkers
==-
Inf
]
<-
NA
CV
<-
left_join
(
CV
,
Biomarkers
,
by
=
c
(
"gupi"
,
"Timepoint"
))
#CV.imp <- left_join(CV, BM.imp, by=c("gupi", "Timepoint"))
# Put all together: ------------------------------------------
# Merge Values at Timepoint 0 and other days:
CV
[
CV
==
Inf
|
CV
==
-
Inf
]
<-
NA
CV
<-
CV
%>%
mutate
(
PupilResponse
=
ifelse
(
Timepoint
==
0
,
PupilsBaselineDerived
,
PupilResponse
),
GCSMotor
=
ifelse
(
Timepoint
==
0
,
GCSMotorBaselineDerived
,
GCSMotor
),
GCSScore
=
ifelse
(
Timepoint
==
0
,
GCSScoreBaselineDerived
,
GCSScore
),
Hypoxia
=
ifelse
(
Timepoint
==
0
,
ComplEventHypoxia
,
Hypoxia
),
Hypotension
=
ifelse
(
Timepoint
==
0
,
ComplEventHypotension
,
Hypotension
),
GCSEyes
=
ifelse
(
Timepoint
==
0
,
GCSPreHospBestEyes
,
GCSEyes
),
GCSVerbal
=
ifelse
(
Timepoint
==
0
,
GCSPreHospBestVerbal
,
GCSVerbal
),
Seizures
=
ifelse
(
Timepoint
==
0
,
Seizures.Arr
,
Seizures
))
#%>%
CV
<-
CV
%>%
select
(
-
PupilsBaselineDerived
,
-
GCSMotorBaselineDerived
,
-
GCSScoreBaselineDerived
,
-
Weight
,
-
Height
,
-
GCSPreHospBestEyes
,
-
GCSPreHospBestVerbal
,
-
Seizures.Arr
,
-
ComplEventHypotension
,
-
ComplEventHypoxia
)
# Cleaning and imputation: --------------------
CV
<-
CV
%>%
mutate
(
ICPMonitor
=
ifelse
(
!
is.na
(
ICPDevice
)
&
ICPStartDate
<=
(
as.Date
(
"1969-12-31"
)
+
Timepoint
)
&
(
ICPStopDate
>=
(
as.Date
(
"1969-12-31"
)
+
Timepoint
)
|
is.na
(
ICPStopDate
)),
ICPDevice
,
0
))
CV
<-
CV
%>%
mutate
(
ICPMonitor
=
ifelse
(
gupi
==
"5pWZ477"
&
ICPMonitor
==
99
,
1
,
ICPMonitor
))
CV
<-
CV
%>%
mutate
(
ICPMonitor
=
ifelse
(
gupi
==
"5pWZ477"
&
Timepoint
==
1
,
3
,
ICPMonitor
))
CV
<-
CV
%>%
select
(
-
ICPDevice
,
-
ICPDeviceOther
,
-
ICPStartDate
,
-
ICPStopDate
)
CV
<-
CV
%>%
rename
(
ICPMonitorType
=
ICPMonitor
)
# Unknown (88) are set to NA. Hypoxia, hypotension, anticoags, hypocapnia, seizures,
# are made binary. Implausible values are set to NA.
CV
<-
CV
%>%
mutate
(
Sex
=
ifelse
(
Sex
==
"M"
,
1
,
0
),
ASAClass
=
ifelse
(
ASAClass
==
88
,
NA
,
ASAClass
),
InjType
=
ifelse
(
InjType
==
88
,
NA
,
InjType
),
InjCause
=
ifelse
(
InjCause
%in%
c
(
88
,
99
),
NA
,
InjCause
),
GOSE6mo
=
ifelse
(
is.na
(
GOSE6mo
),
GOSEScore
,
GOSE6mo
),
PupilResponse
=
ifelse
(
PupilResponse
%in%
c
(
88
,
66
),
NA
,
PupilResponse
),
Lactate
=
ifelse
(
Lactate
>
30
|
Lactate
<
0
,
NA
,
Lactate
),
Hypoxia
=
ifelse
(
Hypoxia
==
88
,
0
,
ifelse
(
Hypoxia
==
2
,
1
,
Hypoxia
)),
Hypotension
=
ifelse
(
Hypotension
==
88
,
0
,
ifelse
(
Hypotension
==
2
,
1
,
Hypotension
)),
Anticoags
=
ifelse
(
Anticoags
==
88
,
NA
,
ifelse
(
Anticoags
==
0
,
0
,
ifelse
(
Anticoags
%in%
c
(
1
:
3
),
1
,
NA
))),
BE
=
round
(
ifelse
(
BE
>
50
,
NA
,
BE
),
digits
=
1
),
MidlineShiftmm
=
ifelse
(
MidlineShiftmm
<
100
,
MidlineShiftmm
,
NA
),
Seizures
=
ifelse
(
Seizures
==
88
,
NA
,
ifelse
(
Seizures
%in%
c
(
2
:
5
),
1
,
Seizures
)),
Hypocapnia
=
ifelse
(
Hypocapnia
==
88
,
NA
,
ifelse
(
Hypocapnia
==
2
,
1
,
Hypocapnia
)),
TempMax
=
ifelse
(
TempMax
>
41
,
NA
,
TempMax
),
PaO2Min
=
round
(
ifelse
(
PaO2Min
<
3
,
NA
,
PaO2Min
),
digits
=
1
),
PaCO2Max
=
round
(
PaCO2Max
,
digits
=
1
),
PaCO2Min
=
round
(
PaCO2Min
,
digits
=
1
),
SpO2Min
=
ifelse
(
SpO2Min
<=
10
,
NA
,
SpO2Min
),
pHMax
=
round
(
ifelse
(
pHMax
<
6
,
NA
,
pHMax
),
digits
=
2
),
pHMin
=
round
(
ifelse
(
pHMin
<
6
,
NA
,
pHMin
),
digits
=
2
),
CSFDrainageVol
=
ifelse
(
CSFDrainageTherapy
==
0
,
NA
,
CSFDrainageVol
),
CSFDrainageYes
=
ifelse
(
CSFDrainageTherapy
>
0
,
1
,
0
),
FluidBal
=
ifelse
(
FluidBal
>
15000
,
NA
,
ifelse
(
FluidBal
<
-10000
,
NA
,
FluidBal
)),
ICPMonitorYes
=
ifelse
(
ICPMonitorType
>
0
,
1
,
ICPMonitorType
),)
# Make means:
CV
<-
CV
%>%
mutate
(
HRMean
=
ifelse
(
is.na
(
HRMean
),
(
HRMax
+
HRMin
)
/
2
,
HRMean
),
HRDiff
=
HRMax
-
HRMin
,
TempMean
=
ifelse
(
is.na
(
TempMean
),
(
TempMax
+
TempMin
)
/
2
,
TempMean
),
TempDiff
=
TempMax
-
TempMin
,
SpO2Mean
=
ifelse
(
is.na
(
SpO2Mean
),
(
SpO2Max
+
SpO2Min
)
/
2
,
SpO2Mean
),
SpO2Diff
=
SpO2Max
-
SpO2Min
,
pHMean
=
ifelse
(
is.na
(
pHMean
),
(
pHMax
+
pHMin
)
/
2
,
pHMean
),
pHDiff
=
pHMax
-
pHMin
,
PaO2Mean
=
ifelse
(
is.na
(
PaO2Mean
),
(
PaO2Max
+
PaO2Min
)
/
2
,
PaO2Mean
),
PaO2Diff
=
PaO2Max
-
PaO2Min
,
PaCO2Mean
=
ifelse
(
is.na
(
PaCO2Mean
),
(
PaCO2Max
+
PaCO2Min
)
/
2
,
PaCO2Mean
),
PaCO2Diff
=
PaCO2Max
-
PaCO2Min
,
MAPMean
=
ifelse
(
!
is.na
(
MAPMean
),
MAPMean
,
ifelse
(
!
is.na
(
HVMAPMean
),
HVMAPMean
,
(
VMAPMax
+
VMAPMin
)
/
2
)),
MAPDiff
=
HVMAPDiff
,
MAPMax
=
ifelse
(
!
is.na
(
HVMAPMax
),
HVMAPMax
,
VMAPMax
),
MAPMin
=
ifelse
(
!
is.na
(
HVMAPMin
),
HVMAPMin
,
VMAPMin
),
GOSE6mo.num
=
ifelse
(
GOSE6mo
==
"2_or_3"
,
3
,
GOSE6mo
))
CV
<-
CV
%>%
mutate
(
MAPMean
=
round
(
MAPMean
,
0
))
CV
<-
CV
%>%
select
(
gupi
:
TotalISS
,
Dead
,
Discharged
,
Intub
,
Anticoags
,
BMI
,
TAI
:
Hypocapnia
,
PupilResponse
,
PupilReactivityWorst
,
GCSScore
,
GCSWorstScore
,
GCSEyes
:
GCSVerbal
,
GCSWorstEyes
:
GCSWorstVerbal
,
HRMean
,
HRDiff
,
HRMin
,
HRMax
,
MAPMean
,
MAPDiff
,
MAPMin
,
MAPMax
,
VMAPMin
,
VMAPMax
,
HVMAPMean
:
HVMAP75
,
HVMAPDiff
,
VSBPMin
,
VSBPMax
,
HVSBPMean
:
HVSBP75
,
HVSBPDiff
,
VDBPMin
,
VDBPMax
,
HVDBPMean
:
HVDBP75
,
HVDBPDiff
,
ICPMean
:
ICPabove30
,
ICPDiff
,
TempMean
,
TempDiff
,
TempMin
,
TempMax
,
SpO2Mean
,
SpO2Diff
,
SpO2Min
,
SpO2Max
,
PaO2Mean
,
PaO2Diff
,
PaO2Min
,
PaO2Max
,
PaCO2Mean
,
PaCO2Diff
,
PaCO2Min
,
PaCO2Max
,
pHMean
,
pHDiff
,
pHMin
,
pHMax
,
Lactate
,
BE
,
SodiumMean
,
SodiumDiff
,
SodiumMin
,
SodiumMax
,
GlucoseMean
,
GlucoseDiff
,
GlucoseMin
,
GlucoseMax
,
AlbuminMean
,
AlbuminMin
,
AlbuminMax
,
PlateletMean
,
PlateletMin
,
PlateletMax
,
CreatinineMean
,
CreatinineMin
,
CreatinineMax
,
HbMean
,
HbMin
,
HbMax
,
S100B
:
NFL
,
TotalTIL
:
DecomprCran
,
ICPMonitorType
,
ICPMonitorYes
,
CSFDrainageYes
,
GOSE6mo.num
)
CV.daily
<-
CV
%>%
select
(
gupi
:
PupilResponse
,
GCSScore
,
GCSEyes
,
GCSMotor
,
GCSVerbal
,
HRMean
,
HRDiff
,
MAPMean
,
MAPDiff
,
ICPMean
,
ICPDiff
,
TempMean
,
TempDiff
,
SpO2Mean
,
SpO2Diff
,
PaO2Mean
,
PaO2Diff
,
PaCO2Mean
,
PaCO2Diff
,
pHMean
,
pHDiff
,
Lactate
,
BE
,
SodiumMean
,
SodiumDiff
,
GlucoseMean
,
GlucoseDiff
,
AlbuminMean
,
PlateletMean
,
CreatinineMean
,
HbMean
,
TotalTIL
,
Position
,
PositionNursedFlat
,
SedationTherapy
,
NeuromuscBlock
,
CSFDrainageTherapy
,
CSFDrainageVol
,
FluidLoading
,
FluidBal
,
Vasopressor
,
HyperventTherapy
,
Mannitol
,
Saline
,
Tempcontrol
,
ICPSurgery
,
DecomprCran
,
S100B
,
NSE
,
GFAP
,
UCH.L1
,
Tau
,
NFL
,
ICPMonitorType
,
ICPMonitorYes
,
CSFDrainageYes
,
GOSE6mo.num
)
View
(
CV.daily
)
# Missingness (data available), before imputation: --------------
View
(
t
(
CV.daily
%>%
filter
(
Dead
==
0
,
Discharged
==
0
)
%>%
group_by
(
Timepoint
)
%>%
summarise_all
(
funs
(
sum
(
!
is.na
(
.
))))))
# Propagate CT, daily therapies (TIL). Imputation by last carried forward: -------------------------
CT2
<-
data.frame
(
gupi
=
as.character
(),
Timepoint
=
as.integer
(),
TAI
=
as.integer
(),
EDH
=
as.integer
(),
aSDH
=
as.integer
(),
Contusion
=
as.integer
(),
tSAH
=
as.integer
(),
MidlineShift
=
as.integer
(),
RotterdamCTScore
=
as.integer
(),
MidlineShiftmm
=
as.numeric
(),
FisherClassification
=
as.integer
(),
Position
=
as.integer
(),
PositionNursedFlat
=
as.integer
(),
SedationTherapy
=
as.integer
(),
NeuromuscBlock
=
as.integer
(),
CSFDrainageTherapy
=
as.integer
(),
FluidLoading
=
as.integer
(),
Vasopressor
=
as.integer
(),
HyperventTherapy
=
as.integer
(),
Mannitol
=
as.integer
(),
Saline
=
as.integer
(),
Tempcontrol
=
as.integer
(),
ICPSurgery
=
as.integer
(),
DecomprCran
=
as.integer
(),
GCSScore
=
as.integer
(),
GCSEyes
=
as.integer
(),
GCSMotor
=
as.integer
(),
GCSVerbal
=
as.integer
())
for
(
j
in
unique
(
CV
$
gupi
))
{
temp
<-
CV.daily
%>%
select
(
gupi
,
Timepoint
,
TAI
:
FisherClassification
,
GCSScore
:
GCSVerbal
,
Position
:
CSFDrainageTherapy
,
FluidLoading
,
Vasopressor
:
DecomprCran
)
%>%
filter
(
gupi
==
j
)
for
(
i
in
2
:
nrow
(
temp
))
for
(
k
in
3
:
ncol
(
temp
))
{
{
temp
[
i
,
k
]
<-
ifelse
(
is.na
(
temp
[
i
,
k
]),
temp
[
i
-1
,
k
],
temp
[
i
,
k
])
}
}
CT2
<-
bind_rows
(
CT2
,
temp
)
}
CT2
<-
CT2
%>%
mutate
(
MidlineShiftmm
=
ifelse
(
is.na
(
MidlineShiftmm
),
0
,
MidlineShiftmm
))
# Add 0 for missing MidlineShift or MidlineShift < 5 mm:
CV.daily
<-
CV.daily
%>%
select
(
-
(
TAI
:
FisherClassification
),
-
(
GCSScore
:
GCSVerbal
),
-
(
Position
:
CSFDrainageTherapy
),
-
FluidLoading
,
-
(
Vasopressor
:
DecomprCran
))
CV.daily
<-
left_join
(
CV.daily
,
CT2
,
by
=
c
(
"gupi"
,
"Timepoint"
))
#--------------------------------------------------
#Impute missing biomarkers and GCS, FluidBal, CSFDrainageVol by interpolation: --------------
temp
<-
CV.daily
%>%
select
(
gupi
,
Timepoint
,
HRMean
:
pHDiff
,
SodiumMean
:
HbMean
,
CSFDrainageVol
:
NFL
)
temp
<-
temp
%>%
pivot_longer
(
cols
=
HRMean
:
NFL
,
names_to
=
"Feature"
,
values_to
=
"Value"
)
BM.imp
<-
temp
%>%
arrange
(
gupi
,
Feature
,
Timepoint
)
%>%
group_by
(
gupi
,
Feature
)
%>%
mutate
(
imp.value
=
na.approx
(
Value
,
na.rm
=
FALSE
))
BM.imp
<-
BM.imp
%>%
select
(
-
Value
)
%>%
group_by
(
gupi
,
Timepoint
)
%>%
pivot_wider
(
names_from
=
Feature
,
values_from
=
imp.value
)
BM.imp
<-
BM.imp
%>%
mutate
(
AlbuminMean
=
round
(
AlbuminMean
,
0
),
CreatinineMean
=
round
(
CreatinineMean
,
0
),
CSFDrainageVol
=
round
(
CSFDrainageVol
,
0
),
FluidBal
=
round
(
FluidBal
,
0
),
GFAP
=
round
(
GFAP
,
2
),
GlucoseDiff
=
round
(
GlucoseDiff
,
1
),
GlucoseMean
=
round
(
GlucoseMean
,
1
),
HbMean
=
round
(
HbMean
,
0
),
HRDiff
=
round
(
HRDiff
,
0
),
HRMean
=
round
(
HRMean
,
0
),
ICPDiff
=
round
(
ICPDiff
,
0
),
ICPMean
=
round
(
ICPMean
,
0
),
MAPDiff
=
round
(
MAPDiff
,
0
),
MAPMean
=
round
(
MAPMean
,
0
),
NFL
=
round
(
NFL
,
2
),
NSE
=
round
(
NSE
,
2
),
PaCO2Diff
=
round
(
PaCO2Diff
,
1
),
PaCO2Mean
=
round
(
PaCO2Mean
,
1
),
PaO2Diff
=
round
(
PaO2Diff
,
1
),
PaO2Mean
=
round
(
PaO2Mean
,
1
),
pHDiff
=
round
(
pHDiff
,
2
),
pHMean
=
round
(
pHMean
,
2
),
PlateletMean
=
round
(
PlateletMean
,
0
),
S100B
=
round
(
S100B
,
2
),
SodiumDiff
=
round
(
SodiumDiff
,
0
),
SodiumMean
=
round
(
SodiumMean
,
0
),
SpO2Diff
=
round
(
SpO2Diff
,
0
),
SpO2Mean
=
round
(
SpO2Mean
,
0
),
Tau
=
round
(
Tau
,
2
),
TempDiff
=
round
(
TempDiff
,
1
),
TempMean
=
round
(
TempMean
,
1
),
UCH.L1
=
round
(
UCH.L1
,
2
))
CV.daily
<-
CV.daily
%>%
select
(
-
(
HRMean
:
pHDiff
),
-
(
SodiumMean
:
HbMean
),
-
(
CSFDrainageVol
:
NFL
))
CV.daily
<-
left_join
(
CV.daily
,
BM.imp
,
by
=
c
(
"gupi"
,
"Timepoint"
))
# ------------------------------------------------------------
# Impute hypoxia, hypotension, seizures, hypocapnia, PupilResponse by NA -> 0: --------------
CV.daily
<-
CV.daily
%>%
mutate
(
Seizures
=
ifelse
(
is.na
(
Seizures
),
0
,
Seizures
),
Hypoxia
=
ifelse
(
is.na
(
Hypoxia
),
0
,
Hypoxia
),
Hypotension
=
ifelse
(
is.na
(
Hypotension
),
0
,
Hypotension
),
Hypocapnia
=
ifelse
(
is.na
(
Hypocapnia
),
0
,
Hypocapnia
),
PupilResponse
=
ifelse
(
is.na
(
PupilResponse
),
0
,
PupilResponse
))
#--------------------
# Missingness (data available), after imputation: --------------
View
(
t
(
CV.daily
%>%
filter
(
Dead
==
0
,
Discharged
==
0
)
%>%
group_by
(
Timepoint
)
%>%
summarise_all
(
funs
(
sum
(
!
is.na
(
.
))))))
View
(
CV.daily
%>%
filter
(
Dead
==
0
,
Discharged
==
0
))
# write.csv(CV.daily, file=paste0(f3,"TS_vars.csv"), row.names = FALSE)
# Make Daily vars: ----------------------------------------
# For every day daily vars, see previous script. Here, wide daily vars is created.
x
<-
c
(
"Dead"
,
"Discharged"
,
"TAI"
,
"EDH"
,
"aSDH"
,
"Contusion"
,
"tSAH"
,
"RotterdamCTScore"
,
"MidlineShiftmm"
,
"FisherClassification"
,
"HRDiff"
,
"MAPDiff"
,
"ICPMean"
,
"ICPDiff"
,
"TempDiff"
,
"SpO2Diff"
,
"PaO2Diff"
,
"PaCO2Diff"
,
"pHDiff"
,
"AlbuminMean"
,
"CreatinineMean"
,
"PlateletMean"
,
"HbMean"
,
"SodiumDiff"
,
"SodiumMean"
,
"GlucoseMean"
,
"GlucoseDiff"
,
"S100B"
,
"NSE"
,
"GFAP"
,
"UCH.L1"
,
"Tau"
,
"NFL"
,
"Position"
,
"PositionNursedFlat"
,
"SedationTherapy"
,
"NeuromuscBlock"
,
"CSFDrainageTherapy"
,
"CSFDrainageVol"
,
"FluidLoading"
,
"Vasopressor"
,
"HyperventTherapy"
,
"Mannitol"
,
"Saline"
,
"Tempcontrol"
,
"ICPSurgery"
,
"DecomprCran"
,
"FluidBal"
,
"ICPMonitorYes"
,
"TotalTIL"
)
# Values on all days (not d0)
y
<-
c
(
"Age"
,
"Sex"
,
"ASAClass"
,
"InjType"
,
"InjCause"
,
"Anticoags"
,
"BMI"
,
"Lactate"
,
"BE"
,
"HeadISS"
,
"ExtracranISSHighest"
,
"TotalISS"
)
# Values on only first day
CV
<-
read.csv
(
file
=
paste0
(
f3
,
"TS_vars.csv"
),
na.strings
=
nas
)
CV.long
<-
CV
#%>% select(-(HeadISS:TotalISS))
CV.long
<-
CV
%>%
select
(
gupi
,
Timepoint
,
Dead
,
Discharged
,
everything
())
for
(
i
in
1
:
nrow
(
CV.long
))
#Set all values to NA if patient is dead or discharged.
{
for
(
j
in
5
:
ncol
(
CV.long
))
{
CV.long
[
i
,
j
]
<-
ifelse
(
CV.long
$
Dead
[
i
]
==
1
|
CV.long
$
Discharged
[
i
]
==
1
,
NA
,
CV.long
[
i
,
j
])
}
}
colnames
(
CV.long
)
CV.long
<-
CV.long
%>%
select
(
-
Dead
,
-
Discharged
,
-
GOSE6mo
)
CV.long
<-
CV.long
%>%
pivot_longer
(
cols
=
Age
:
UCH.L1
,
names_to
=
"Feature"
,
values_to
=
"Value"
)
temp2
<-
CV.long
%>%
filter
(
Timepoint
==
0
,
!
Feature
%in%
x
)
temp3
<-
CV.long
%>%
filter
(
Timepoint
%in%
c
(
1
:
7
),
!
Feature
%in%
y
)
CV.long
<-
rbind
(
temp2
,
temp3
)
rm
(
temp2
,
temp3
)
unique
(
CV.long
$
Feature
)
CV2
<-
CV.long
%>%
filter
(
!
grepl
(
"ICPabove"
,
Feature
),
!
grepl
(
"GOSE6mo"
,
Feature
),
!
grepl
(
"ISS"
,
Feature
),
!
grepl
(
"GCSScore"
,
Feature
),
!
grepl
(
"MidlineShift"
,
Feature
),
!
grepl
(
"TotalTIL"
,
Feature
),
!
grepl
(
"CSFDrainageTherapy"
,
Feature
),
!
grepl
(
"CSFDrainageYes"
,
Feature
),
!
grepl
(
"ICPMonitorType"
,
Feature
))
CV2
<-
CV2
%>%
mutate
(
Feature
=
ifelse
(
!
Feature
%in%
y
,
paste0
(
Feature
,
"."
,
Timepoint
),
Feature
))
CV2
<-
CV2
%>%
select
(
-
Timepoint
)
CV2
<-
spread
(
CV2
,
"Feature"
,
"Value"
)
View
(
t
(
CV2
%>%
summarise_all
(
funs
(
sum
(
!
is.na
(
.
))))))
# write.csv(CV2, file=paste0(f2, "Clustering Time-series CENTER-TBI/Clustervars_TS/TS_vars.alldays.csv"), row.names = FALSE)
# CV <- read.csv(file=paste0(f2,"Clustering Time-series CENTER-TBI/Clustervars_TS/TS_vars.csv"), na.strings = nas)
# Create list of features to add in hgmtest-file:-------
temp2
<-
read.csv
(
paste0
(
f3
,
"TS_vars.alldays.csv"
))
View
(
temp2
)
joint.features
<-
colnames
(
CV2
%>%
select
(
contains
(
"4"
)))
joint.features
<-
gsub
(
".4"
,
""
,
joint.features
)
joint.features07
<-
c
()
joint.features17
<-
c
()
for
(
i
in
(
1
:
length
(
joint.features
)))
{
temp
<-
ncol
(
CV2
%>%
select
(
contains
(
joint.features
[
i
])))
if
(
temp
==
7
)
{
joint.features17
<-
c
(
joint.features17
,
joint.features
[
i
])
}
if
(
temp
==
8
)
{
joint.features07
<-
c
(
joint.features07
,
joint.features
[
i
])
}
}
cmodel
<-
c
()
for
(
i
in
1
:
length
(
colnames
(
CV2
)))
{
cmodel
<-
c
(
cmodel
,
paste0
(
"add_feature(pmod, data->format(), \""
,
colnames
(
CV2
)[
i
],
"\");"
))
}
cmodel
<-
c
(
cmodel
,
"if (graph) {"
)
cmodel
<-
c
(
cmodel
,
paste0
(
"add_joint_feature(pmod, data->format(), \"Age\", \"ASAClass\");"
))
cmodel
<-
c
(
cmodel
,
paste0
(
"add_joint_feature(pmod, data->format(), \"Age\", \"Anticoags\");"
))
cmodel
<-
c
(
cmodel
,
paste0
(
"add_joint_feature(pmod, data->format(), \"BE\", \"pHMean.0\");"
))
for
(
i
in
1
:
length
(
joint.features07
))
{
for
(
j
in
0
:
6
)
cmodel
<-
c
(
cmodel
,
paste0
(
"add_joint_feature(pmod, data->format(), \""
,
joint.features07
[
i
],
"."
,
j
,
"\","
,
" \""
,
joint.features07
[
i
],
"."
,
j
+1
,
"\");"
))
}
for
(
i
in
1
:
length
(
joint.features17
))
{
for
(
j
in
1
:
6
)
cmodel
<-
c
(
cmodel
,
paste0
(
"add_joint_feature(pmod, data->format(), \""
,
joint.features17
[
i
],
"."
,
j
,
"\","
,
" \""
,
joint.features17
[
i
],
"."
,
j
+1
,
"\");"
))
}
cmodel
<-
c
(
cmodel
[
-1
],
"}"
)
# write(cmodel, paste0(f3,"cmodel.txt"))
CreatingModelLists_TS_220826.R
0 → 100644
View file @
63b3a308
rm
(
list
=
ls
())
library
(
dplyr
)
library
(
ggplot2
)
library
(
gtools
)
# f <- paste0("C:/Users/Cecilia KI/Dropbox/Cecilia/R_code/Cluster_CENTER-TBI/EM_timeseries/models/")
# f2 <- paste0("C:/Users/Cecilia KI/Dropbox/Cecilia/R_code/Cluster_CENTER-TBI/EM_timeseries/models.alldays/")
# f3 <- paste0("C:/Users/Cecilia KI/Dropbox/Cecilia/R_code/Cluster_CENTER-TBI/EM_timeseries/models.alldays.all/")
# f <- "C:/Users/cecil/Dropbox/Cecilia/R_code/Cluster_CENTER-TBI/EM_timeseries/models/"
f
<-
paste0
(
"/Users/ceciliamac/Dropbox/Cecilia/R_code/Cluster_CENTER-TBI/EM_timeseries/models210524/models"
)
f2
<-
paste0
(
"/Users/ceciliamac/Dropbox/Cecilia/R_code/Cluster_CENTER-TBI/EM_timeseries/models.alldays/"
)
f3
<-
paste0
(
"/Users/ceciliamac/Dropbox/Cecilia/R_code/Cluster_CENTER-TBI/EM_timeseries/models.alldays.500"
)
FOLDERS
<-
list.dirs
(
f
)[
2
:
length
(
list.dirs
(
f
))]
Createmodellist
<-
function
(
models
,
df
,
belong
,
k
)
{
g
=
paste0
(
FOLDERS
[
j
],
"/"
)
models
<-
mixedsort
(
models
[
grepl
(
paste0
(
"Maxmodel"
,
k
),
models
)])
df
<-
mixedsort
(
df
[
grepl
(
paste0
(
"df"
,
k
),
df
)])
belong
<-
mixedsort
(
belong
[
grepl
(
paste0
(
"belongprob"
,
k
),
belong
)])
Model.list
<-
list
()
for
(
i
in
1
:
length
(
models
))
{
# name <- gsub("Maxmodel","",Models[i])
# name <- gsub(".txt","", name)
# name <- gsub("\\..*","", name)
source
(
paste0
(
g
,
models
[
i
]))
Model.list
[[
i
]]
<-
mod
}
# Add df and belong:
for
(
i
in
1
:
length
(
df
))
{
Model.list
[[
i
]]
$
df
<-
read.delim
(
paste0
(
g
,
df
[
i
]),
sep
=
","
)
colnames
(
Model.list
[[
i
]]
$
df
)
<-
c
(
"iter"
,
"difference"
)
}
for
(
i
in
1
:
length
(
belong
))
{
Model.list
[[
i
]]
$
belong
<-
read.delim
(
paste0
(
g
,
belong
[
i
]),
header
=
FALSE
,
sep
=
","
)
colnames
(
Model.list
[[
i
]]
$
belong
)
<-
c
(
"gupi"
,
"clustnumber"
,
1
:
k
)
Model.list
[[
i
]]
$
filename
<-
belong
[
i
]
#Added 211124 (to keep track of seed)
}
return
(
Model.list
)
}
Model.list
<-
vector
(
mode
=
"list"
,
length
=
length
(
FOLDERS
))
for
(
j
in
(
1
:
length
(
FOLDERS
)))
{
Models
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"Maxmodel"
)
df
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"df"
)
Belong
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"belong"
)
source
(
file
=
paste0
(
FOLDERS
[
j
],
"/"
,
Models
[[
1
]]))
Model.list
[[
j
]][[
1
]]
<-
mod
Model.list
[[
j
]][[
1
]]
$
df
<-
read.delim
(
paste0
(
FOLDERS
[
j
],
"/"
,
df
[
1
]),
sep
=
","
)
colnames
(
Model.list
[[
j
]][[
1
]]
$
df
)
<-
c
(
"iter"
,
"difference"
)
Model.list
[[
j
]][[
1
]]
$
belong
<-
read.delim
(
paste0
(
FOLDERS
[
j
],
"/"
,
Belong
[
1
]),
header
=
FALSE
,
sep
=
","
)
colnames
(
Model.list
[[
j
]][[
1
]]
$
belong
)
<-
c
(
"gupi"
,
"clustnumber"
,
1
)
#if(j == 2)
#{kmax <- 10} else{kmax <- 15}
kmax
<-
10
for
(
k
in
3
:
kmax
)
{
Model.list
[[
j
]][[
k
]]
<-
Createmodellist
(
models
=
Models
,
df
,
belong
=
Belong
,
k
)
}
}
names
(
Model.list
)
<-
c
(
"Day0"
,
"Day1"
,
"Day2"
,
"Day3"
,
"Day4"
,
"Day5"
,
"Day6"
,
"Day7"
)
# save(Model.list, file=paste0(f,"Model.list.RData"))
#View(Model.list)
# List of models all days clustered together:
# All days, trajectory model:
FOLDERS
<-
f2
Model.list.alldays
<-
vector
(
mode
=
"list"
,
length
=
length
(
FOLDERS
))
for
(
j
in
1
:
length
(
FOLDERS
))
{
Models
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"Maxmodel"
)
df
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"df"
)
Belong
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"belong"
)
source
(
file
=
paste0
(
FOLDERS
[
j
],
"/"
,
Models
[[
1
]]))
Model.list.alldays
[[
j
]][[
1
]]
<-
mod
Model.list.alldays
[[
j
]][[
1
]]
$
df
<-
read.delim
(
paste0
(
FOLDERS
[
j
],
"/"
,
df
[
1
]),
sep
=
","
)
colnames
(
Model.list.alldays
[[
j
]][[
1
]]
$
df
)
<-
c
(
"iter"
,
"difference"
)
Model.list.alldays
[[
j
]][[
1
]]
$
belong
<-
read.delim
(
paste0
(
FOLDERS
[
j
],
"/"
,
Belong
[
1
]),
header
=
FALSE
,
sep
=
","
)
colnames
(
Model.list.alldays
[[
j
]][[
1
]]
$
belong
)
<-
c
(
"gupi"
,
"clustnumber"
,
1
)
#if(j == 2)
#{kmax <- 10} else{kmax <- 15}
kmax
<-
10
for
(
k
in
3
:
kmax
)
{
Model.list.alldays
[[
j
]][[
k
]]
<-
Createmodellist
(
models
=
Models
,
df
,
belong
=
Belong
,
k
)
}
}
View
(
Model.list.alldays
)
# save(Model.list.alldays, file=paste0(f2,"Model.list.alldays.RData"))
# For 500 models each day, make list of only belong files:
Createbelonglist
<-
function
(
models
,
df
,
belong
,
k
)
{
g
=
paste0
(
FOLDERS
[
j
],
"/"
)
models
<-
mixedsort
(
models
[
grepl
(
paste0
(
"Maxmodel"
,
k
),
models
)])
df
<-
mixedsort
(
df
[
grepl
(
paste0
(
"df"
,
k
),
df
)])
belong
<-
mixedsort
(
belong
[
grepl
(
paste0
(
"belongprob"
,
k
),
belong
)])
Model.list
<-
list
()
for
(
i
in
1
:
length
(
models
))
{
# name <- gsub("Maxmodel","",Models[i])
# name <- gsub(".txt","", name)
# name <- gsub("\\..*","", name)
source
(
paste0
(
g
,
models
[
i
]))
Model.list
[[
i
]]
$
LLMax
<-
mod
$
LLMax
Model.list
[[
i
]]
$
Entropy
<-
mod
$
Entropy
}
# Add df and belong:
for
(
i
in
1
:
length
(
df
))
{
Model.list
[[
i
]]
$
df
<-
read.delim
(
paste0
(
g
,
df
[
i
]),
sep
=
","
)
colnames
(
Model.list
[[
i
]]
$
df
)
<-
c
(
"iter"
,
"difference"
)
}
for
(
i
in
1
:
length
(
belong
))
{
Model.list
[[
i
]]
$
belong
<-
read.delim
(
paste0
(
g
,
belong
[
i
]),
header
=
FALSE
,
sep
=
","
)
colnames
(
Model.list
[[
i
]]
$
belong
)
<-
c
(
"gupi"
,
"clustnumber"
,
1
:
k
)
Model.list
[[
i
]]
$
filename
<-
belong
[
i
]
#Added 211124 (to keep track of seed)
}
return
(
Model.list
)
}
FOLDERS
<-
f3
Model.list.alldays.500
<-
vector
(
mode
=
"list"
,
length
=
length
(
FOLDERS
))
for
(
j
in
1
:
length
(
FOLDERS
))
{
print
(
j
)
Models
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"Maxmodel"
)
df
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"df"
)
Belong
<-
list.files
(
FOLDERS
[
j
],
pattern
=
"belong"
)
# source(file=paste0(FOLDERS[j], "/", Models[[1]]))
# Model.list.alldays.500[[j]][[1]] <- mod
# Model.list.alldays.500[[j]][[1]]$df <- read.delim(paste0(FOLDERS[j],"/", df[1]), sep=",")
# colnames(Model.list.alldays.500[[j]][[1]]$df) <- c("iter", "difference")
# Model.list.alldays.500[[j]][[1]]$belong <- read.delim(paste0(FOLDERS[j],"/",Belong[1]), header=FALSE, sep=",")
# colnames(Model.list.alldays.500[[j]][[1]]$belong) <- c("gupi", "clustnumber", 1)
#
#if(j == 2)
#{kmax <- 10} else{kmax <- 15}
kmax
<-
15
for
(
k
in
3
:
kmax
)
{
print
(
k
)
Model.list.alldays.500
[[
j
]][[
k
]]
<-
Createmodellist
(
models
=
Models
,
df
,
belong
=
Belong
,
k
)
}
}
View
(
Model.list.alldays.500
)
# save(Model.list.alldays.500, file=paste0(f3,"Model.list.alldays.500.RData"))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment