Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
shared
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
02460F20-Advanced Machine Learning - TSAD project
shared
Commits
8e2a8519
Commit
8e2a8519
authored
Mar 25, 2020
by
milenabaj
Browse files
Options
Downloads
Patches
Plain Diff
a script to load postgreSQL database into python
parent
52446e91
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
postgre-test.py
+63
-0
63 additions, 0 deletions
postgre-test.py
with
63 additions
and
0 deletions
postgre-test.py
0 → 100755
+
63
−
0
View file @
8e2a8519
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 28 15:19:55 2020
@author: milena
"""
import
psycopg2
import
sys
import
json
import
pandas
as
pd
import
numpy
as
np
import
matplotlib.pyplot
as
plt
def
get_var_data
(
data
,
var_name
):
"""
Return numpy array and pandas dataframe for selected variable from data fetched from the server.
"""
var_data
=
np
.
empty
([])
for
row
in
data
:
this_ev
=
json
.
loads
(
row
[
0
])
this_ev_var
=
this_ev
[
var_name
]
var_data
=
np
.
append
(
var_data
,
this_ev_var
)
var_data_pd
=
pd
.
DataFrame
(
var_data
)
return
var_data
,
var_data_pd
def
get_var_names
(
data
):
"""
Get the list of variable names in messages.
"""
return
list
(
json
.
loads
(
data
[
0
][
0
]).
keys
())
#======== START HERE ========#
#============================#
# Connect to DB
conn
=
psycopg2
.
connect
(
database
=
"
Lira
"
,
password
=
'
postgres
'
,
user
=
"
postgres
"
,
host
=
"
127.0.0.1
"
)
cursor
=
conn
.
cursor
()
print
(
conn
.
get_dsn_parameters
(),
'
\n
'
)
# Print PostgreSQL Connection properties
# Execute quory
cursor
.
execute
(
'
SELECT message FROM
"
Measurements
"
WHERE
"
FK_MeasurementType
"
=2
'
)
data
=
cursor
.
fetchall
()
# Get variable names
all_vars
=
get_var_names
(
data
)
# Test: Get one var
acc_z_np
,
acc_z_pd
=
get_var_data
(
data
,
'
acc.xyz.z
'
)
plt
.
plot
(
acc_z_np
)
plt
.
title
(
'
acc.xyz.z
'
)
# Get data for all variables into np arrays and pd dataframes
loaded_data
=
{}
for
var
in
all_vars
:
var_np
,
var_pd
=
get_var_data
(
data
,
var
)
loaded_data
[
var
]
=
{
'
numpy
'
:
var_np
,
'
pandas
'
:
var_pd
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment