Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mhc_rank
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Patrick Skillman-Lawrence
mhc_rank
Commits
5545f31f
Commit
5545f31f
authored
6 years ago
by
Tim O'Donnell
Browse files
Options
Downloads
Patches
Plain Diff
add missing file
parent
a96ecdd1
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mhcflurry/allele_encoding_transforms.py
+77
-0
77 additions, 0 deletions
mhcflurry/allele_encoding_transforms.py
with
77 additions
and
0 deletions
mhcflurry/allele_encoding_transforms.py
0 → 100644
+
77
−
0
View file @
5545f31f
import
time
import
pandas
import
sklearn.decomposition
class
AlleleEncodingTransform
(
object
):
def
transform
(
self
,
data
):
raise
NotImplementedError
()
def
get_fit
(
self
):
"""
Get the fit for serialization, which must be in the form of one or more
dataframes.
Returns
-------
dict : string to DataFrame
"""
raise
NotImplementedError
()
def
restore_fit
(
self
,
fit
):
"""
Restore a serialized fit.
Parameters
----------
fit : string to DataFrame
"""
class
PCATransform
(
AlleleEncodingTransform
):
name
=
'
pca
'
serialization_keys
=
[
'
data
'
]
def
__init__
(
self
):
self
.
model
=
None
def
is_fit
(
self
):
return
self
.
model
is
not
None
def
fit
(
self
,
allele_representations
):
self
.
model
=
sklearn
.
decomposition
.
PCA
()
shape
=
allele_representations
.
shape
flattened
=
allele_representations
.
reshape
(
(
shape
[
0
],
shape
[
1
]
*
shape
[
2
]))
print
(
"
Fitting PCA allele encoding transform on data of shape: %s
"
%
(
str
(
flattened
.
shape
)))
start
=
time
.
time
()
self
.
model
.
fit
(
flattened
)
print
(
"
Fit complete in %0.3f sec.
"
%
(
time
.
time
()
-
start
))
def
get_fit
(
self
):
df
=
pandas
.
DataFrame
(
self
.
model
.
components_
)
df
.
columns
=
[
"
pca_%s
"
%
c
for
c
in
df
.
columns
]
df
[
"
mean
"
]
=
self
.
model
.
mean_
return
{
'
data
'
:
df
}
def
restore_fit
(
self
,
fit
):
assert
list
(
fit
)
==
[
'
data
'
]
data
=
fit
[
"
data
"
]
self
.
model
=
sklearn
.
decomposition
.
PCA
()
self
.
model
.
mean_
=
data
[
"
mean
"
].
values
self
.
model
.
components_
=
data
.
drop
(
columns
=
"
mean
"
).
values
def
transform
(
self
,
allele_representations
):
if
not
self
.
is_fit
():
self
.
fit
(
allele_representations
)
flattened
=
allele_representations
.
reshape
(
(
allele_representations
.
shape
[
0
],
allele_representations
.
shape
[
1
]
*
allele_representations
.
shape
[
2
]))
return
self
.
model
.
transform
(
flattened
)
TRANSFORMS
=
dict
((
klass
.
name
,
klass
)
for
klass
in
[
PCATransform
])
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